Skip to content

Commit 542faeb

Browse files
feat(telemetrygen): support integer attributes in telemetrygen tool (… (#38410)
#38392) **Description** Introduced support for integer attributes in the `telemetrygen` tool. Previously, the tool required all telemetry attribute values to be strings wrapped in double quotes, which limited the ability to generate realistic test data with integer values. This change extends the existing attribute handling to correctly process integer values. The `--telemetry-attributes` flag now supports both quoted string values and unquoted integer values. **Link to tracking issue** Fixes [[#38392](#38392)] **Documentation** Updated documentation for the `--telemetry-attributes` flag to reflect the new support for integer attributes and provide examples of both string and integer usage. --------- Co-authored-by: Andrzej Stencel <[email protected]>
1 parent a21a6ac commit 542faeb

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: telemetrygen
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Support integer values in `--telemetry-attributes` and `--otlp-attributes` flags
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [38392]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
- Previously, all attribute values had to be strings wrapped in double quotes.
20+
- Now, unquoted integer values (e.g., `server.port=8000`) are correctly parsed as integers.
21+
- Ensures backward compatibility with existing string and boolean attributes.
22+
23+
# If your change doesn't affect end users or the exported elements of any package,
24+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
25+
# Optional: The change log or logs in which this entry should be included.
26+
# e.g. '[user]' or '[user, api]'
27+
# Include 'user' if the change is relevant to end users.
28+
# Include 'api' if there is a change to a library API.
29+
# Default: '[user]'
30+
change_logs: [user]

cmd/telemetrygen/internal/common/config.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
var (
18-
errFormatOTLPAttributes = fmt.Errorf("value should be of the format key=\"value\"")
18+
errFormatOTLPAttributes = fmt.Errorf("value should be in one of the following formats: key=\"value\", key=true, key=false, or key=<integer>")
1919
errDoubleQuotesOTLPAttributes = fmt.Errorf("value should be a string wrapped in double quotes")
2020
)
2121

@@ -46,6 +46,10 @@ func (v *KeyValue) Set(s string) error {
4646
(*v)[kv[0]] = false
4747
return nil
4848
}
49+
if intVal, err := strconv.Atoi(val); err == nil {
50+
(*v)[kv[0]] = intVal
51+
return nil
52+
}
4953
if len(val) < 2 || !strings.HasPrefix(val, "\"") || !strings.HasSuffix(val, "\"") {
5054
return errDoubleQuotesOTLPAttributes
5155
}
@@ -113,6 +117,8 @@ func (c *Config) GetAttributes() []attribute.KeyValue {
113117
attributes = append(attributes, attribute.String(k, v))
114118
case bool:
115119
attributes = append(attributes, attribute.Bool(k, v))
120+
case int:
121+
attributes = append(attributes, attribute.Int(k, v))
116122
}
117123
}
118124
}
@@ -129,6 +135,8 @@ func (c *Config) GetTelemetryAttributes() []attribute.KeyValue {
129135
attributes = append(attributes, attribute.String(k, v))
130136
case bool:
131137
attributes = append(attributes, attribute.Bool(k, v))
138+
case int:
139+
attributes = append(attributes, attribute.Int(k, v))
132140
}
133141
}
134142
}
@@ -170,15 +178,13 @@ func (c *Config) CommonFlags(fs *pflag.FlagSet) {
170178
`Flag may be repeated to set multiple headers (e.g --otlp-header key1=\"value1\" --otlp-header key2=\"value2\")`)
171179

172180
// custom resource attributes
173-
fs.Var(&c.ResourceAttributes, "otlp-attributes", "Custom resource attributes to use. The value is expected in the format key=\"value\". "+
174-
"You can use key=true or key=false. to set boolean attribute."+
181+
fs.Var(&c.ResourceAttributes, "otlp-attributes", "Custom telemetry attributes to use. The value is expected in one of the following formats: key=\"value\", key=true, key=false, or key=<integer>. "+
175182
"Note you may need to escape the quotes when using the tool from a cli. "+
176-
`Flag may be repeated to set multiple attributes (e.g --otlp-attributes key1=\"value1\" --otlp-attributes key2=\"value2\" --telemetry-attributes key3=true)`)
183+
`Flag may be repeated to set multiple attributes (e.g --otlp-attributes key1=\"value1\" --otlp-attributes key2=\"value2\" --telemetry-attributes key3=true --telemetry-attributes key4=123)`)
177184

178-
fs.Var(&c.TelemetryAttributes, "telemetry-attributes", "Custom telemetry attributes to use. The value is expected in the format key=\"value\". "+
179-
"You can use key=true or key=false. to set boolean attribute."+
185+
fs.Var(&c.TelemetryAttributes, "telemetry-attributes", "Custom telemetry attributes to use. The value is expected in one of the following formats: key=\"value\", key=true, key=false, or key=<integer>. "+
180186
"Note you may need to escape the quotes when using the tool from a cli. "+
181-
`Flag may be repeated to set multiple attributes (e.g --telemetry-attributes key1=\"value1\" --telemetry-attributes key2=\"value2\" --telemetry-attributes key3=true)`)
187+
`Flag may be repeated to set multiple attributes (e.g --telemetry-attributes key1=\"value1\" --telemetry-attributes key2=\"value2\" --telemetry-attributes key3=true --telemetry-attributes key4=123)`)
182188

183189
// TLS CA configuration
184190
fs.StringVar(&c.CaFile, "ca-cert", c.CaFile, "Trusted Certificate Authority to verify server certificate")

cmd/telemetrygen/internal/common/config_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ func TestKeyValueSet(t *testing.T) {
4343
flag: "key=false",
4444
expected: KeyValue(map[string]any{"key": false}),
4545
},
46+
{
47+
flag: "key=123",
48+
expected: KeyValue(map[string]any{"key": 123}),
49+
},
50+
{
51+
flag: "key=-456",
52+
expected: KeyValue(map[string]any{"key": -456}),
53+
},
54+
{
55+
flag: "key=0",
56+
expected: KeyValue(map[string]any{"key": 0}),
57+
},
58+
{
59+
flag: "key=12.34",
60+
err: errDoubleQuotesOTLPAttributes,
61+
},
4662
}
4763

4864
for _, tt := range tests {

0 commit comments

Comments
 (0)