Skip to content
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

feat(telemetrygen): support integer attributes in telemetrygen tool (… #38410

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .chloggen/telemetrygen-attribute-support-integer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: telemetrygen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support integer values in `--telemetry-attributes` and `--otlp-attributes` flags

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [38392]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
- Previously, all attribute values had to be strings wrapped in double quotes.
- Now, unquoted integer values (e.g., `server.port=8000`) are correctly parsed as integers.
- Ensures backward compatibility with existing string and boolean attributes.

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
20 changes: 13 additions & 7 deletions cmd/telemetrygen/internal/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

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

Expand Down Expand Up @@ -46,6 +46,10 @@ func (v *KeyValue) Set(s string) error {
(*v)[kv[0]] = false
return nil
}
if intVal, err := strconv.Atoi(val); err == nil {
(*v)[kv[0]] = intVal
return nil
}
if len(val) < 2 || !strings.HasPrefix(val, "\"") || !strings.HasSuffix(val, "\"") {
return errDoubleQuotesOTLPAttributes
}
Expand Down Expand Up @@ -113,6 +117,8 @@ func (c *Config) GetAttributes() []attribute.KeyValue {
attributes = append(attributes, attribute.String(k, v))
case bool:
attributes = append(attributes, attribute.Bool(k, v))
case int:
attributes = append(attributes, attribute.Int(k, v))
}
}
}
Expand All @@ -129,6 +135,8 @@ func (c *Config) GetTelemetryAttributes() []attribute.KeyValue {
attributes = append(attributes, attribute.String(k, v))
case bool:
attributes = append(attributes, attribute.Bool(k, v))
case int:
attributes = append(attributes, attribute.Int(k, v))
}
}
}
Expand Down Expand Up @@ -170,15 +178,13 @@ func (c *Config) CommonFlags(fs *pflag.FlagSet) {
`Flag may be repeated to set multiple headers (e.g --otlp-header key1=\"value1\" --otlp-header key2=\"value2\")`)

// custom resource attributes
fs.Var(&c.ResourceAttributes, "otlp-attributes", "Custom resource attributes to use. The value is expected in the format key=\"value\". "+
"You can use key=true or key=false. to set boolean attribute."+
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>. "+
"Note you may need to escape the quotes when using the tool from a cli. "+
`Flag may be repeated to set multiple attributes (e.g --otlp-attributes key1=\"value1\" --otlp-attributes key2=\"value2\" --telemetry-attributes key3=true)`)
`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)`)

fs.Var(&c.TelemetryAttributes, "telemetry-attributes", "Custom telemetry attributes to use. The value is expected in the format key=\"value\". "+
"You can use key=true or key=false. to set boolean attribute."+
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>. "+
"Note you may need to escape the quotes when using the tool from a cli. "+
`Flag may be repeated to set multiple attributes (e.g --telemetry-attributes key1=\"value1\" --telemetry-attributes key2=\"value2\" --telemetry-attributes key3=true)`)
`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)`)

// TLS CA configuration
fs.StringVar(&c.CaFile, "ca-cert", c.CaFile, "Trusted Certificate Authority to verify server certificate")
Expand Down
16 changes: 16 additions & 0 deletions cmd/telemetrygen/internal/common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ func TestKeyValueSet(t *testing.T) {
flag: "key=false",
expected: KeyValue(map[string]any{"key": false}),
},
{
flag: "key=123",
expected: KeyValue(map[string]any{"key": 123}),
},
{
flag: "key=-456",
expected: KeyValue(map[string]any{"key": -456}),
},
{
flag: "key=0",
expected: KeyValue(map[string]any{"key": 0}),
},
{
flag: "key=12.34",
err: errDoubleQuotesOTLPAttributes,
},
}

for _, tt := range tests {
Expand Down