|
22 | 22 |
|
23 | 23 | import opentelemetry.trace as trace_api
|
24 | 24 | from opentelemetry.exporter.datadog.constants import (
|
| 25 | + DD_ERROR_MSG_TAG_KEY, |
| 26 | + DD_ERROR_STACK_TAG_KEY, |
| 27 | + DD_ERROR_TYPE_TAG_KEY, |
25 | 28 | DD_ORIGIN,
|
26 | 29 | ENV_KEY,
|
| 30 | + EVENT_NAME_EXCEPTION, |
| 31 | + EXCEPTION_MSG_ATTR_KEY, |
| 32 | + EXCEPTION_STACK_ATTR_KEY, |
| 33 | + EXCEPTION_TYPE_ATTR_KEY, |
27 | 34 | SAMPLE_RATE_METRIC_KEY,
|
28 | 35 | SERVICE_NAME_TAG,
|
29 | 36 | VERSION_KEY,
|
@@ -145,11 +152,12 @@ def _translate_to_datadog(self, spans):
|
145 | 152 |
|
146 | 153 | if not span.status.is_ok:
|
147 | 154 | datadog_span.error = 1
|
148 |
| - if span.status.description: |
149 |
| - exc_type, exc_val = _get_exc_info(span) |
150 |
| - # no mapping for error.stack since traceback not recorded |
151 |
| - datadog_span.set_tag("error.msg", exc_val) |
152 |
| - datadog_span.set_tag("error.type", exc_type) |
| 155 | + # loop over events and look for exception events, extract info. |
| 156 | + # https://github.com/open-telemetry/opentelemetry-python/blob/71e3a7a192c0fc8a7503fac967ada36a74b79e58/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py#L810-L819 |
| 157 | + if span.events: |
| 158 | + _extract_tags_from_exception_events( |
| 159 | + span.events, datadog_span |
| 160 | + ) |
153 | 161 |
|
154 | 162 | # combine resource attributes and span attributes, don't modify existing span attributes
|
155 | 163 | combined_span_tags = {}
|
@@ -178,7 +186,7 @@ def _translate_to_datadog(self, spans):
|
178 | 186 | if sampling_rate is not None:
|
179 | 187 | datadog_span.set_metric(SAMPLE_RATE_METRIC_KEY, sampling_rate)
|
180 | 188 |
|
181 |
| - # span events and span links are not supported |
| 189 | + # span events and span links are not supported except for extracting exception event context |
182 | 190 |
|
183 | 191 | datadog_spans.append(datadog_span)
|
184 | 192 |
|
@@ -318,3 +326,17 @@ def _extract_tags_from_resource(resource):
|
318 | 326 | else:
|
319 | 327 | tags[attribute_key] = attribute_value
|
320 | 328 | return [tags, service_name]
|
| 329 | + |
| 330 | + |
| 331 | +def _extract_tags_from_exception_events(events, datadog_span): |
| 332 | + """Parse error tags from exception events, error.msg error.type |
| 333 | + and error.stack have special significance within datadog""" |
| 334 | + for event in events: |
| 335 | + if event.name is not None and event.name == EVENT_NAME_EXCEPTION: |
| 336 | + for key, value in event.attributes.items(): |
| 337 | + if key == EXCEPTION_TYPE_ATTR_KEY: |
| 338 | + datadog_span.set_tag(DD_ERROR_TYPE_TAG_KEY, value) |
| 339 | + elif key == EXCEPTION_MSG_ATTR_KEY: |
| 340 | + datadog_span.set_tag(DD_ERROR_MSG_TAG_KEY, value) |
| 341 | + elif key == EXCEPTION_STACK_ATTR_KEY: |
| 342 | + datadog_span.set_tag(DD_ERROR_STACK_TAG_KEY, value) |
0 commit comments