1414
1515import sys
1616from datetime import datetime
17- import warnings
1817from typing import Optional
1918
2019from opentelemetry import trace
2120from opentelemetry .trace .propagation import set_span_in_context
2221from opentelemetry .trace .propagation .tracecontext import TraceContextTextMapPropagator
22+ from opentelemetry .semconv .trace import SpanAttributes
23+ from opentelemetry .semconv ._incubating .attributes .messaging_attributes import (
24+ MESSAGING_GCP_PUBSUB_MESSAGE_ORDERING_KEY ,
25+ MESSAGING_MESSAGE_BODY_SIZE ,
26+ )
2327
2428from google .pubsub_v1 import types as gapic_types
2529from google .cloud .pubsub_v1 .open_telemetry .context_propagation import (
2832
2933
3034class PublishMessageWrapper :
31- _OPEN_TELEMETRY_TRACER_NAME : str = "google.cloud.pubsub_v1.publisher "
35+ _OPEN_TELEMETRY_TRACER_NAME : str = "google.cloud.pubsub_v1"
3236 _OPEN_TELEMETRY_MESSAGING_SYSTEM : str = "gcp_pubsub"
3337 _OPEN_TELEMETRY_PUBLISHER_BATCHING = "publisher batching"
3438
@@ -58,17 +62,20 @@ def __eq__(self, other): # pragma: NO COVER
5862
5963 def start_create_span (self , topic : str , ordering_key : str ) -> None :
6064 tracer = trace .get_tracer (self ._OPEN_TELEMETRY_TRACER_NAME )
65+ assert len (topic .split ("/" )) == 4
6166 topic_short_name = topic .split ("/" )[3 ]
6267 with tracer .start_as_current_span (
6368 name = f"{ topic_short_name } create" ,
6469 attributes = {
65- "messaging.system" : self ._OPEN_TELEMETRY_MESSAGING_SYSTEM ,
66- "messaging.destination.name" : topic_short_name ,
67- "code.function" : "publish" ,
68- "messaging.gcp_pubsub.message.ordering_key" : ordering_key ,
69- "messaging.operation" : "create" ,
70+ SpanAttributes . MESSAGING_SYSTEM : self ._OPEN_TELEMETRY_MESSAGING_SYSTEM ,
71+ SpanAttributes . MESSAGING_DESTINATION_NAME : topic_short_name ,
72+ SpanAttributes . CODE_FUNCTION : "publish" ,
73+ MESSAGING_GCP_PUBSUB_MESSAGE_ORDERING_KEY : ordering_key ,
74+ SpanAttributes . MESSAGING_OPERATION : "create" ,
7075 "gcp.project_id" : topic .split ("/" )[1 ],
71- "messaging.message.body.size" : sys .getsizeof (self ._message .data ),
76+ MESSAGING_MESSAGE_BODY_SIZE : sys .getsizeof (
77+ self ._message .data
78+ ), # sys.getsizeof() used since the attribute expects size of message body in bytes
7279 },
7380 kind = trace .SpanKind .PRODUCER ,
7481 end_on_exit = False ,
@@ -86,12 +93,7 @@ def start_create_span(self, topic: str, ordering_key: str) -> None:
8693 )
8794
8895 def end_create_span (self , exc : Optional [BaseException ] = None ) -> None :
89- if self ._create_span is None : # pragma: NO COVER
90- warnings .warn (
91- message = "publish create span is None. Hence, not ending it" ,
92- category = RuntimeWarning ,
93- )
94- return
96+ assert self ._create_span is not None
9597 if exc :
9698 self ._create_span .record_exception (exception = exc )
9799 self ._create_span .set_status (
@@ -101,12 +103,7 @@ def end_create_span(self, exc: Optional[BaseException] = None) -> None:
101103
102104 def start_publisher_flow_control_span (self ) -> None :
103105 tracer = trace .get_tracer (self ._OPEN_TELEMETRY_TRACER_NAME )
104- if self ._create_span is None : # pragma: NO COVER
105- warnings .warn (
106- message = "publish create span is None. Hence, not starting publish flow control span" ,
107- category = RuntimeWarning ,
108- )
109- return
106+ assert self ._create_span is not None
110107 with tracer .start_as_current_span (
111108 name = self ._PUBLISH_FLOW_CONTROL ,
112109 kind = trace .SpanKind .INTERNAL ,
@@ -118,12 +115,7 @@ def start_publisher_flow_control_span(self) -> None:
118115 def end_publisher_flow_control_span (
119116 self , exc : Optional [BaseException ] = None
120117 ) -> None :
121- if self ._flow_control_span is None : # pragma: NO COVER
122- warnings .warn (
123- message = "publish flow control span is None. Hence, not ending it" ,
124- category = RuntimeWarning ,
125- )
126- return
118+ assert self ._flow_control_span is not None
127119 if exc :
128120 self ._flow_control_span .record_exception (exception = exc )
129121 self ._flow_control_span .set_status (
@@ -132,12 +124,7 @@ def end_publisher_flow_control_span(
132124 self ._flow_control_span .end ()
133125
134126 def start_publisher_batching_span (self ) -> None :
135- if self ._create_span is None : # pragma: NO COVER
136- warnings .warn (
137- message = "publish create span is None. Hence, not starting publisher batching span" ,
138- category = RuntimeWarning ,
139- )
140- return
127+ assert self ._create_span is not None
141128 tracer = trace .get_tracer (self ._OPEN_TELEMETRY_TRACER_NAME )
142129 with tracer .start_as_current_span (
143130 name = self ._OPEN_TELEMETRY_PUBLISHER_BATCHING ,
@@ -148,12 +135,7 @@ def start_publisher_batching_span(self) -> None:
148135 self ._batching_span = batching_span
149136
150137 def end_publisher_batching_span (self , exc : Optional [BaseException ] = None ) -> None :
151- if self ._batching_span is None : # pragma: NO COVER
152- warnings .warn (
153- message = "publisher batching span is None. Hence, not ending it" ,
154- category = RuntimeWarning ,
155- )
156- return
138+ assert self ._batching_span is not None
157139 if exc :
158140 self ._batching_span .record_exception (exception = exc )
159141 self ._batching_span .set_status (
0 commit comments