Commit 3900b84
Deduplicate identical key/value pairs in user agent builder (#1718)
## Summary
`useragent.data.With()` appended every key/value pair unconditionally
and never deduplicated. When the same user-agent dimension is injected
repeatedly onto a reused or long-lived `context.Context` — for example a
caller that threads a single context across many operations — the
per-context user-agent data accumulates duplicate entries, so
`FromContext` produces a `User-Agent` string that grows without bound
(e.g. `... sdk/x sdk/x resource/y resource/y ...`). Sufficiently large
headers can be rejected or truncated downstream.
Note that `With`'s doc comment claimed it "always uses the latest value
for a given alphanumeric key", but the implementation always appended,
and existing tests rely on multiple **distinct** values per key being
preserved (e.g. multiple partners). This change keeps that behavior and
only removes exact duplicates.
## Change
`data.With()` now skips appending a key/value pair when an identical
pair is already present. Distinct values for the same key are still
preserved, so no existing behavior or tests change. The doc comment is
updated to describe the actual behavior.
## Testing
- `go test ./useragent/` — all pass, including `TestMultiplePartners`
and `TestFromContext_Custom`.
- Added `TestInContext_DeduplicatesIdenticalPairs`: 100 repeated
identical injections collapse to one entry each; distinct values still
preserved.
- `go vet ./useragent/` and `gofmt` clean.
🤖 Generated with [Claude Code](https://claude.com/claude-code).
---------
Co-authored-by: Renaud Hartert <renaud.hartert@databricks.com>1 parent ad23528 commit 3900b84
3 files changed
Lines changed: 32 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
115 | 118 | | |
116 | 119 | | |
117 | 120 | | |
| |||
120 | 123 | | |
121 | 124 | | |
122 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
123 | 131 | | |
124 | 132 | | |
125 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
0 commit comments