Skip to content

Commit

Permalink
feat: Allow filtering the http.target attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
fbogsany committed Sep 29, 2023
1 parent d89b68e commit 43b6cc3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
# or `Regexp` in this array, the instrumentation will not record a
# `kind = :client` representing the request and will not propagate
# context in the request.
option :untraced_hosts, default: [], validate: :array
# filter_target: a callable object that takes a path `String` and returns
# a `String` to be used as the http.target attribute. The default is to
# use the request path as-is.
option :untraced_hosts, default: [], validate: :array
option :filter_target, default: nil, validate: :callable

private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def request(req, body = nil, &block)
attributes = {
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => req.method,
OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => USE_SSL_TO_SCHEME[use_ssl?],
OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => req.path,
OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => filter_target(req.path),
OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => @address,
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => @port
}.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
Expand Down Expand Up @@ -85,6 +85,14 @@ def tracer
Net::HTTP::Instrumentation.instance.tracer
end

def filter_target(path)
if (filter = Net::HTTP::Instrumentation.instance.config[:filter_target])
filter.call(path)
else
path
end
end

def untraced?
return true if Net::HTTP::Instrumentation.instance.config[:untraced_hosts]&.any? do |host|
host.is_a?(Regexp) ? host.match?(@address) : host == @address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@
OpenTelemetry.propagation = @orig_propagation
end

describe '#filter_target' do
before do
stub_request(:get, 'http://example.com/path?token=broken').to_return(status: 200)
instrumentation.instance_variable_set(:@installed, false)
config = {
filter_target: proc { |path| URI(path).path }
}

instrumentation.install(config)
end

it 'reports the target without params' do
Net::HTTP.get(URI('http://example.com/path?token=broken'))

_(exporter.finished_spans.size).must_equal 1
_(span.attributes['http.target']).must_equal '/path'
_(span.attributes['net.peer.name']).must_equal 'example.com'
assert_requested(
:get,
'http://example.com/path?token=broken',
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
)
end
end

describe '#request' do
it 'before request' do
_(exporter.finished_spans.size).must_equal 0
Expand Down

0 comments on commit 43b6cc3

Please sign in to comment.