1
0

test_opentracing.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import cast
  15. from twisted.internet import defer
  16. from twisted.test.proto_helpers import MemoryReactorClock
  17. from synapse.logging.context import (
  18. LoggingContext,
  19. make_deferred_yieldable,
  20. run_in_background,
  21. )
  22. from synapse.logging.opentracing import (
  23. start_active_span,
  24. start_active_span_follows_from,
  25. tag_args,
  26. trace_with_opname,
  27. )
  28. from synapse.util import Clock
  29. try:
  30. from synapse.logging.scopecontextmanager import LogContextScopeManager
  31. except ImportError:
  32. LogContextScopeManager = None # type: ignore
  33. try:
  34. import jaeger_client
  35. except ImportError:
  36. jaeger_client = None # type: ignore
  37. import logging
  38. from tests.unittest import TestCase
  39. logger = logging.getLogger(__name__)
  40. class LogContextScopeManagerTestCase(TestCase):
  41. """
  42. Test logging contexts and active opentracing spans.
  43. There's casts throughout this from generic opentracing objects (e.g.
  44. opentracing.Span) to the ones specific to Jaeger since they have additional
  45. properties that these tests depend on. This is safe since the only supported
  46. opentracing backend is Jaeger.
  47. """
  48. if LogContextScopeManager is None:
  49. skip = "Requires opentracing" # type: ignore[unreachable]
  50. if jaeger_client is None:
  51. skip = "Requires jaeger_client" # type: ignore[unreachable]
  52. def setUp(self) -> None:
  53. # since this is a unit test, we don't really want to mess around with the
  54. # global variables that power opentracing. We create our own tracer instance
  55. # and test with it.
  56. scope_manager = LogContextScopeManager()
  57. config = jaeger_client.config.Config(
  58. config={}, service_name="test", scope_manager=scope_manager
  59. )
  60. self._reporter = jaeger_client.reporter.InMemoryReporter()
  61. self._tracer = config.create_tracer(
  62. sampler=jaeger_client.ConstSampler(True),
  63. reporter=self._reporter,
  64. )
  65. def test_start_active_span(self) -> None:
  66. # the scope manager assumes a logging context of some sort.
  67. with LoggingContext("root context"):
  68. self.assertIsNone(self._tracer.active_span)
  69. # start_active_span should start and activate a span.
  70. scope = start_active_span("span", tracer=self._tracer)
  71. span = cast(jaeger_client.Span, scope.span)
  72. self.assertEqual(self._tracer.active_span, span)
  73. self.assertIsNotNone(span.start_time)
  74. # entering the context doesn't actually do a whole lot.
  75. with scope as ctx:
  76. self.assertIs(ctx, scope)
  77. self.assertEqual(self._tracer.active_span, span)
  78. # ... but leaving it unsets the active span, and finishes the span.
  79. self.assertIsNone(self._tracer.active_span)
  80. self.assertIsNotNone(span.end_time)
  81. # the span should have been reported
  82. self.assertEqual(self._reporter.get_spans(), [span])
  83. def test_nested_spans(self) -> None:
  84. """Starting two spans off inside each other should work"""
  85. with LoggingContext("root context"):
  86. with start_active_span("root span", tracer=self._tracer) as root_scope:
  87. self.assertEqual(self._tracer.active_span, root_scope.span)
  88. root_context = cast(jaeger_client.SpanContext, root_scope.span.context)
  89. scope1 = start_active_span(
  90. "child1",
  91. tracer=self._tracer,
  92. )
  93. self.assertEqual(
  94. self._tracer.active_span, scope1.span, "child1 was not activated"
  95. )
  96. context1 = cast(jaeger_client.SpanContext, scope1.span.context)
  97. self.assertEqual(context1.parent_id, root_context.span_id)
  98. scope2 = start_active_span_follows_from(
  99. "child2",
  100. contexts=(scope1,),
  101. tracer=self._tracer,
  102. )
  103. self.assertEqual(self._tracer.active_span, scope2.span)
  104. context2 = cast(jaeger_client.SpanContext, scope2.span.context)
  105. self.assertEqual(context2.parent_id, context1.span_id)
  106. with scope1, scope2:
  107. pass
  108. # the root scope should be restored
  109. self.assertEqual(self._tracer.active_span, root_scope.span)
  110. span2 = cast(jaeger_client.Span, scope2.span)
  111. span1 = cast(jaeger_client.Span, scope1.span)
  112. self.assertIsNotNone(span2.end_time)
  113. self.assertIsNotNone(span1.end_time)
  114. self.assertIsNone(self._tracer.active_span)
  115. # the spans should be reported in order of their finishing.
  116. self.assertEqual(
  117. self._reporter.get_spans(), [scope2.span, scope1.span, root_scope.span]
  118. )
  119. def test_overlapping_spans(self) -> None:
  120. """Overlapping spans which are not neatly nested should work"""
  121. reactor = MemoryReactorClock()
  122. clock = Clock(reactor)
  123. scopes = []
  124. async def task(i: int):
  125. scope = start_active_span(
  126. f"task{i}",
  127. tracer=self._tracer,
  128. )
  129. scopes.append(scope)
  130. self.assertEqual(self._tracer.active_span, scope.span)
  131. await clock.sleep(4)
  132. self.assertEqual(self._tracer.active_span, scope.span)
  133. scope.close()
  134. async def root():
  135. with start_active_span("root span", tracer=self._tracer) as root_scope:
  136. self.assertEqual(self._tracer.active_span, root_scope.span)
  137. scopes.append(root_scope)
  138. d1 = run_in_background(task, 1)
  139. await clock.sleep(2)
  140. d2 = run_in_background(task, 2)
  141. # because we did run_in_background, the active span should still be the
  142. # root.
  143. self.assertEqual(self._tracer.active_span, root_scope.span)
  144. await make_deferred_yieldable(
  145. defer.gatherResults([d1, d2], consumeErrors=True)
  146. )
  147. self.assertEqual(self._tracer.active_span, root_scope.span)
  148. with LoggingContext("root context"):
  149. # start the test off
  150. d1 = defer.ensureDeferred(root())
  151. # let the tasks complete
  152. reactor.pump((2,) * 8)
  153. self.successResultOf(d1)
  154. self.assertIsNone(self._tracer.active_span)
  155. # the spans should be reported in order of their finishing: task 1, task 2,
  156. # root.
  157. self.assertEqual(
  158. self._reporter.get_spans(),
  159. [scopes[1].span, scopes[2].span, scopes[0].span],
  160. )
  161. def test_trace_decorator_sync(self) -> None:
  162. """
  163. Test whether we can use `@trace_with_opname` (`@trace`) and `@tag_args`
  164. with sync functions
  165. """
  166. with LoggingContext("root context"):
  167. @trace_with_opname("fixture_sync_func", tracer=self._tracer)
  168. @tag_args
  169. def fixture_sync_func() -> str:
  170. return "foo"
  171. result = fixture_sync_func()
  172. self.assertEqual(result, "foo")
  173. # the span should have been reported
  174. self.assertEqual(
  175. [span.operation_name for span in self._reporter.get_spans()],
  176. ["fixture_sync_func"],
  177. )
  178. def test_trace_decorator_deferred(self) -> None:
  179. """
  180. Test whether we can use `@trace_with_opname` (`@trace`) and `@tag_args`
  181. with functions that return deferreds
  182. """
  183. reactor = MemoryReactorClock()
  184. with LoggingContext("root context"):
  185. @trace_with_opname("fixture_deferred_func", tracer=self._tracer)
  186. @tag_args
  187. def fixture_deferred_func() -> "defer.Deferred[str]":
  188. d1: defer.Deferred[str] = defer.Deferred()
  189. d1.callback("foo")
  190. return d1
  191. result_d1 = fixture_deferred_func()
  192. # let the tasks complete
  193. reactor.pump((2,) * 8)
  194. self.assertEqual(self.successResultOf(result_d1), "foo")
  195. # the span should have been reported
  196. self.assertEqual(
  197. [span.operation_name for span in self._reporter.get_spans()],
  198. ["fixture_deferred_func"],
  199. )
  200. def test_trace_decorator_async(self) -> None:
  201. """
  202. Test whether we can use `@trace_with_opname` (`@trace`) and `@tag_args`
  203. with async functions
  204. """
  205. reactor = MemoryReactorClock()
  206. with LoggingContext("root context"):
  207. @trace_with_opname("fixture_async_func", tracer=self._tracer)
  208. @tag_args
  209. async def fixture_async_func() -> str:
  210. return "foo"
  211. d1 = defer.ensureDeferred(fixture_async_func())
  212. # let the tasks complete
  213. reactor.pump((2,) * 8)
  214. self.assertEqual(self.successResultOf(d1), "foo")
  215. # the span should have been reported
  216. self.assertEqual(
  217. [span.operation_name for span in self._reporter.get_spans()],
  218. ["fixture_async_func"],
  219. )