Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,23 @@
from opentelemetry.sdk.trace import Event
from opentelemetry.sdk.trace.export import Span, SpanExporter, SpanExportResult
from opentelemetry.sdk.util import BoundedDict
from opentelemetry.sdk.version import __version__ as core_version
from opentelemetry.util import types

# pylint: disable=import-error
Comment thread
AndrewAXue marked this conversation as resolved.
Outdated
from .version import __version__ as cloud_trace_version

logger = logging.getLogger(__name__)

MAX_NUM_LINKS = 128
MAX_NUM_EVENTS = 32
MAX_EVENT_ATTRS = 4
MAX_LINK_ATTRS = 32
MAX_SPAN_ATTRS = 32
AGENT_LABEL_KEY = "g.co/agent"
AGENT_LABEL_VALUE = "opentelemetry-python {}; google-cloud-trace-exporter {}".format(
Comment thread
AndrewAXue marked this conversation as resolved.
Outdated
core_version, cloud_trace_version
)


class CloudTraceSpanExporter(SpanExporter):
Expand Down Expand Up @@ -153,7 +161,7 @@ def _translate_to_cloud_trace(
"end_time": end_time,
"parent_span_id": parent_id,
"attributes": _extract_attributes(
span.attributes, MAX_SPAN_ATTRS
span.attributes, MAX_SPAN_ATTRS, span_attributes=True
),
"links": _extract_links(span.links),
"status": _extract_status(span.status),
Expand Down Expand Up @@ -292,7 +300,9 @@ def _extract_events(events: Sequence[Event]) -> ProtoSpan.TimeEvents:


def _extract_attributes(
attrs: types.Attributes, num_attrs_limit: int
attrs: types.Attributes,
num_attrs_limit: int,
span_attributes: bool = False,
Comment thread
AndrewAXue marked this conversation as resolved.
Outdated
) -> ProtoSpan.Attributes:
"""Convert span.attributes to dict."""
attributes_dict = BoundedDict(num_attrs_limit)
Expand All @@ -301,11 +311,18 @@ def _extract_attributes(
key = _truncate_str(key, 128)[0]
value = _format_attribute_value(value)

if value is not None:
if value:
attributes_dict[key] = value
dropped_attributes_count = len(attrs) - len(attributes_dict)
Comment thread
AndrewAXue marked this conversation as resolved.
Outdated
if span_attributes:
if len(attributes_dict) == num_attrs_limit:
dropped_attributes_count += 1
attributes_dict[AGENT_LABEL_KEY] = _format_attribute_value(
AGENT_LABEL_VALUE
)
return ProtoSpan.Attributes(
attribute_map=attributes_dict,
dropped_attributes_count=len(attrs) - len(attributes_dict),
dropped_attributes_count=dropped_attributes_count,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from google.rpc.status_pb2 import Status

from opentelemetry.exporter.cloud_trace import (
AGENT_LABEL_KEY,
AGENT_LABEL_VALUE,
MAX_EVENT_ATTRS,
MAX_LINK_ATTRS,
MAX_NUM_EVENTS,
Expand Down Expand Up @@ -108,7 +110,11 @@ def test_export(self):
"display_name": TruncatableString(
value="span_name", truncated_byte_count=0
),
"attributes": ProtoSpan.Attributes(attribute_map={}),
"attributes": ProtoSpan.Attributes(
attribute_map={
AGENT_LABEL_KEY: _format_attribute_value(AGENT_LABEL_VALUE)
}
),
"links": None,
"status": None,
"time_events": None,
Expand Down