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

Datadog::Correlation usage improvements #664

Merged
merged 3 commits into from
Dec 27, 2018
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
48 changes: 29 additions & 19 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -1608,9 +1608,35 @@ Datadog::Pipeline.before_flush(

### Trace correlation

In many cases, such as logging, it may be useful to correlate trace IDs to other events or data streams, for easier cross referencing. The tracer can produce a correlation identifier for the currently active trace via `active_correlation_ids`, which can be used to decorate these other data sources.
In many cases, such as logging, it may be useful to correlate trace IDs to other events or data streams, for easier cross referencing. The tracer can produce a correlation identifier for the currently active trace via `active_correlation`, which can be used to decorate these other data sources.

An example of this for the purpose of logging:
```ruby
# When a trace is active...
Datadog.tracer.trace('correlation.example') do
# Returns #<Datadog::Correlation::Identifier>
correlation = Datadog.tracer.active_correlation
correlation.trace_id # => 5963550561812073440
correlation.span_id # => 2232727802607726424
end

# When a trace isn't active...
correlation = Datadog.tracer.active_correlation
# Returns #<Datadog::Correlation::Identifier>
correlation = Datadog.tracer.active_correlation
correlation.trace_id # => 0
correlation.span_id # => 0
```

#### For logging

To add correlation IDs to your logger, simply add a log formatter which retrieve the correlation IDs via `Datadog.tracer.active_correlation`, then add them to the message.

To properly correlate with Datadog logging, be sure the following is present:

- `dd.trace_id=<trace_id>`: Where `<trace_id>` is `Datadog.tracer.active_correlation.trace_id`. `0` if no trace active.
- `dd.span_id=<span_id>`: Where `<span_id>` is `Datadog.tracer.active_correlation.span_id`. `0` if no trace active.

An example of this in practice:

```ruby
require 'ddtrace'
Expand All @@ -1620,25 +1646,9 @@ logger = Logger.new(STDOUT)
logger.progname = 'my_app'
logger.formatter = proc do |severity, datetime, progname, msg|
# Returns Datadog::Correlation::Identifier
ids = Datadog.tracer.active_correlation_ids
ids = Datadog.tracer.active_correlation
"[#{datetime}][#{progname}][#{severity}][dd.trace_id=#{ids.trace_id} dd.span_id=#{ids.span_id}] #{msg}\n"
end

# When a trace is active...
Datadog.tracer.trace('logging.example') do
# And a warning is produced...
logger.warn('This is a warning.')

# Prints:
# [2018-12-18 22:42:25 +0000][my_app][WARN][dd.trace_id=5963550561812073440 dd.span_id=2232727802607726424] This is a warning.
end

# When no trace is active...
# And a warning is produced...
logger.warn('This is a warning.')

# Prints:
# [2018-12-18 22:44:19 +0000][my_app][WARN][dd.trace_id= dd.span_id=] This is a warning.
```

### OpenTracing
Expand Down
13 changes: 12 additions & 1 deletion lib/ddtrace/correlation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ module Datadog
# e.g. Retrieve a correlation to the current trace for logging, etc.
module Correlation
# Struct representing correlation
Identifier = Struct.new(:trace_id, :span_id)
Identifier = Struct.new(:trace_id, :span_id).tap do |struct|
# Do this #class_eval here for Ruby 1.9.3 support.
# Ruby 2.0+ supports passing a block to Struct::new instead.
struct.class_eval do
def initialize(*args)
super
self.trace_id = trace_id || 0
self.span_id = span_id || 0
end
end
end.freeze

NULL_IDENTIFIER = Identifier.new.freeze

module_function
Expand Down
2 changes: 1 addition & 1 deletion lib/ddtrace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def active_root_span
end

# Return a CorrelationIdentifier for active span
def active_correlation_ids
def active_correlation
Datadog::Correlation.identifier_from_context(call_context)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/ddtrace/correlation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

it 'returns an empty Correlation::Identifier' do
is_expected.to be_a_kind_of(Datadog::Correlation::Identifier)
expect(correlation_ids.trace_id).to be nil
expect(correlation_ids.span_id).to be nil
expect(correlation_ids.trace_id).to be 0
expect(correlation_ids.span_id).to be 0
end
end

Expand Down
12 changes: 6 additions & 6 deletions spec/ddtrace/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
end
end

describe '#active_correlation_ids' do
subject(:active_correlation_ids) { tracer.active_correlation_ids }
describe '#active_correlation' do
subject(:active_correlation) { tracer.active_correlation }

context 'when a trace is active' do
let(:span) { @span }
Expand All @@ -112,16 +112,16 @@

it 'produces an Datadog::Correlation::Identifier with data' do
is_expected.to be_a_kind_of(Datadog::Correlation::Identifier)
expect(active_correlation_ids.trace_id).to eq(span.trace_id)
expect(active_correlation_ids.span_id).to eq(span.span_id)
expect(active_correlation.trace_id).to eq(span.trace_id)
expect(active_correlation.span_id).to eq(span.span_id)
end
end

context 'when no trace is active' do
it 'produces an empty Datadog::Correlation::Identifier' do
is_expected.to be_a_kind_of(Datadog::Correlation::Identifier)
expect(active_correlation_ids.trace_id).to be nil
expect(active_correlation_ids.span_id).to be nil
expect(active_correlation.trace_id).to be 0
expect(active_correlation.span_id).to be 0
end
end
end
Expand Down