2
2
# (c) Copyright Instana Inc. 2021
3
3
4
4
5
- import json
5
+ from typing import TYPE_CHECKING , Any , Callable , Dict , Tuple
6
+
6
7
import wrapt
7
- from opentracing import Format
8
8
9
- from ....log import logger
10
- from ....singletons import tracer
11
- from ....util .traceutils import get_tracer_tuple , tracing_is_off
9
+ from instana .log import logger
10
+ from instana .propagators .format import Format
11
+ from instana .singletons import tracer
12
+ from instana .util .traceutils import get_tracer_tuple , tracing_is_off
13
+
14
+ if TYPE_CHECKING :
15
+ from instana .span .span import InstanaSpan
12
16
13
17
try :
14
18
from google .cloud import pubsub_v1
15
19
16
-
17
- def _set_publisher_tags (span , topic_path ):
18
- span .set_tag ('gcps.op' , 'publish' )
20
+ def _set_publisher_attributes (
21
+ span : "InstanaSpan" ,
22
+ topic_path : str ,
23
+ ) -> None :
24
+ span .set_attribute ("gcps.op" , "publish" )
19
25
# Fully qualified identifier is in the form of
20
26
# `projects/{project_id}/topic/{topic_name}`
21
- project_id , topic_name = topic_path .split ('/' )[1 ::2 ]
22
- span .set_tag ('gcps.projid' , project_id )
23
- span .set_tag ('gcps.top' , topic_name )
24
-
25
-
26
- def _set_consumer_tags (span , subscription_path ):
27
- span .set_tag ('gcps.op' , 'consume' )
27
+ project_id , topic_name = topic_path .split ("/" )[1 ::2 ]
28
+ span .set_attribute ("gcps.projid" , project_id )
29
+ span .set_attribute ("gcps.top" , topic_name )
30
+
31
+ def _set_consumer_attributes (
32
+ span : "InstanaSpan" ,
33
+ subscription_path : str ,
34
+ ) -> None :
35
+ span .set_attribute ("gcps.op" , "consume" )
28
36
# Fully qualified identifier is in the form of
29
37
# `projects/{project_id}/subscriptions/{subscription_name}`
30
- project_id , subscription_id = subscription_path .split ('/' )[1 ::2 ]
31
- span .set_tag ('gcps.projid' , project_id )
32
- span .set_tag ('gcps.sub' , subscription_id )
33
-
34
-
35
- @wrapt .patch_function_wrapper ('google.cloud.pubsub_v1' , 'PublisherClient.publish' )
36
- def publish_with_instana (wrapped , instance , args , kwargs ):
38
+ project_id , subscription_id = subscription_path .split ("/" )[1 ::2 ]
39
+ span .set_attribute ("gcps.projid" , project_id )
40
+ span .set_attribute ("gcps.sub" , subscription_id )
41
+
42
+ @wrapt .patch_function_wrapper ("google.cloud.pubsub_v1" , "PublisherClient.publish" )
43
+ def publish_with_instana (
44
+ wrapped : Callable [..., object ],
45
+ instance : pubsub_v1 .PublisherClient ,
46
+ args : Tuple [object , ...],
47
+ kwargs : Dict [str , Any ],
48
+ ) -> object :
37
49
"""References:
38
50
- PublisherClient.publish(topic_path, messages, metadata)
39
51
"""
@@ -42,59 +54,75 @@ def publish_with_instana(wrapped, instance, args, kwargs):
42
54
return wrapped (* args , ** kwargs )
43
55
44
56
tracer , parent_span , _ = get_tracer_tuple ()
57
+ parent_context = parent_span .get_span_context () if parent_span else None
45
58
46
- with tracer .start_active_span ('gcps-producer' , child_of = parent_span ) as scope :
59
+ with tracer .start_as_current_span (
60
+ "gcps-producer" , span_context = parent_context
61
+ ) as span :
47
62
# trace continuity, inject to the span context
48
- headers = dict ()
49
- tracer .inject (scope .span .context , Format .TEXT_MAP , headers , disable_w3c_trace_context = True )
63
+ headers = {}
64
+ tracer .inject (
65
+ span .context ,
66
+ Format .TEXT_MAP ,
67
+ headers ,
68
+ disable_w3c_trace_context = True ,
69
+ )
70
+
71
+ headers = {key : str (value ) for key , value in headers .items ()}
50
72
51
73
# update the metadata dict with instana trace attributes
52
74
kwargs .update (headers )
53
75
54
- _set_publisher_tags ( scope . span , topic_path = args [0 ])
76
+ _set_publisher_attributes ( span , topic_path = args [0 ])
55
77
56
78
try :
57
79
rv = wrapped (* args , ** kwargs )
58
- except Exception as e :
59
- scope .span .log_exception (e )
60
- raise
80
+ except Exception as exc :
81
+ span .record_exception (exc )
61
82
else :
62
83
return rv
63
84
64
-
65
- @wrapt .patch_function_wrapper ('google.cloud.pubsub_v1' , 'SubscriberClient.subscribe' )
66
- def subscribe_with_instana (wrapped , instance , args , kwargs ):
67
-
85
+ @wrapt .patch_function_wrapper (
86
+ "google.cloud.pubsub_v1" , "SubscriberClient.subscribe"
87
+ )
88
+ def subscribe_with_instana (
89
+ wrapped : Callable [..., object ],
90
+ instance : pubsub_v1 .SubscriberClient ,
91
+ args : Tuple [object , ...],
92
+ kwargs : Dict [str , Any ],
93
+ ) -> object :
68
94
"""References:
69
95
- SubscriberClient.subscribe(subscription_path, callback)
70
96
- callback(message) is called from the subscription future
71
97
"""
72
98
73
99
def callback_with_instana (message ):
74
100
if message .attributes :
75
- parent_span = tracer .extract (Format .TEXT_MAP , message .attributes , disable_w3c_trace_context = True )
101
+ parent_context = tracer .extract (
102
+ Format .TEXT_MAP , message .attributes , disable_w3c_trace_context = True
103
+ )
76
104
else :
77
- parent_span = None
105
+ parent_context = None
78
106
79
- with tracer .start_active_span ('gcps-consumer' , child_of = parent_span ) as scope :
80
- _set_consumer_tags (scope .span , subscription_path = args [0 ])
107
+ with tracer .start_as_current_span (
108
+ "gcps-consumer" , span_context = parent_context
109
+ ) as span :
110
+ _set_consumer_attributes (span , subscription_path = args [0 ])
81
111
try :
82
112
callback (message )
83
- except Exception as e :
84
- scope .span .log_exception (e )
85
- raise
113
+ except Exception as exc :
114
+ span .record_exception (exc )
86
115
87
116
# Handle callback appropriately from args or kwargs
88
- if ' callback' in kwargs :
89
- callback = kwargs .get (' callback' )
90
- kwargs [' callback' ] = callback_with_instana
117
+ if " callback" in kwargs :
118
+ callback = kwargs .get (" callback" )
119
+ kwargs [" callback" ] = callback_with_instana
91
120
return wrapped (* args , ** kwargs )
92
121
else :
93
122
subscription , callback , * args = args
94
123
args = (subscription , callback_with_instana , * args )
95
124
return wrapped (* args , ** kwargs )
96
125
97
-
98
- logger .debug ('Instrumenting Google Cloud Pub/Sub' )
126
+ logger .debug ("Instrumenting Google Cloud Pub/Sub" )
99
127
except ImportError :
100
128
pass
0 commit comments