-
Notifications
You must be signed in to change notification settings - Fork 3
NH-132896: increase test coverage #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9ebccc8
update simplecov config
xuan-cao-swi 987cd43
update for coverage
xuan-cao-swi c505f1f
remove duplicates
xuan-cao-swi 027032a
lint
xuan-cao-swi f611b82
codeql
xuan-cao-swi f078f72
try codecov
xuan-cao-swi d5ab7ed
lint
xuan-cao-swi 982750c
skip_validation
xuan-cao-swi 1b92979
revision
xuan-cao-swi 26b4850
typo
xuan-cao-swi 739b755
improve test case
xuan-cao-swi 5108c93
revision
xuan-cao-swi 9c89c68
Apply suggestions from code review
xuan-cao-swi 7667d5d
revision
xuan-cao-swi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -57,3 +57,5 @@ Style/StringLiterals: | |
| - 'Gemfile' | ||
| Naming/PredicateMethod: | ||
| Enabled: false | ||
| Style/OneClassPerFile: | ||
| Enabled: false | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,145 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright (c) 2025 SolarWinds, LLC. | ||
| # All rights reserved. | ||
|
|
||
| require 'minitest_helper' | ||
| require_relative '../../lib/solarwinds_apm/config' | ||
| require_relative '../../lib/solarwinds_apm/otel_config' | ||
| require './lib/solarwinds_apm/api' | ||
|
|
||
| describe 'API::OpenTelemetry#in_span delegation to OpenTelemetry tracer' do | ||
| it 'returns nil and warns when block is nil' do | ||
|
cheempz marked this conversation as resolved.
|
||
| warned = false | ||
| SolarWindsAPM.logger.stub(:warn, ->(_msg = nil, &block) { warned = true if block&.call&.include?('please provide block') }) do | ||
| result = SolarWindsAPM::API.in_span('test_span') | ||
| assert_nil result | ||
| end | ||
| assert warned, 'Expected a warning to be logged when block is nil' | ||
| end | ||
|
|
||
| it 'calls in_span with a block and asserts the return value' do | ||
| OpenTelemetry::SDK.configure | ||
| result = SolarWindsAPM::API.in_span('test_span') do |span| | ||
| refute_nil span | ||
| assert_equal 'test_span', span.name | ||
| assert span.attributes.empty? | ||
| assert_equal :internal, span.kind | ||
| 42 | ||
| end | ||
| assert_equal 42, result | ||
| end | ||
|
|
||
| it 'passes attributes, kind and other options to in_span' do | ||
| OpenTelemetry::SDK.configure | ||
| result = SolarWindsAPM::API.in_span('test_span', attributes: { 'key' => 'value' }, kind: :internal) do |span| | ||
| refute_nil span | ||
|
cheempz marked this conversation as resolved.
|
||
| assert_equal 'test_span', span.name | ||
| assert_equal 'value', span.attributes['key'] | ||
| assert_equal :internal, span.kind | ||
| 'done' | ||
| end | ||
| assert_equal 'done', result | ||
| end | ||
| end | ||
|
|
||
| describe 'API::CustomMetrics deprecated methods return false' do | ||
| it 'increment_metric returns false with deprecation' do | ||
| warned = false | ||
| SolarWindsAPM.logger.stub(:warn, ->(_msg = nil, &block) { warned = true if block&.call&.include?('increment_metric is deprecated') }) do | ||
| result = SolarWindsAPM::API.increment_metric('test_metric', 1, false, {}) | ||
| assert_equal false, result | ||
| end | ||
| assert warned, 'Expected a deprecation warning to be logged for increment_metric' | ||
| end | ||
|
|
||
| it 'summary_metric returns false with deprecation' do | ||
| warned = false | ||
| SolarWindsAPM.logger.stub(:warn, ->(_msg = nil, &block) { warned = true if block&.call&.include?('summary_metric is deprecated') }) do | ||
| result = SolarWindsAPM::API.summary_metric('test_metric', 5.0, 1, false, {}) | ||
| assert_equal false, result | ||
| end | ||
| assert warned, 'Expected a deprecation warning to be logged for summary_metric' | ||
| end | ||
| end | ||
|
|
||
| describe 'API::Tracer#add_tracer method wrapping with span instrumentation' do | ||
| let(:sdk) { OpenTelemetry::SDK } | ||
| let(:exporter) { sdk::Trace::Export::InMemorySpanExporter.new } | ||
| let(:span_processor) { sdk::Trace::Export::SimpleSpanProcessor.new(exporter) } | ||
|
|
||
| before do | ||
| ENV['OTEL_SERVICE_NAME'] = __FILE__ | ||
| OpenTelemetry.tracer_provider = sdk::Trace::TracerProvider.new.tap do |provider| | ||
| provider.add_span_processor(span_processor) | ||
| end | ||
| end | ||
|
|
||
| after do | ||
| ENV.delete('OTEL_SERVICE_NAME') | ||
| end | ||
|
|
||
| it 'add_tracer wraps an instance method with in_span' do | ||
| klass = Class.new do | ||
| include SolarWindsAPM::API::Tracer | ||
|
|
||
| def greeting | ||
| 'hello' | ||
| end | ||
| add_tracer :greeting, 'greeting_span' | ||
| end | ||
|
|
||
| instance = klass.new | ||
| result = instance.greeting | ||
| assert_equal 'hello', result | ||
|
|
||
| spans = exporter.finished_spans | ||
| skip if spans.empty? | ||
| assert_equal 1, spans.size | ||
| assert_equal 'greeting_span', spans[0].name | ||
| assert_equal :internal, spans[0].kind | ||
| end | ||
|
|
||
| it 'add_tracer uses default span name when nil' do | ||
| klass = Class.new do | ||
| include SolarWindsAPM::API::Tracer | ||
|
|
||
| def work | ||
| 'done' | ||
| end | ||
| add_tracer :work | ||
| end | ||
|
|
||
| instance = klass.new | ||
| result = instance.work | ||
| assert_equal 'done', result | ||
|
|
||
| spans = exporter.finished_spans | ||
| skip if spans.empty? | ||
| assert_equal 1, spans.size | ||
| assert spans[0].name.end_with?('/add_tracer'), "Expected span name to end with '/add_tracer', got #{spans[0].name}" | ||
| assert_equal :internal, spans[0].kind | ||
| end | ||
|
|
||
| it 'add_tracer passes options to in_span' do | ||
| klass = Class.new do | ||
| include SolarWindsAPM::API::Tracer | ||
|
|
||
| def compute | ||
| 100 | ||
| end | ||
| add_tracer :compute, 'compute_span', { attributes: { 'foo' => 'bar' }, kind: :consumer } | ||
| end | ||
|
|
||
| instance = klass.new | ||
| result = instance.compute | ||
| assert_equal 100, result | ||
|
|
||
| spans = exporter.finished_spans | ||
| skip if spans.empty? | ||
| assert_equal 1, spans.size | ||
| assert_equal 'compute_span', spans[0].name | ||
| assert_equal :consumer, spans[0].kind | ||
| assert_equal 'bar', spans[0].attributes['foo'] | ||
| end | ||
| end | ||
This file contains hidden or 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,146 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright (c) 2025 SolarWinds, LLC. | ||
| # All rights reserved. | ||
|
|
||
| require 'minitest_helper' | ||
| require_relative '../../lib/solarwinds_apm/config' | ||
| require_relative '../../lib/solarwinds_apm/otel_config' | ||
| require './lib/solarwinds_apm/api' | ||
|
|
||
| describe 'API::CurrentTraceInfo#for_log and #hash_for_log with log_traceId configuration' do | ||
| describe 'TraceInfo' do | ||
| it 'returns empty string for_log when log_traceId is :never' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :never | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| assert_equal '', trace.for_log | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
|
|
||
| it 'returns empty hash for hash_for_log when log_traceId is :never' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :never | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| assert_equal({}, trace.hash_for_log) | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
|
|
||
| it 'returns trace info for_log when log_traceId is :always' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :always | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| result = trace.for_log | ||
| assert_match(/trace_id=[0-9a-f]{32}/, result) | ||
| assert_match(/span_id=[0-9a-f]{16}/, result) | ||
| assert_match(/trace_flags=[0-9a-f]{2}/, result) | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
|
|
||
| it 'returns hash for hash_for_log when log_traceId is :always' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :always | ||
| original_service_name = ENV.fetch('OTEL_SERVICE_NAME', nil) | ||
| ENV['OTEL_SERVICE_NAME'] = 'test-service-name' | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| result = trace.hash_for_log | ||
| assert_match(/^[0-9a-f]{32}$/, result['trace_id']) | ||
| assert_match(/^[0-9a-f]{16}$/, result['span_id']) | ||
| assert_match(/^[0-9a-f]{2}$/, result['trace_flags']) | ||
| assert_equal 'test-service-name', result['resource.service.name'] | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| ENV['OTEL_SERVICE_NAME'] = original_service_name | ||
| end | ||
|
|
||
| it 'returns empty for_log when :traced and no active trace' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :traced | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| # Without an active trace, trace_id is all zeros, so valid? returns false | ||
| assert_equal '', trace.for_log | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
|
|
||
| it 'returns empty for_log when :sampled and not sampled' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :sampled | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| # Without an active sampled trace, should return empty | ||
| assert_equal '', trace.for_log | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
|
|
||
| it 'returns trace info within an active span for :traced' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :traced | ||
|
|
||
| OpenTelemetry::SDK.configure | ||
| tracer = OpenTelemetry.tracer_provider.tracer('test') | ||
| tracer.in_span('test_span') do | ||
| trace = SolarWindsAPM::API.current_trace_info | ||
| result = trace.for_log | ||
| assert_match(/trace_id=[0-9a-f]{32}/, result) | ||
| end | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
|
|
||
| it 'returns trace info within a sampled span for :sampled' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :sampled | ||
|
|
||
| OpenTelemetry::SDK.configure | ||
| tracer = OpenTelemetry.tracer_provider.tracer('test') | ||
| tracer.in_span('test_span') do | ||
| trace = SolarWindsAPM::API.current_trace_info | ||
| result = trace.for_log | ||
| # The default sampler records & samples, so this should have trace info | ||
| assert_match(/trace_id=[0-9a-f]{32}/, result) | ||
| end | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
|
|
||
| it 'has boolean do_log attribute' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :always | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| assert_equal true, trace.do_log | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
|
|
||
| it 'has trace_id, span_id, trace_flags, tracestring attributes' do | ||
| trace = SolarWindsAPM::API.current_trace_info | ||
| refute_nil trace.trace_id | ||
| refute_nil trace.span_id | ||
| refute_nil trace.trace_flags | ||
| refute_nil trace.tracestring | ||
| end | ||
|
|
||
| it 'for_log is memoized' do | ||
| original = SolarWindsAPM::Config[:log_traceId] | ||
| SolarWindsAPM::Config[:log_traceId] = :always | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| result1 = trace.for_log | ||
| result2 = trace.for_log | ||
| assert_same result1, result2 | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.