5656import opentelemetry .trace as trace_api
5757import pkg_resources
5858from google .cloud .trace_v2 import TraceServiceClient
59- from google .cloud .trace_v2 .proto .trace_pb2 import AttributeValue
60- from google .cloud .trace_v2 .proto .trace_pb2 import Span as ProtoSpan
61- from google .cloud .trace_v2 .proto .trace_pb2 import TruncatableString
59+ from google .cloud .trace_v2 .proto import trace_pb2
6260from google .protobuf .timestamp_pb2 import Timestamp
63- from google .rpc . status_pb2 import Status
61+ from google .rpc import code_pb2 , status_pb2
6462from opentelemetry .exporter .google .version import (
6563 __version__ as google_ext_version ,
6664)
7270 get_hexadecimal_span_id ,
7371 get_hexadecimal_trace_id ,
7472)
73+ from opentelemetry .trace .status import StatusCode
7574from opentelemetry .util import types
7675
7776logger = logging .getLogger (__name__ )
@@ -204,7 +203,7 @@ def _get_truncatable_str_object(str_to_convert: str, max_length: int):
204203 truncated bytes count."""
205204 truncated , truncated_byte_count = _truncate_str (str_to_convert , max_length )
206205
207- return TruncatableString (
206+ return trace_pb2 . TruncatableString (
208207 value = truncated , truncated_byte_count = truncated_byte_count
209208 )
210209
@@ -216,22 +215,30 @@ def _truncate_str(str_to_check: str, limit: int) -> Tuple[str, int]:
216215 return truncated_str , len (encoded ) - len (truncated_str .encode ("utf-8" ))
217216
218217
219- def _extract_status (status : trace_api .Status ) -> Optional [Status ]:
220- """Convert a Status object to protobuf object."""
221- if not status :
222- return None
223- status_dict = {
224- "details" : None ,
225- "code" : status .status_code .value ,
226- } # type: Dict[str, Any]
227-
228- if status .description is not None :
229- status_dict ["message" ] = status .description
218+ def _extract_status (status : trace_api .Status ) -> Optional [status_pb2 .Status ]:
219+ """Convert a OTel Status to protobuf Status."""
220+ if status .status_code is StatusCode .UNSET :
221+ status_proto = None
222+ elif status .status_code is StatusCode .OK :
223+ status_proto = status_pb2 .Status (code = code_pb2 .OK )
224+ elif status .status_code is StatusCode .ERROR :
225+ status_proto = status_pb2 .Status (
226+ code = code_pb2 .UNKNOWN , message = status .description
227+ )
228+ # future added value
229+ else :
230+ logger .info (
231+ "Couldn't handle OTel status code %s, assuming error" ,
232+ status .status_code ,
233+ )
234+ status_proto = status_pb2 .Status (
235+ code = code_pb2 .UNKNOWN , message = status .description
236+ )
230237
231- return Status ( ** status_dict )
238+ return status_proto
232239
233240
234- def _extract_links (links : Sequence [trace_api .Link ]) -> ProtoSpan .Links :
241+ def _extract_links (links : Sequence [trace_api .Link ]) -> trace_pb2 . Span .Links :
235242 """Convert span.links"""
236243 if not links :
237244 return None
@@ -263,12 +270,12 @@ def _extract_links(links: Sequence[trace_api.Link]) -> ProtoSpan.Links:
263270 ),
264271 }
265272 )
266- return ProtoSpan .Links (
273+ return trace_pb2 . Span .Links (
267274 link = extracted_links , dropped_links_count = dropped_links
268275 )
269276
270277
271- def _extract_events (events : Sequence [Event ]) -> ProtoSpan .TimeEvents :
278+ def _extract_events (events : Sequence [Event ]) -> trace_pb2 . Span .TimeEvents :
272279 """Convert span.events to dict."""
273280 if not events :
274281 return None
@@ -301,7 +308,7 @@ def _extract_events(events: Sequence[Event]) -> ProtoSpan.TimeEvents:
301308 },
302309 }
303310 )
304- return ProtoSpan .TimeEvents (
311+ return trace_pb2 . Span .TimeEvents (
305312 time_event = logs ,
306313 dropped_annotations_count = dropped_annontations ,
307314 dropped_message_events_count = 0 ,
@@ -310,18 +317,20 @@ def _extract_events(events: Sequence[Event]) -> ProtoSpan.TimeEvents:
310317
311318# pylint: disable=no-member
312319SPAN_KIND_MAPPING = {
313- trace_api .SpanKind .INTERNAL : ProtoSpan .SpanKind .INTERNAL ,
314- trace_api .SpanKind .CLIENT : ProtoSpan .SpanKind .CLIENT ,
315- trace_api .SpanKind .SERVER : ProtoSpan .SpanKind .SERVER ,
316- trace_api .SpanKind .PRODUCER : ProtoSpan .SpanKind .PRODUCER ,
317- trace_api .SpanKind .CONSUMER : ProtoSpan .SpanKind .CONSUMER ,
320+ trace_api .SpanKind .INTERNAL : trace_pb2 . Span .SpanKind .INTERNAL ,
321+ trace_api .SpanKind .CLIENT : trace_pb2 . Span .SpanKind .CLIENT ,
322+ trace_api .SpanKind .SERVER : trace_pb2 . Span .SpanKind .SERVER ,
323+ trace_api .SpanKind .PRODUCER : trace_pb2 . Span .SpanKind .PRODUCER ,
324+ trace_api .SpanKind .CONSUMER : trace_pb2 . Span .SpanKind .CONSUMER ,
318325}
319326
320327
321328# pylint: disable=no-member
322- def _extract_span_kind (span_kind : trace_api .SpanKind ) -> ProtoSpan .SpanKind :
329+ def _extract_span_kind (
330+ span_kind : trace_api .SpanKind ,
331+ ) -> trace_pb2 .Span .SpanKind :
323332 return SPAN_KIND_MAPPING .get (
324- span_kind , ProtoSpan .SpanKind .SPAN_KIND_UNSPECIFIED
333+ span_kind , trace_pb2 . Span .SpanKind .SPAN_KIND_UNSPECIFIED
325334 )
326335
327336
@@ -386,11 +395,11 @@ def _extract_attributes(
386395 attrs : types .Attributes ,
387396 num_attrs_limit : int ,
388397 add_agent_attr : bool = False ,
389- ) -> ProtoSpan .Attributes :
398+ ) -> trace_pb2 . Span .Attributes :
390399 """Convert span.attributes to dict."""
391400 attributes_dict = BoundedDict (
392401 num_attrs_limit
393- ) # type: BoundedDict[str, AttributeValue]
402+ ) # type: BoundedDict[str, trace_pb2. AttributeValue]
394403 invalid_value_dropped_count = 0
395404 for key , value in attrs .items () if attrs else []:
396405 key = _truncate_str (key , 128 )[0 ]
@@ -411,14 +420,16 @@ def _extract_attributes(
411420 _strip_characters (google_ext_version ),
412421 )
413422 )
414- return ProtoSpan .Attributes (
423+ return trace_pb2 . Span .Attributes (
415424 attribute_map = attributes_dict ,
416425 dropped_attributes_count = attributes_dict .dropped # type: ignore[attr-defined]
417426 + invalid_value_dropped_count ,
418427 )
419428
420429
421- def _format_attribute_value (value : types .AttributeValue ) -> AttributeValue :
430+ def _format_attribute_value (
431+ value : types .AttributeValue ,
432+ ) -> trace_pb2 .AttributeValue :
422433 if isinstance (value , bool ):
423434 value_type = "bool_value"
424435 elif isinstance (value , int ):
@@ -443,4 +454,4 @@ def _format_attribute_value(value: types.AttributeValue) -> AttributeValue:
443454 )
444455 return None
445456
446- return AttributeValue (** {value_type : value })
457+ return trace_pb2 . AttributeValue (** {value_type : value })
0 commit comments