[test-improver] Improve tests for tracing package#3938
Merged
Conversation
Add tests for three previously uncovered paths in the tracing package: - TestInitProvider_InvalidSampleRate: exercises the warning/fallback path in resolveSampleRate when the configured rate is outside [0.0, 1.0] - TestParentContext_InvalidSpanID: covers the resolveParentContext fallback to a random span ID when spanId fails hex decode or has wrong length returns the original context when the traceId is all zeros Coverage improves from 91.7% to 96.3% for the tracing package. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
4 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
Improves unit test coverage for the internal/tracing provider initialization and W3C parent context resolution paths, focusing on previously untested invalid configuration inputs.
Changes:
- Add table-driven coverage for invalid tracing
SampleRatevalues to exercise the fallback-to-default path. - Add coverage for invalid
spanIdinputs to confirmParentContextfalls back to generating a random span ID while preserving the configuredtraceId. - Add coverage for the all-zero
traceIdvalidity guard to ensureParentContextreturns the original context unchanged.
Show a summary per file
| File | Description |
|---|---|
| internal/tracing/provider_test.go | Adds new test cases for invalid sample rates and additional ParentContext fallback/guard branches. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 1
Comment on lines
+544
to
+545
| // generating a random span ID when the configured spanId is not valid hex, and still | ||
| // returns an enriched context (per spec T-OTEL-008). |
There was a problem hiding this comment.
The doc comment says the random span ID path is only when spanId is "not valid hex", but this test also covers valid-hex values with invalid byte length (too short/too long). Update the comment to reflect both decode errors and wrong-length inputs so the test intent stays accurate.
Suggested change
| // generating a random span ID when the configured spanId is not valid hex, and still | |
| // returns an enriched context (per spec T-OTEL-008). | |
| // generating a random span ID when the configured spanId is not valid hex or is valid | |
| // hex with the wrong byte length, and still returns an enriched context (per spec | |
| // T-OTEL-008). |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
File Analyzed
internal/tracing/provider_test.gointernal/tracingImprovements Made
1. Increased Coverage
✅ Added
TestInitProvider_InvalidSampleRate— exercises the warning/fallback path inresolveSampleRatewhen the configured rate is outside [0.0, 1.0] (e.g., -0.5 or 1.5); verifies the defaultAlwaysSampleis used✅ Added
TestParentContext_InvalidSpanID— covers theresolveParentContextfallback to a random span ID whenspanIdfails hex decode or has wrong byte length; uses three sub-cases (non-hex, too-short, too-long)✅ Added
TestParentContext_AllZerosTraceID— covers the!sc.IsValid()guard that returns the original context whentraceIddecodes to all-zero bytes (invalid per W3C Trace Context spec)Previous Coverage: 91.7%
New Coverage: 96.3%
Improvement: +4.6%
resolveSampleRateresolveParentContextgenerateRandomSpanID2. Better Testing Patterns
TestInitProvider_InvalidSampleRateandTestParentContext_InvalidSpanID, consistent with existing test styleTestParentContext_InvalidSpanIDto confirm the enriched context carries the correct traceIdTest Execution
All tests in the tracing package pass:
Note:
TestFetchAndFixSchema_NetworkErrorininternal/configfails on the baseline (pre-existing, unrelated to this PR).Why These Changes?
The tracing package had three functions with meaningful untested branches.
resolveSampleRateat 60% left the warning/default path completely uncovered, meaning misconfigured sample rates (outside [0.0, 1.0]) were not validated by tests.resolveParentContextat 81.5% missed the span ID fallback logic that silently generates a random span ID — an important spec behaviour (T-OTEL-008). The all-zeros TraceID path guarded by!sc.IsValid()was also untested. All three gaps are now covered with clear, focused test cases.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests
Warning
The following domain was blocked by the firewall during workflow execution:
invalidhostthatdoesnotexist12345.comTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.