Skip to content

Commit

Permalink
Merge pull request #1458 from DataDog/feature/trace_completed_event
Browse files Browse the repository at this point in the history
Add trace_completed event to Tracer
  • Loading branch information
delner authored Apr 13, 2021
2 parents 40c16d3 + 34e2fa3 commit 80a960f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
14 changes: 14 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ RSpec/LeadingSubject:
RSpec/ImplicitSubject:
Enabled: false

# Enforces empty line after subject declaration.
RSpec/EmptyLineAfterSubject:
Enabled: false

# Enforces empty line after last let declaration.
RSpec/EmptyLineAfterFinalLet:
Enabled: false

# TODO: Disabling until we categorize which file are safe to do so.
Style/FrozenStringLiteralComment:
Enabled: false
Expand All @@ -250,6 +258,12 @@ Lint/EmptyBlock:
RSpec/ContextWording:
Enabled: false

# Checks for multiple top-level example groups.
# Multiple descriptions for the same class or module should either
# be nested or separated into different test files.
RSpec/MultipleDescribes:
Enabled: false

# Enforces that examples should only have a limited amount of assertions.
RSpec/MultipleExpectations:
Enabled: false
Expand Down
21 changes: 21 additions & 0 deletions lib/ddtrace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'ddtrace/sampler'
require 'ddtrace/sampling'
require 'ddtrace/correlation'
require 'ddtrace/event'
require 'ddtrace/utils/only_once'

# \Datadog global namespace that includes all tracing functionality for Tracer and Span classes.
Expand Down Expand Up @@ -315,6 +316,10 @@ def trace(name, options = {})
end
end

def trace_completed
@trace_completed ||= TraceCompleted.new
end

# Record the given +context+. For compatibility with previous versions,
# +context+ can also be a span. It is similar to the +child_of+ argument,
# method will figure out what to do, submitting a +span+ for recording
Expand Down Expand Up @@ -366,6 +371,22 @@ def write(trace)
end

@writer.write(trace)
trace_completed.publish(trace)
end

# Triggered whenever a trace is completed
class TraceCompleted < Datadog::Event
def initialize
super(:trace_completed)
end

# NOTE: Ignore Rubocop rule. This definition allows for
# description of and constraints on arguments.
# rubocop:disable Lint/UselessMethodDefinition
def publish(trace)
super(trace)
end
# rubocop:enable Lint/UselessMethodDefinition
end

# TODO: Move this kind of configuration building out of the tracer.
Expand Down
41 changes: 37 additions & 4 deletions spec/ddtrace/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,12 @@

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

let(:context) { instance_double(Datadog::Context) }

before do
allow(tracer.trace_completed).to receive(:publish)
end

context 'with trace' do
let(:trace) { [Datadog::Span.new(tracer, 'dummy')] }

Expand All @@ -545,19 +548,49 @@
subject
end

it { expect(writer.spans).to eq(trace) }
it 'writes the trace' do
expect(writer.spans).to eq(trace)

expect(tracer.trace_completed)
.to have_received(:publish)
.with(trace)
end
end

context 'with empty trace' do
let(:trace) { [] }

it { expect(writer.spans).to be_empty }
it 'does not write a trace' do
expect(writer.spans).to be_empty

expect(tracer.trace_completed)
.to_not have_received(:publish)
end
end

context 'with nil trace' do
let(:trace) { nil }

it { expect(writer.spans).to be_empty }
it 'does not write a trace' do
expect(writer.spans).to be_empty

expect(tracer.trace_completed)
.to_not have_received(:publish)
end
end
end

describe '#trace_completed' do
subject(:trace_completed) { tracer.trace_completed }
it { is_expected.to be_a_kind_of(described_class::TraceCompleted) }
end
end

RSpec.describe Datadog::Tracer::TraceCompleted do
subject(:event) { described_class.new }

describe '#name' do
subject(:name) { event.name }
it { is_expected.to be :trace_completed }
end
end

0 comments on commit 80a960f

Please sign in to comment.