Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update datadog exporter error tagging #459

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#415](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/415))
- `opentelemetry-instrumentation-tornado` Add request/response hooks.
([#426](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/426))
- `opentelemetry-exporter-datadog` Add parsing exception events for error tags.
([#459](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/459))
- `opentelemetry-instrumenation-django` now supports trace response headers.
([#436](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/436))
- `opentelemetry-instrumenation-tornado` now supports trace response headers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@
ENV_KEY = "env"
VERSION_KEY = "version"
SERVICE_NAME_TAG = "service.name"
EVENT_NAME_EXCEPTION = "exception"
EXCEPTION_TYPE_ATTR_KEY = "exception.type"
EXCEPTION_MSG_ATTR_KEY = "exception.message"
EXCEPTION_STACK_ATTR_KEY = "exception.stacktrace"
DD_ERROR_TYPE_TAG_KEY = "error.type"
DD_ERROR_MSG_TAG_KEY = "error.msg"
DD_ERROR_STACK_TAG_KEY = "error.stack"
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@

import opentelemetry.trace as trace_api
from opentelemetry.exporter.datadog.constants import (
DD_ERROR_MSG_TAG_KEY,
DD_ERROR_STACK_TAG_KEY,
DD_ERROR_TYPE_TAG_KEY,
DD_ORIGIN,
ENV_KEY,
EVENT_NAME_EXCEPTION,
EXCEPTION_MSG_ATTR_KEY,
EXCEPTION_STACK_ATTR_KEY,
EXCEPTION_TYPE_ATTR_KEY,
SAMPLE_RATE_METRIC_KEY,
SERVICE_NAME_TAG,
VERSION_KEY,
Expand Down Expand Up @@ -145,11 +152,12 @@ def _translate_to_datadog(self, spans):

if not span.status.is_ok:
datadog_span.error = 1
if span.status.description:
exc_type, exc_val = _get_exc_info(span)
# no mapping for error.stack since traceback not recorded
datadog_span.set_tag("error.msg", exc_val)
datadog_span.set_tag("error.type", exc_type)
# loop over events and look for exception events, extract info.
# https://github.com/open-telemetry/opentelemetry-python/blob/71e3a7a192c0fc8a7503fac967ada36a74b79e58/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py#L810-L819
if span.events:
_extract_tags_from_exception_events(
span.events, datadog_span
)

# combine resource attributes and span attributes, don't modify existing span attributes
combined_span_tags = {}
Expand Down Expand Up @@ -178,7 +186,7 @@ def _translate_to_datadog(self, spans):
if sampling_rate is not None:
datadog_span.set_metric(SAMPLE_RATE_METRIC_KEY, sampling_rate)

# span events and span links are not supported
# span events and span links are not supported except for extracting exception event context

datadog_spans.append(datadog_span)

Expand Down Expand Up @@ -318,3 +326,17 @@ def _extract_tags_from_resource(resource):
else:
tags[attribute_key] = attribute_value
return [tags, service_name]


def _extract_tags_from_exception_events(events, datadog_span):
"""Parse error tags from exception events, error.msg error.type
and error.stack have special significance within datadog"""
for event in events:
if event.name is not None and event.name == EVENT_NAME_EXCEPTION:
for key, value in event.attributes.items():
if key == EXCEPTION_TYPE_ATTR_KEY:
datadog_span.set_tag(DD_ERROR_TYPE_TAG_KEY, value)
elif key == EXCEPTION_MSG_ATTR_KEY:
datadog_span.set_tag(DD_ERROR_MSG_TAG_KEY, value)
elif key == EXCEPTION_STACK_ATTR_KEY:
datadog_span.set_tag(DD_ERROR_STACK_TAG_KEY, value)
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ def test_errors(self):
self.assertEqual(span["error"], 1)
self.assertEqual(span["meta"]["error.msg"], "bar")
self.assertEqual(span["meta"]["error.type"], "ValueError")
self.assertTrue(span["meta"]["error.stack"] is not None)

def test_shutdown(self):
span_names = ["xxx", "bar", "foo"]
Expand Down