|
44 | 44 | from typing import Any, Dict, List, Optional, Sequence, Tuple
|
45 | 45 |
|
46 | 46 | import google.auth
|
| 47 | +import pkg_resources |
47 | 48 | from google.cloud.trace_v2 import TraceServiceClient
|
48 | 49 | from google.cloud.trace_v2.proto.trace_pb2 import AttributeValue
|
49 | 50 | from google.cloud.trace_v2.proto.trace_pb2 import Span as ProtoSpan
|
50 | 51 | from google.cloud.trace_v2.proto.trace_pb2 import TruncatableString
|
51 | 52 | from google.rpc.status_pb2 import Status
|
52 | 53 |
|
53 | 54 | import opentelemetry.trace as trace_api
|
| 55 | +from opentelemetry.exporter.cloud_trace.version import ( |
| 56 | + __version__ as cloud_trace_version, |
| 57 | +) |
54 | 58 | from opentelemetry.sdk.trace import Event
|
55 | 59 | from opentelemetry.sdk.trace.export import Span, SpanExporter, SpanExportResult
|
56 | 60 | from opentelemetry.sdk.util import BoundedDict
|
@@ -157,7 +161,7 @@ def _translate_to_cloud_trace(
|
157 | 161 | "end_time": end_time,
|
158 | 162 | "parent_span_id": parent_id,
|
159 | 163 | "attributes": _extract_attributes(
|
160 |
| - span.attributes, MAX_SPAN_ATTRS |
| 164 | + span.attributes, MAX_SPAN_ATTRS, add_agent_attr=True |
161 | 165 | ),
|
162 | 166 | "links": _extract_links(span.links),
|
163 | 167 | "status": _extract_status(span.status),
|
@@ -288,20 +292,32 @@ def _extract_events(events: Sequence[Event]) -> ProtoSpan.TimeEvents:
|
288 | 292 |
|
289 | 293 |
|
290 | 294 | def _extract_attributes(
|
291 |
| - attrs: types.Attributes, num_attrs_limit: int |
| 295 | + attrs: types.Attributes, |
| 296 | + num_attrs_limit: int, |
| 297 | + add_agent_attr: bool = False, |
292 | 298 | ) -> ProtoSpan.Attributes:
|
293 | 299 | """Convert span.attributes to dict."""
|
294 | 300 | attributes_dict = BoundedDict(num_attrs_limit)
|
295 |
| - |
| 301 | + invalid_value_dropped_count = 0 |
296 | 302 | for key, value in attrs.items():
|
297 | 303 | key = _truncate_str(key, 128)[0]
|
298 | 304 | value = _format_attribute_value(value)
|
299 | 305 |
|
300 |
| - if value is not None: |
| 306 | + if value: |
301 | 307 | attributes_dict[key] = value
|
| 308 | + else: |
| 309 | + invalid_value_dropped_count += 1 |
| 310 | + if add_agent_attr: |
| 311 | + attributes_dict["g.co/agent"] = _format_attribute_value( |
| 312 | + "opentelemetry-python {}; google-cloud-trace-exporter {}".format( |
| 313 | + pkg_resources.get_distribution("opentelemetry-sdk").version, |
| 314 | + cloud_trace_version, |
| 315 | + ) |
| 316 | + ) |
302 | 317 | return ProtoSpan.Attributes(
|
303 | 318 | attribute_map=attributes_dict,
|
304 |
| - dropped_attributes_count=len(attrs) - len(attributes_dict), |
| 319 | + dropped_attributes_count=attributes_dict.dropped |
| 320 | + + invalid_value_dropped_count, |
305 | 321 | )
|
306 | 322 |
|
307 | 323 |
|
|
0 commit comments