Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export OTEL_TRACES_EXPORTER=otlp,console # Multiple Exporters
export OTEL_TRACES_EXPORTER=jaeger # Third-party Exporters (e.g. jaeger, add gem opentelemetry-exporter-jaeger before solarwinds_apm)
```

The API token from `SW_APM_SERVICE_KEY` is automatically added as an `authorization` header only for a SolarWinds Observability OTLP endpoint (`https://otel.collector.*.*.solarwinds.com`) and when neither the general OTLP headers variable nor that signal's headers variable has a non-empty value. It is automatically added to the signal-specific variable (e.g. `OTEL_EXPORTER_OTLP_TRACES_HEADERS`), never to `OTEL_EXPORTER_OTLP_HEADERS`.

To route telemetry from an OTel Collector in your environment to SolarWinds Observability, please configure the API token in your OTelCol's exporter.

#### Service Name

By default the service name portion of the service key is used, e.g. `my-service` if the service key is `SW_APM_SERVICE_KEY=api-token:my-service`. If the `OTEL_SERVICE_NAME` or `OTEL_RESOURCE_ATTRIBUTES` environment variable is used to specify a service name, it will take precedence over the default. The precedence is as follows:
Expand Down
50 changes: 38 additions & 12 deletions lib/solarwinds_apm/support/otlp_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

require 'uri'
require_relative 'service_key_checker'

module SolarWindsAPM
# OTLPEndPoint
class OTLPEndPoint
attr_reader :token, :service_name

SWO_APM_ENDPOINT_REGEX = /^apm\.collector\.([a-z]{2}-\d{2})\.([^.]+)\.solarwinds\.com(?::\d+)?$/
SWO_APM_ENDPOINT_REGEX = /\Aapm\.collector\.([a-z]{2}-\d{2})\.([a-z0-9-]+)\.solarwinds\.com(?::\d+)?\z/
SWO_APM_ENDPOINT_DEFAULT = 'apm.collector.na-01.cloud.solarwinds.com:443'

SWO_OTLP_GENERAL_ENDPOINT_REGEX = %r{^https://otel\.collector\.[a-z0-9-]+\.[a-z0-9-]+\.solarwinds\.com(?::\d+)?$}
SWO_OTLP_SIGNAL_ENDPOINT_REGEX = %r{^https://otel\.collector\.[a-z0-9-]+\.[a-z0-9-]+\.solarwinds\.com(?::\d+)?/v1/(?:logs|metrics|traces)$}
SWO_OTLP_HOST_REGEX = /\Aotel\.collector\.[a-z0-9-]+\.[a-z0-9-]+\.solarwinds\.com\z/
SWO_OTLP_ENDPOINT_DEFAULT = 'https://otel.collector.na-01.cloud.solarwinds.com:443'

OTEL_SIGNAL_TYPE = %w[TRACES METRICS LOGS].freeze
Expand All @@ -37,8 +37,8 @@ def config_otlp_token_and_endpoint
@service_name = service_key_checker.service_name

OTEL_SIGNAL_TYPE.each do |data_type|
config_token(data_type)
configure_otlp_endpoint(data_type, matches)
config_token(data_type)
end
end

Expand All @@ -47,16 +47,13 @@ def config_otlp_token_and_endpoint
# The OTLP export endpoint is SWO, i.e. host is otel.collector.*.*.solarwinds.com
def config_token(data_type)
data_type_upper = data_type.upcase
signal_headers_env = "OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"

return unless @token
return unless ENV[signal_headers_env].to_s.empty? && ENV['OTEL_EXPORTER_OTLP_HEADERS'].to_s.empty?

if ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"].to_s.empty? && ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT"].to_s.match?(SWO_OTLP_SIGNAL_ENDPOINT_REGEX)
ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"] = "authorization=Bearer #{@token}"
elsif ENV['OTEL_EXPORTER_OTLP_HEADERS'].to_s.empty? && ENV['OTEL_EXPORTER_OTLP_ENDPOINT'].to_s.match?(SWO_OTLP_GENERAL_ENDPOINT_REGEX)
ENV['OTEL_EXPORTER_OTLP_HEADERS'] = "authorization=Bearer #{@token}"
end

ENV['OTEL_EXPORTER_OTLP_HEADERS'] = "authorization=Bearer #{@token}" if ENV['OTEL_EXPORTER_OTLP_HEADERS'].to_s.empty? && ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"].to_s.empty?
endpoint = effective_endpoint(data_type_upper)
ENV[signal_headers_env] = "authorization=Bearer #{@token}" if swo_otlp_endpoint?(endpoint)
end

def configure_otlp_endpoint(data_type, matches)
Expand All @@ -83,14 +80,43 @@ def configure_otlp_endpoint(data_type, matches)
end
end

# Same precedence as the OpenTelemetry OTLP exporters: signal-specific endpoint, then the general endpoint.
def effective_endpoint(data_type_upper)
ENV.fetch("OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT", nil) || ENV.fetch('OTEL_EXPORTER_OTLP_ENDPOINT', nil)
end

# Parse the URL rather than pattern-matching the raw string, so userinfo, path or
# newline tricks cannot make a non-SWO host look like a SWO one.
def swo_otlp_endpoint?(endpoint)
return false if endpoint.to_s.empty?

uri = URI.parse(endpoint.to_s)
uri.scheme == 'https' && uri.userinfo.nil? && uri.host.to_s.downcase.match?(SWO_OTLP_HOST_REGEX)
rescue URI::Error
false
end

# Only scheme, host and port are logged, never userinfo or query that may carry credentials.
def sanitize_endpoint(endpoint)

@cheempz cheempz Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From PR title the intention is to also apply sanitization to the collector endpoint, but i'm not seeing this method used anywhere other than tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to sanitize collector endpoint

uri = URI.parse(endpoint.to_s.strip)
return '<unparseable>' if uri.host.nil?

"#{uri.scheme}://#{uri.host}:#{uri.port}"
rescue URI::Error
'<unparseable>'
end

# only valid value is apm.collector.*.*.solarwinds.com
# If SW APM config for collector is not set, the fallback: apm.collector.na-01.cloud.solarwinds.com
def resolve_get_setting_endpoint(matches)
return if matches&.size == 3

unless ENV['SW_APM_COLLECTOR'].to_s.empty?
# SW_APM_COLLECTOR is expected as host:port; add a scheme only if the user did not, so sanitize_endpoint can parse it.
collector_value = ENV.fetch('SW_APM_COLLECTOR', nil)
collector = sanitize_endpoint(collector_value.include?('://') ? collector_value : "https://#{collector_value}")
SolarWindsAPM.logger.warn do
"[#{self.class}/#{__method__}] SW_APM_COLLECTOR format invalid: #{ENV.fetch('SW_APM_COLLECTOR', nil)}. Valid format: apm.collector.*.*.solarwinds.com"
"[#{self.class}/#{__method__}] SW_APM_COLLECTOR format invalid: #{collector}. Valid format: apm.collector.*.*.solarwinds.com"
end
end
ENV['SW_APM_COLLECTOR'] = SWO_APM_ENDPOINT_DEFAULT
Expand Down
2 changes: 1 addition & 1 deletion test/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def extract(carrier, context: ::OpenTelemetry::Context.current, getter: ::OpenTe
# Dummy Exporter for testing
module Exporter
class Dummy
def flush(); end
def flush; end
end
end

Expand Down
3 changes: 1 addition & 2 deletions test/patch/sw_mysql2_patch_integrate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
require './lib/solarwinds_apm/support'
require './lib/solarwinds_apm/patch/tag_sql/sw_dbo_utils'

# rubocop:disable Naming/MethodName
# rubocop:disable-next Naming/MethodName
module SolarWindsAPM
module Span
def self.createSpan(trans_name, domain, span_time, has_error); end
end
end
# rubocop:enable Naming/MethodName

describe 'mysql2 patch integrate test' do
puts "\n\033[1m=== TEST RUN MYSQL2 PATCH TEST: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n"
Expand Down
3 changes: 1 addition & 2 deletions test/patch/sw_pg_patch_integrate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
require './lib/solarwinds_apm/support'
require './lib/solarwinds_apm/patch/tag_sql/sw_dbo_utils'

# rubocop:disable Naming/MethodName
# rubocop:disable-next Naming/MethodName
module SolarWindsAPM
module Span
def self.createSpan(trans_name, domain, span_time, has_error); end
end
end
Comment on lines +15 to 20

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new rubocop will use rubocop:disable-next to disable entire module block so # rubocop:disable Naming/MethodName is not needed

# rubocop:enable Naming/MethodName

def pg_dbo_integration_verification(sql, finished_spans)
pattern = %r{/\*traceparent='[\da-f-]+'*\*/$}
Expand Down
3 changes: 1 addition & 2 deletions test/sampling_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def check_counters(metric_exporter, counters = [])
end
end

# rubocop:disable Lint/DuplicateMethods
# rubocop:disable-next Lint/DuplicateMethods
class OboeTestSampler < SolarWindsAPM::OboeSampler
attr_accessor :response_headers, :local_settings, :request_headers

Expand All @@ -155,7 +155,6 @@ def request_headers(_params)
@request_headers
end
end
# rubocop:enable Lint/DuplicateMethods

def replace_sampler(sampler)
OpenTelemetry.tracer_provider.sampler = OpenTelemetry::SDK::Trace::Samplers.parent_based(
Expand Down
Loading
Loading