Skip to content

Commit cca8d63

Browse files
author
Andrew Xue
committed
add gco agent span label (open-telemetry#833)
add uuid label to metricdescriptor
1 parent 046fbac commit cca8d63

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

ext/opentelemetry-exporter-cloud-monitoring/src/opentelemetry/exporter/cloud_monitoring/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from google.api.metric_pb2 import MetricDescriptor
88
from google.cloud.monitoring_v3 import MetricServiceClient
99
from google.cloud.monitoring_v3.proto.metric_pb2 import TimeSeries
10+
1011
from opentelemetry.sdk.metrics.export import (
1112
MetricRecord,
1213
MetricsExporter,
@@ -113,6 +114,12 @@ def _get_metric_descriptor(
113114
logger.warning(
114115
"Label value %s is not a string, bool or integer", value
115116
)
117+
118+
if self.unique_identifier:
119+
descriptor["labels"].append(
120+
LabelDescriptor(key="opentelemetry_uuid", value_type="STRING")
121+
)
122+
116123
if isinstance(record.aggregator, SumAggregator):
117124
descriptor["metric_kind"] = MetricDescriptor.MetricKind.GAUGE
118125
else:

ext/opentelemetry-exporter-cloud-monitoring/tests/test_cloud_monitoring.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ def test_unique_identifier(self):
311311
"type": "custom.googleapis.com/OpenTelemetry/name",
312312
"display_name": "name",
313313
"description": "description",
314-
"labels": [],
314+
"labels": [
315+
LabelDescriptor(
316+
key="opentelemetry_uuid", value_type="STRING"
317+
),
318+
],
315319
"metric_kind": "GAUGE",
316320
"value_type": "DOUBLE",
317321
}
@@ -323,6 +327,13 @@ def test_unique_identifier(self):
323327
exporter1.export([metric_record])
324328
exporter2.export([metric_record])
325329

330+
(
331+
first_call,
332+
second_call,
333+
) = client.create_metric_descriptor.call_args_list
334+
self.assertEqual(first_call[0][1].labels[0].key, "opentelemetry_uuid")
335+
self.assertEqual(second_call[0][1].labels[0].key, "opentelemetry_uuid")
336+
326337
first_call, second_call = client.create_time_series.call_args_list
327338
self.assertNotEqual(
328339
first_call[0][1][0].metric.labels["opentelemetry_uuid"],

ext/opentelemetry-exporter-cloud-trace/src/opentelemetry/exporter/cloud_trace/__init__.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@
4444
from typing import Any, Dict, List, Optional, Sequence, Tuple
4545

4646
import google.auth
47+
import pkg_resources
4748
from google.cloud.trace_v2 import TraceServiceClient
4849
from google.cloud.trace_v2.proto.trace_pb2 import AttributeValue
4950
from google.cloud.trace_v2.proto.trace_pb2 import Span as ProtoSpan
5051
from google.cloud.trace_v2.proto.trace_pb2 import TruncatableString
5152
from google.rpc.status_pb2 import Status
5253

5354
import opentelemetry.trace as trace_api
55+
from opentelemetry.exporter.cloud_trace.version import (
56+
__version__ as cloud_trace_version,
57+
)
5458
from opentelemetry.sdk.trace import Event
5559
from opentelemetry.sdk.trace.export import Span, SpanExporter, SpanExportResult
5660
from opentelemetry.sdk.util import BoundedDict
@@ -157,7 +161,7 @@ def _translate_to_cloud_trace(
157161
"end_time": end_time,
158162
"parent_span_id": parent_id,
159163
"attributes": _extract_attributes(
160-
span.attributes, MAX_SPAN_ATTRS
164+
span.attributes, MAX_SPAN_ATTRS, add_agent_attr=True
161165
),
162166
"links": _extract_links(span.links),
163167
"status": _extract_status(span.status),
@@ -288,20 +292,32 @@ def _extract_events(events: Sequence[Event]) -> ProtoSpan.TimeEvents:
288292

289293

290294
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,
292298
) -> ProtoSpan.Attributes:
293299
"""Convert span.attributes to dict."""
294300
attributes_dict = BoundedDict(num_attrs_limit)
295-
301+
invalid_value_dropped_count = 0
296302
for key, value in attrs.items():
297303
key = _truncate_str(key, 128)[0]
298304
value = _format_attribute_value(value)
299305

300-
if value is not None:
306+
if value:
301307
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+
)
302317
return ProtoSpan.Attributes(
303318
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,
305321
)
306322

307323

ext/opentelemetry-exporter-cloud-trace/tests/test_cloud_trace_exporter.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616
from unittest import mock
1717

18+
import pkg_resources
1819
from google.cloud.trace_v2.proto.trace_pb2 import AttributeValue
1920
from google.cloud.trace_v2.proto.trace_pb2 import Span as ProtoSpan
2021
from google.cloud.trace_v2.proto.trace_pb2 import TruncatableString
@@ -33,7 +34,11 @@
3334
_format_attribute_value,
3435
_truncate_str,
3536
)
36-
from opentelemetry.sdk.trace import Event, Span
37+
from opentelemetry.exporter.cloud_trace.version import (
38+
__version__ as cloud_trace_version,
39+
)
40+
from opentelemetry.sdk.trace import Event
41+
from opentelemetry.sdk.trace.export import Span
3742
from opentelemetry.trace import Link, SpanContext, SpanKind
3843
from opentelemetry.trace.status import Status as SpanStatus
3944
from opentelemetry.trace.status import StatusCanonicalCode
@@ -108,7 +113,18 @@ def test_export(self):
108113
"display_name": TruncatableString(
109114
value="span_name", truncated_byte_count=0
110115
),
111-
"attributes": ProtoSpan.Attributes(attribute_map={}),
116+
"attributes": ProtoSpan.Attributes(
117+
attribute_map={
118+
"g.co/agent": _format_attribute_value(
119+
"opentelemetry-python {}; google-cloud-trace-exporter {}".format(
120+
pkg_resources.get_distribution(
121+
"opentelemetry-sdk"
122+
).version,
123+
cloud_trace_version,
124+
)
125+
)
126+
}
127+
),
112128
"links": None,
113129
"status": None,
114130
"time_events": None,

0 commit comments

Comments
 (0)