Skip to content

Commit

Permalink
feat: Add untraced endpoints config to rack middleware (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlaurin authored Feb 3, 2021
1 parent 9cbce7e commit 02fa36b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ def clear_cached_config

def initialize(app)
@app = app
@untraced_endpoints = config[:untraced_endpoints].is_a?(Array) ? config[:untraced_endpoints] : []
end

def call(env) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
if untraced_request?(env)
OpenTelemetry::Common::Utilities.untraced do
return @app.call(env)
end
end
original_env = env.dup
extracted_context = OpenTelemetry.propagation.extract(
env,
Expand Down Expand Up @@ -79,6 +85,10 @@ def call(env) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength

private

def untraced_request?(env)
@untraced_endpoints.include?(env['PATH_INFO'])
end

# return Context with the frontend span as the current span
def create_frontend_span(env, extracted_context)
request_start_time = OpenTelemetry::Instrumentation::Rack::Util::QueueTime.get_request_start(env)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
let(:rack_builder) { Rack::Builder.new }

let(:exporter) { EXPORTER }
let(:finished_spans) { exporter.finished_spans }
let(:first_span) { exporter.finished_spans.first }

let(:default_config) { {} }
Expand Down Expand Up @@ -71,6 +72,50 @@
_(first_span.parent_span_id).must_equal OpenTelemetry::Trace::INVALID_SPAN_ID
end

describe 'config[:untraced_endpoints]' do
describe 'when an array is passed in' do
let(:config) { { untraced_endpoints: ['/ping'] } }

it 'does not trace paths listed in the array' do
Rack::MockRequest.new(rack_builder).get('/ping', env)

ping_span = finished_spans.find { |s| s.name == '/ping' }
_(ping_span).must_be_nil

root_span = finished_spans.find { |s| s.name == '/' }
_(root_span).wont_be_nil
end
end

describe 'when a string is passed in' do
let(:config) { { untraced_endpoints: '/ping' } }

it 'traces everything' do
Rack::MockRequest.new(rack_builder).get('/ping', env)

ping_span = finished_spans.find { |s| s.name == '/ping' }
_(ping_span).wont_be_nil

root_span = finished_spans.find { |s| s.name == '/' }
_(root_span).wont_be_nil
end
end

describe 'when nil is passed in' do
let(:config) { { untraced_endpoints: nil } }

it 'traces everything' do
Rack::MockRequest.new(rack_builder).get('/ping', env)

ping_span = finished_spans.find { |s| s.name == '/ping' }
_(ping_span).wont_be_nil

root_span = finished_spans.find { |s| s.name == '/' }
_(root_span).wont_be_nil
end
end
end

describe 'config[:allowed_request_headers]' do
let(:env) { Hash('HTTP_FOO_BAR' => 'http foo bar value') }

Expand Down

0 comments on commit 02fa36b

Please sign in to comment.