@@ -174,10 +174,48 @@ def set_fates(clotho, lachesis, atropos, father="Zues", mother="Themis"):
174
174
175
175
from synapse .config import ConfigError
176
176
177
+ # Helper class
178
+
179
+
180
+ class _DummyTagNames (object ):
181
+ """wrapper of opentracings tags. We need to have them if we
182
+ want to reference them without opentracing around. Clearly they
183
+ should never actually show up in a trace. `set_tags` overwrites
184
+ these with the correct ones."""
185
+
186
+ INVALID_TAG = "invalid-tag"
187
+ COMPONENT = INVALID_TAG
188
+ DATABASE_INSTANCE = INVALID_TAG
189
+ DATABASE_STATEMENT = INVALID_TAG
190
+ DATABASE_TYPE = INVALID_TAG
191
+ DATABASE_USER = INVALID_TAG
192
+ ERROR = INVALID_TAG
193
+ HTTP_METHOD = INVALID_TAG
194
+ HTTP_STATUS_CODE = INVALID_TAG
195
+ HTTP_URL = INVALID_TAG
196
+ MESSAGE_BUS_DESTINATION = INVALID_TAG
197
+ PEER_ADDRESS = INVALID_TAG
198
+ PEER_HOSTNAME = INVALID_TAG
199
+ PEER_HOST_IPV4 = INVALID_TAG
200
+ PEER_HOST_IPV6 = INVALID_TAG
201
+ PEER_PORT = INVALID_TAG
202
+ PEER_SERVICE = INVALID_TAG
203
+ SAMPLING_PRIORITY = INVALID_TAG
204
+ SERVICE = INVALID_TAG
205
+ SPAN_KIND = INVALID_TAG
206
+ SPAN_KIND_CONSUMER = INVALID_TAG
207
+ SPAN_KIND_PRODUCER = INVALID_TAG
208
+ SPAN_KIND_RPC_CLIENT = INVALID_TAG
209
+ SPAN_KIND_RPC_SERVER = INVALID_TAG
210
+
211
+
177
212
try :
178
213
import opentracing
214
+
215
+ tags = opentracing .tags
179
216
except ImportError :
180
217
opentracing = None
218
+ tags = _DummyTagNames
181
219
try :
182
220
from jaeger_client import Config as JaegerConfig
183
221
from synapse .logging .scopecontextmanager import LogContextScopeManager
@@ -252,10 +290,6 @@ def init_tracer(config):
252
290
scope_manager = LogContextScopeManager (config ),
253
291
).initialize_tracer ()
254
292
255
- # Set up tags to be opentracing's tags
256
- global tags
257
- tags = opentracing .tags
258
-
259
293
260
294
# Whitelisting
261
295
@@ -334,8 +368,8 @@ def start_active_span_follows_from(operation_name, contexts):
334
368
return scope
335
369
336
370
337
- def start_active_span_from_context (
338
- headers ,
371
+ def start_active_span_from_request (
372
+ request ,
339
373
operation_name ,
340
374
references = None ,
341
375
tags = None ,
@@ -344,9 +378,9 @@ def start_active_span_from_context(
344
378
finish_on_close = True ,
345
379
):
346
380
"""
347
- Extracts a span context from Twisted Headers .
381
+ Extracts a span context from a Twisted Request .
348
382
args:
349
- headers (twisted.web.http_headers.Headers )
383
+ headers (twisted.web.http.Request )
350
384
351
385
For the other args see opentracing.tracer
352
386
@@ -360,7 +394,9 @@ def start_active_span_from_context(
360
394
if opentracing is None :
361
395
return _noop_context_manager ()
362
396
363
- header_dict = {k .decode (): v [0 ].decode () for k , v in headers .getAllRawHeaders ()}
397
+ header_dict = {
398
+ k .decode (): v [0 ].decode () for k , v in request .requestHeaders .getAllRawHeaders ()
399
+ }
364
400
context = opentracing .tracer .extract (opentracing .Format .HTTP_HEADERS , header_dict )
365
401
366
402
return opentracing .tracer .start_active_span (
@@ -448,7 +484,7 @@ def set_operation_name(operation_name):
448
484
449
485
450
486
@only_if_tracing
451
- def inject_active_span_twisted_headers (headers , destination ):
487
+ def inject_active_span_twisted_headers (headers , destination , check_destination = True ):
452
488
"""
453
489
Injects a span context into twisted headers in-place
454
490
@@ -467,7 +503,7 @@ def inject_active_span_twisted_headers(headers, destination):
467
503
https://github.com/jaegertracing/jaeger-client-python/blob/master/jaeger_client/constants.py
468
504
"""
469
505
470
- if not whitelisted_homeserver (destination ):
506
+ if check_destination and not whitelisted_homeserver (destination ):
471
507
return
472
508
473
509
span = opentracing .tracer .active_span
@@ -479,7 +515,7 @@ def inject_active_span_twisted_headers(headers, destination):
479
515
480
516
481
517
@only_if_tracing
482
- def inject_active_span_byte_dict (headers , destination ):
518
+ def inject_active_span_byte_dict (headers , destination , check_destination = True ):
483
519
"""
484
520
Injects a span context into a dict where the headers are encoded as byte
485
521
strings
@@ -511,7 +547,7 @@ def inject_active_span_byte_dict(headers, destination):
511
547
512
548
513
549
@only_if_tracing
514
- def inject_active_span_text_map (carrier , destination = None ):
550
+ def inject_active_span_text_map (carrier , destination , check_destination = True ):
515
551
"""
516
552
Injects a span context into a dict
517
553
@@ -532,7 +568,7 @@ def inject_active_span_text_map(carrier, destination=None):
532
568
https://github.com/jaegertracing/jaeger-client-python/blob/master/jaeger_client/constants.py
533
569
"""
534
570
535
- if destination and not whitelisted_homeserver (destination ):
571
+ if check_destination and not whitelisted_homeserver (destination ):
536
572
return
537
573
538
574
opentracing .tracer .inject (
@@ -689,65 +725,43 @@ def _tag_args_inner(self, *args, **kwargs):
689
725
return _tag_args_inner
690
726
691
727
692
- def trace_servlet (servlet_name , func ):
728
+ def trace_servlet (servlet_name , extract_context = False ):
693
729
"""Decorator which traces a serlet. It starts a span with some servlet specific
694
- tags such as the servlet_name and request information"""
695
- if not opentracing :
696
- return func
730
+ tags such as the servlet_name and request information
697
731
698
- @wraps (func )
699
- @defer .inlineCallbacks
700
- def _trace_servlet_inner (request , * args , ** kwargs ):
701
- with start_active_span (
702
- "incoming-client-request" ,
703
- tags = {
732
+ Args:
733
+ servlet_name (str): The name to be used for the span's operation_name
734
+ extract_context (bool): Whether to attempt to extract the opentracing
735
+ context from the request the servlet is handling.
736
+
737
+ """
738
+
739
+ def _trace_servlet_inner_1 (func ):
740
+ if not opentracing :
741
+ return func
742
+
743
+ @wraps (func )
744
+ @defer .inlineCallbacks
745
+ def _trace_servlet_inner (request , * args , ** kwargs ):
746
+ request_tags = {
704
747
"request_id" : request .get_request_id (),
705
748
tags .SPAN_KIND : tags .SPAN_KIND_RPC_SERVER ,
706
749
tags .HTTP_METHOD : request .get_method (),
707
750
tags .HTTP_URL : request .get_redacted_uri (),
708
751
tags .PEER_HOST_IPV6 : request .getClientIP (),
709
- "servlet_name" : servlet_name ,
710
- },
711
- ):
712
- result = yield defer .maybeDeferred (func , request , * args , ** kwargs )
713
- return result
714
-
715
- return _trace_servlet_inner
716
-
717
-
718
- # Helper class
719
-
752
+ }
720
753
721
- class _DummyTagNames (object ):
722
- """wrapper of opentracings tags. We need to have them if we
723
- want to reference them without opentracing around. Clearly they
724
- should never actually show up in a trace. `set_tags` overwrites
725
- these with the correct ones."""
754
+ if extract_context :
755
+ scope = start_active_span_from_request (
756
+ request , servlet_name , tags = request_tags
757
+ )
758
+ else :
759
+ scope = start_active_span (servlet_name , tags = request_tags )
726
760
727
- INVALID_TAG = "invalid-tag"
728
- COMPONENT = INVALID_TAG
729
- DATABASE_INSTANCE = INVALID_TAG
730
- DATABASE_STATEMENT = INVALID_TAG
731
- DATABASE_TYPE = INVALID_TAG
732
- DATABASE_USER = INVALID_TAG
733
- ERROR = INVALID_TAG
734
- HTTP_METHOD = INVALID_TAG
735
- HTTP_STATUS_CODE = INVALID_TAG
736
- HTTP_URL = INVALID_TAG
737
- MESSAGE_BUS_DESTINATION = INVALID_TAG
738
- PEER_ADDRESS = INVALID_TAG
739
- PEER_HOSTNAME = INVALID_TAG
740
- PEER_HOST_IPV4 = INVALID_TAG
741
- PEER_HOST_IPV6 = INVALID_TAG
742
- PEER_PORT = INVALID_TAG
743
- PEER_SERVICE = INVALID_TAG
744
- SAMPLING_PRIORITY = INVALID_TAG
745
- SERVICE = INVALID_TAG
746
- SPAN_KIND = INVALID_TAG
747
- SPAN_KIND_CONSUMER = INVALID_TAG
748
- SPAN_KIND_PRODUCER = INVALID_TAG
749
- SPAN_KIND_RPC_CLIENT = INVALID_TAG
750
- SPAN_KIND_RPC_SERVER = INVALID_TAG
761
+ with scope :
762
+ result = yield defer .maybeDeferred (func , request , * args , ** kwargs )
763
+ return result
751
764
765
+ return _trace_servlet_inner
752
766
753
- tags = _DummyTagNames
767
+ return _trace_servlet_inner_1
0 commit comments