Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

require_relative '../../../../../lib/opentelemetry/instrumentation/net/http/http_helper'

describe OpenTelemetry::Instrumentation::Net::HTTP::HttpHelper do
let(:helper) { OpenTelemetry::Instrumentation::Net::HTTP::HttpHelper }

describe '.span_attrs_for_stable' do
it 'normalizes known methods from symbol and lowercase string' do
data_sym = helper.span_attrs_for_stable(:get)
_(data_sym.span_name).must_equal 'GET'
_(data_sym.attributes['http.request.method']).must_equal 'GET'

data_str = helper.span_attrs_for_stable('get')
_(data_str.span_name).must_equal 'GET'
_(data_str.attributes['http.request.method']).must_equal 'GET'
end

it 'handles unknown methods and sets original' do
data = helper.span_attrs_for_stable('purge')
_(data.span_name).must_equal 'HTTP'
_(data.attributes['http.request.method']).must_equal '_OTHER'
_(data.attributes['http.request.method_original']).must_equal 'purge'
end

it 'includes url.template in span name from client context' do
OpenTelemetry::Common::HTTP::ClientContext.with_attributes('url.template' => '/widgets/{id}') do
data = helper.span_attrs_for_stable('GET')
_(data.span_name).must_equal 'GET /widgets/{id}'
_(data.attributes['url.template']).must_equal '/widgets/{id}'
end
end
end

describe '.span_attrs_for_old' do
it 'builds old-style span name and attributes' do
data = helper.span_attrs_for_old('GET')
_(data.span_name).must_equal 'HTTP GET'
_(data.attributes['http.method']).must_equal 'GET'
end

it 'handles unknown methods with _OTHER' do
data = helper.span_attrs_for_old(:purge)
_(data.span_name).must_equal 'HTTP'
_(data.attributes['http.method']).must_equal '_OTHER'
end
end

describe '.span_attrs_for_dup' do
it 'sets both old and stable method attributes' do
data = helper.span_attrs_for_dup('POST')
_(data.span_name).must_equal 'POST'
_(data.attributes['http.method']).must_equal 'POST'
_(data.attributes['http.request.method']).must_equal 'POST'
end

it 'captures original method for unknown methods' do
data = helper.span_attrs_for_dup('weird')
_(data.span_name).must_equal 'HTTP'
_(data.attributes['http.method']).must_equal '_OTHER'
_(data.attributes['http.request.method']).must_equal '_OTHER'
_(data.attributes['http.request.method_original']).must_equal 'weird'
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

require_relative '../../../../../lib/opentelemetry/instrumentation/net/http/instrumentation'
require_relative '../../../../../lib/opentelemetry/instrumentation/net/http/patches/stable/instrumentation'

describe OpenTelemetry::Instrumentation::Net::HTTP::Instrumentation do
let(:instrumentation) { OpenTelemetry::Instrumentation::Net::HTTP::Instrumentation.instance }

describe 'semantic convention selection' do
after do
ENV.delete('OTEL_SEMCONV_STABILITY_OPT_IN')
end

it 'defaults to stable when no env set' do
ENV.delete('OTEL_SEMCONV_STABILITY_OPT_IN')
result = instrumentation.send(:determine_semconv)
_(result).must_equal 'stable'
end

it 'selects dup and warns when http/dup set' do
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http/dup'

expect(OpenTelemetry.logger).to receive(:warn)
result = instrumentation.send(:determine_semconv)
_(result).must_equal 'dup'
end

it 'selects old and warns when old set' do
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'old'

expect(OpenTelemetry.logger).to receive(:warn)
result = instrumentation.send(:determine_semconv)
_(result).must_equal 'old'
end
end

describe 'split_path_and_query helper' do
it 'splits path and query when query present' do
dummy = Class.new do
include OpenTelemetry::Instrumentation::Net::HTTP::Patches::Stable::Instrumentation
end.new

path, query = dummy.send(:split_path_and_query, '/foo/bar?x=1&y=2')
_(path).must_equal '/foo/bar'
_(query).must_equal 'x=1&y=2'
end

it 'returns nil query when absent' do
dummy = Class.new do
include OpenTelemetry::Instrumentation::Net::HTTP::Patches::Stable::Instrumentation
end.new

path, query = dummy.send(:split_path_and_query, '/foo')
_(path).must_equal '/foo'
_(query).must_be_nil
end
end
end
Loading