-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Add a connect span to excon (#712)
* Add a connect span to excon and add more span attributes to the tracer middleware * Skip matching on the error message as it varies by platform * Rescue the IOError that can occur on accept * Switch to must_be_empty assertion instead of size * Move allowing and disallowing connect to setup and after respectively. * use dig when getting hostname and port of proxy * Call untraced when we hit an untraced host. * Switch to using recording? to test whether we should finish a span. * Switch to handle_error instead of debug logging. * Record the exception on error * Perform the next step in the middleware stack in the context of the current span. * Add assertions on http spans to connect tests. * Fix rubocop lints * Remove interpolation from status message on span now that we capture exceptions as an event. * Switch to attach and detach instead of with_span * Include untraced context into untraced? check for the middleware and patch. * Add test for untraced. * Add a module that centralizes the untraced hosts concern. * Expand doc comment. * Move module to the excon gem. * Add doc comment for untraced? in the concern * Update instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb Co-authored-by: Ariel Valentin <[email protected]> --------- Co-authored-by: Ariel Valentin <[email protected]>
- Loading branch information
1 parent
12eef00
commit aedc42c
Showing
5 changed files
with
320 additions
and
51 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
instrumentation/excon/lib/opentelemetry/instrumentation/concerns/untraced_hosts.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Instrumentation | ||
module Concerns | ||
# The untraced hosts concerns allows instrumentation to skip traces on hostnames in an exclusion list. | ||
# If the current OpenTelemetry context is untraced, all hosts will be treated as untraced. | ||
# When included in a class that extends OpenTelemetry::Instrumentation::Base, this module defines an option named :untraced_hosts. | ||
module UntracedHosts | ||
def self.included(klass) | ||
klass.instance_eval do | ||
# untraced_hosts: if a request's address matches any of the `String` | ||
# 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 | ||
end | ||
end | ||
|
||
# Checks whether the given host should be treated as untraced. | ||
# If the current OpenTelemetry context is untraced, all hosts will be treated as untraced. | ||
# The given host must be a String. | ||
def untraced?(host) | ||
OpenTelemetry::Common::Utilities.untraced? || untraced_host?(host) | ||
end | ||
|
||
private | ||
|
||
def untraced_host?(host) | ||
config[:untraced_hosts].any? do |rule| | ||
rule.is_a?(Regexp) ? rule.match?(host) : rule == host | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
instrumentation/excon/lib/opentelemetry/instrumentation/excon/patches/socket.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Instrumentation | ||
module Excon | ||
module Patches | ||
# Module to prepend to an Excon Socket for instrumentation | ||
module Socket | ||
private | ||
|
||
def connect | ||
return super if untraced? | ||
|
||
if @data[:proxy] | ||
conn_address = @data.dig(:proxy, :hostname) | ||
conn_port = @data.dig(:proxy, :port) | ||
else | ||
conn_address = @data[:hostname] | ||
conn_port = @port | ||
end | ||
|
||
attributes = { OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => conn_address, OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => conn_port }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) | ||
|
||
if is_a?(::Excon::SSLSocket) && @data[:proxy] | ||
span_name = 'HTTP CONNECT' | ||
span_kind = :client | ||
else | ||
span_name = 'connect' | ||
span_kind = :internal | ||
end | ||
|
||
tracer.in_span(span_name, attributes: attributes, kind: span_kind) do | ||
super | ||
end | ||
end | ||
|
||
def tracer | ||
Excon::Instrumentation.instance.tracer | ||
end | ||
|
||
def untraced? | ||
address = if @data[:proxy] | ||
@data.dig(:proxy, :hostname) | ||
else | ||
@data[:hostname] | ||
end | ||
|
||
Excon::Instrumentation.instance.untraced?(address) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.