Skip to content

Commit

Permalink
feat: Propagate span errors to the service entry span as well
Browse files Browse the repository at this point in the history
This is required for error tracking to pick them up.

If we already set a a different error on that span, that would likely
happeng later, and overwrite this error, meaning this change only adds
errors where previously we didn't have any.

The main use case for this is the 502s we return for invalid cards,
without throwing an error on the top-level.
  • Loading branch information
sulami committed Dec 9, 2024
1 parent 3b02f42 commit 927556c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ DegicaDatadog::Tracing.span_tags!(**tags)
# Add tags to the current root span.
DegicaDatadog::Tracing.root_span_tags!(**tags)
# Record an exception on the current span. This is only required if the
# exception is caught and swallowed, so we return a 200 or similar, but we
# still want to report the exception to Datadog for tracking purposes.
DegicaDatadog::Tracing.error!(e)
```

## Profiling
Expand Down Expand Up @@ -173,4 +178,3 @@ DD_AGENT_URI=http://datadog_agent:8126 bin/rails s -p 50130
### Disabling

By default statsd and tracing are enabled by simply including this gem into an environment that has the `ECS_ENABLE_CONTAINER_METADATA` or `DD_AGENT_URI` environment variables enabled above, and the `RAILS_ENV` is set to either `production` or `staging`. If there are cases where you do want to disable it, you can set the ENV var `DISABLE_DEGICA_DATADOG=true` and you will force disable this gem.

23 changes: 15 additions & 8 deletions lib/degica_datadog/tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,34 @@ def span!(name, **options, &block)
def span_tags!(**tags)
return unless Config.enabled?

current_span = Datadog::Tracing.active_span
tags.each do |k, v|
current_span&.set_tag(k.to_s, v)
end
end

# Add an exception to the current span and mark it as errored.
def error!(e)
return unless Config.enabled?
return unless e.is_a?(Exception)
return unless Config.enabled? && e.is_a?(Exception)

current_span = Datadog::Tracing.active_span
current_span&.set_error(e)
root_span&.set_error(e)
end

# Returns the current span.
def current_span
Datadog::Tracing.active_span unless Config.enabled?
end

# Returns the current root span. Root here meaning within the service, not necessarily the
# actual trace root span if that is from a different service.
def root_span
# forgive me my friends
Datadog::Tracing.active_trace.instance_variable_get(:@root_span) unless Config.enabled?
end

# Please don't use this. It's just a temporary thing until we can get the
# statsd agent installed
def root_span_tags!(**tags)
return unless Config.enabled?

# forgive me my friends
root_span = Datadog::Tracing.active_trace.instance_variable_get(:@root_span)
tags.each do |k, v|
root_span&.set_tag(k.to_s, v)
end
Expand Down Expand Up @@ -153,6 +159,7 @@ def enrich_span_options!(options)
end
end

# Default span tags that get attached automatically.
def default_span_tags
{
"env" => Config.environment,
Expand Down

0 comments on commit 927556c

Please sign in to comment.