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

Add active_root_span to Tracer #503

Merged
merged 6 commits into from
Aug 7, 2018
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
11 changes: 10 additions & 1 deletion docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def finish(name, id, payload)
end
end
```
#####Enriching traces from nested methods
##### Enriching traces from nested methods

You can tag additional information to current active span from any method. Note however that if the method is called and there is no span currently active `active_span` will be nil.

Expand All @@ -240,6 +240,15 @@ current_span = Datadog.tracer.active_span
current_span.set_tag('my_tag', 'my_value') unless current_span.nil?
```

You can also get the root span of the current active trace using the `active_root_span` method. This method will return `nil` if there is no active trace.

```ruby
# e.g. adding tag to active root span

current_root_span = Datadog.tracer.active_root_span
current_root_span.set_tag('my_tag', 'my_value') unless current_root_span.nil?
```

## Integration instrumentation

Many popular libraries and frameworks are supported out-of-the-box, which can be auto-instrumented. Although they are not activated automatically, they can be easily activated and configured by using the `Datadog.configure` API:
Expand Down
8 changes: 8 additions & 0 deletions lib/ddtrace/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def current_span
end
end

def current_root_span
@mutex.synchronize do
return @current_root_span
end
end

# Add a span to the context trace list, keeping it as the last active span.
def add_span(span)
@mutex.synchronize do
Expand All @@ -77,6 +83,7 @@ def add_span(span)
return
end
set_current_span(span)
@current_root_span = span if @trace.empty?
@trace << span
span.context = self
end
Expand Down Expand Up @@ -158,6 +165,7 @@ def reset(options = {})
@sampling_priority = options.fetch(:sampling_priority, nil)
@finished_spans = 0
@current_span = nil
@current_root_span = nil
end

def set_current_span(span)
Expand Down
5 changes: 5 additions & 0 deletions lib/ddtrace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ def active_span
call_context.current_span
end

# Return the current active root span or +nil+.
def active_root_span
call_context.current_root_span
end

# Send the trace to the writer to enqueue the spans list in the agent
# sending queue.
def write(trace)
Expand Down
33 changes: 33 additions & 0 deletions spec/ddtrace/context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Context do
subject(:context) { described_class.new(options) }
let(:options) { {} }
let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) }

describe '#current_root_span' do
subject(:current_root_span) { context.current_root_span }

it { is_expected.to be nil }

context 'after a span is added' do
let(:span) { Datadog::Span.new(tracer, 'span.one', context: context) }
before(:each) { context.add_span(span) }

it { is_expected.to be span }

context 'and is reset' do
before(:each) { context.send(:reset) }
it { is_expected.to be nil }
end

context 'followed by a second' do
let(:span_two) { Datadog::Span.new(tracer, 'span.two', context: context) }
before(:each) { context.add_span(span_two) }
it { is_expected.to be span }
end
end
end
end
10 changes: 10 additions & 0 deletions spec/ddtrace/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@
end
end
end

describe '#active_root_span' do
subject(:active_root_span) { tracer.active_root_span }
let(:span) { instance_double(Datadog::Span) }

it do
expect(tracer.call_context).to receive(:current_root_span).and_return(span)
is_expected.to be(span)
end
end
end