-
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
Open
xuan-cao-swi
wants to merge
10
commits into
main
Choose a base branch
from
NH-132896
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 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 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
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # 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 | ||
| 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 | ||
| 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 | ||
| 'done' | ||
| end | ||
| assert_equal 'done', result | ||
| end | ||
| end | ||
|
|
||
| describe 'API::CustomMetrics deprecated methods return false' do | ||
| it 'increment_metric returns false with deprecation' do | ||
| result = SolarWindsAPM::API.increment_metric('test_metric', 1, false, {}) | ||
| assert_equal false, result | ||
| end | ||
|
|
||
| it 'summary_metric returns false with deprecation' do | ||
| result = SolarWindsAPM::API.summary_metric('test_metric', 5.0, 1, false, {}) | ||
| assert_equal false, result | ||
| end | ||
| end | ||
|
|
||
| describe 'API::Tracer#add_tracer method wrapping with span instrumentation' do | ||
| 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 | ||
|
|
||
| OpenTelemetry::SDK.configure | ||
|
|
||
| instance = klass.new | ||
| result = instance.greeting | ||
| assert_equal 'hello', result | ||
| 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 | ||
|
|
||
| OpenTelemetry::SDK.configure | ||
|
|
||
| instance = klass.new | ||
| result = instance.work | ||
| assert_equal 'done', result | ||
| 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 | ||
|
|
||
| OpenTelemetry::SDK.configure | ||
|
|
||
| instance = klass.new | ||
| result = instance.compute | ||
| assert_equal 100, result | ||
| 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,143 @@ | ||
| # 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 | ||
|
|
||
| trace = SolarWindsAPM::API.current_trace_info | ||
| result = trace.hash_for_log | ||
| assert result.key?('trace_id') | ||
| assert result.key?('span_id') | ||
| assert result.key?('trace_flags') | ||
| assert result.key?('resource.service.name') | ||
| ensure | ||
| SolarWindsAPM::Config[:log_traceId] = original | ||
| 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 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_equal 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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see the return nil is asserted. how is the "warns when block is nil" tested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added assertion that logger.warn will be called.