[test] Add tests for logger.SlogHandler.Handle via attrKeyString helper - #8730
Merged
Conversation
Extract the defensive non-string attribute key conversion from SlogHandler.Handle into a named helper attrKeyString, and add comprehensive table-driven tests for it. Previously SlogHandler.Handle had 95.2% coverage: the fmt.Sprint fallback branch for non-string keys (defensive dead code - slog always provides string keys via a.Key) was unreachable through the slog public API. By extracting the logic into attrKeyString, the fallback path is now testable independently of the slog record machinery, bringing Handle to 100% coverage and adding an explicit contract test for the string-vs-fallback behavior. Coverage change: Handle: 95.2% -> 100.0% attrKeyString: (new) -> 100.0% logger package: 98.5% -> 98.7% Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors internal/logger.SlogHandler.Handle to extract attribute-key stringification into a small helper (attrKeyString), enabling direct unit tests for the previously untestable defensive fallback branch and increasing coverage for the Handle path.
Changes:
- Extracted key conversion logic from
SlogHandler.HandleintoattrKeyString(v any) string. - Updated/added tests to cover both the normal string-key path and the defensive non-string fallback behavior via table-driven cases.
Show a summary per file
| File | Description |
|---|---|
| internal/logger/slog_adapter.go | Refactors attribute key conversion into attrKeyString and uses it in SlogHandler.Handle. |
| internal/logger/slog_adapter_test.go | Updates the existing test comment and adds TestAttrKeyString table tests to cover fallback behavior. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Low
Comment on lines
811
to
814
| // TestSlogHandler_Handle_NonStringKeyFallback tests the defensive non-string key path. | ||
| // This exercises the fmt.Sprint fallback for non-string attribute keys. | ||
| // In production slog always provides string keys, but the attrKeyString helper | ||
| // also handles unexpected types via fmt.Sprint. | ||
| func TestSlogHandler_Handle_NonStringKeyFallback(t *testing.T) { |
Collaborator
|
@copilot address review feedback |
…_Handle_StringKeyFormatting
Contributor
Done — renamed |
This was referenced Jul 5, 2026
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.
Function Analyzed
internal/loggerSlogHandler.HandleWhy This Function?
SlogHandler.Handlehad an unreachable defensive branch: the inlinefmt.Sprintfallback for non-string attribute keys. Since the slog API always providesstringkeys (viaa.Key), this branch could never be exercised through the public API.The fix extracts the key-conversion logic into a named helper
attrKeyString(v any) string, which:Handlefrom 95.2% to 100% coverageChanges
internal/logger/slog_adapter.goHandle()into new package-level helperattrKeyString(v any) stringHandle()now callsattrKeyString(attrs[i])instead of inlining the type assertioninternal/logger/slog_adapter_test.goTestSlogHandler_Handle_NonStringKeyFallbackcomment to reflect the refactorTestAttrKeyStringwith 6 table-driven test cases:fmt.Sprintfmt.Sprintnilfalls back tofmt.Sprintfmt.SprintTests Added
int,bool,nil,float64Coverage Report
Test Execution
All tests pass:
Generated by Test Coverage Improver
Next run will target the next most complex under-tested function
Warning
Firewall blocked 7 domains
The following domains were blocked by the firewall during workflow execution:
awmgmcpggo.opentelemetry.iogo.yaml.ingolang.orggoogle.golang.orggopkg.inproxy.golang.orgSee Network Configuration for more information.