Sanitize Dropwizard metric names to comply with OpenTelemetry spec#15954
Sanitize Dropwizard metric names to comply with OpenTelemetry spec#15954
Conversation
Co-authored-by: trask <[email protected]>
Co-authored-by: trask <[email protected]>
| /** | ||
| * Sanitizes instrument names to comply with OpenTelemetry specification. | ||
| * Instrument names must consist of alphanumeric characters, '_', '.', '-', '/', | ||
| * and must start with a letter. | ||
| * Invalid characters are replaced with '_', and consecutive underscores are collapsed. | ||
| * If the name doesn't start with a letter, 'metric_' is prepended. | ||
| * | ||
| * @param name the original metric name from Dropwizard | ||
| * @return the sanitized instrument name | ||
| */ |
There was a problem hiding this comment.
@copilot keep the logic simple, and just strip invalid characters. If there are no characters left, then throw exception.
There was a problem hiding this comment.
Simplified the logic in c9ae12a:
- Now strips invalid characters instead of replacing with
_ - Throws
IllegalArgumentExceptionif name is null, empty, contains no valid characters, or doesn't start with a letter after sanitization - Removed the logic for prepending
metric_and collapsing consecutive underscores
There was a problem hiding this comment.
Returning empty string could be weird as probably then the sdk will log a warning that empty string is not a valid name. Perhaps when sanitization fails to come up with a name we should skip creating the instrument? Maybe we should log warning? Maybe we should log something when sanitization changes the name of the instrument?
There was a problem hiding this comment.
when sanitization fails to come up with a name (it's empty) log a warning and should skip creating the instrument
also log a warning any time that sanitization changes the name of the instrument
There was a problem hiding this comment.
Done in 73747cf:
- When sanitization fails (returns null), logs a warning and skips instrument creation
- When sanitization changes the name, logs a warning with both original and sanitized names
- All tests pass with visible warning logs for the sanitized metric names
…, throw exception for invalid names Co-authored-by: trask <[email protected]>
Co-authored-by: trask <[email protected]>
…l characters Co-authored-by: trask <[email protected]>
| */ | ||
| private static String sanitizeInstrumentName(String name) { | ||
| if (name == null || name.isEmpty()) { | ||
| throw new IllegalArgumentException("Metric name cannot be null or empty"); |
There was a problem hiding this comment.
we shouldn't throw exceptions from here as they'll propagate into application code
There was a problem hiding this comment.
good point, I checked SDK and it will handle this and return noop instrument
@copilot don't throw exceptions from this method, returning empty string is ok
There was a problem hiding this comment.
i wonder if copilot is referencing this or not, or if we need to use different language
There was a problem hiding this comment.
Done in 54b96e5 - now returns empty string instead of throwing exceptions, which allows the SDK to handle invalid names and return noop instruments
There was a problem hiding this comment.
Done in 54b96e5 - now returns empty string instead of throwing exceptions
There was a problem hiding this comment.
i wonder if copilot is referencing this or not, or if we need to use different language
the problem is me #15954 (comment)
…eptions Co-authored-by: trask <[email protected]>
Co-authored-by: trask <[email protected]>
…r invalid names Co-authored-by: trask <[email protected]>
Co-authored-by: trask <[email protected]>
Fix illegal characters when bridging from Dropwizard metrics
Based on issue #15931 and OpenTelemetry specification for instrument name syntax:
Plan:
Summary:
Fixed issue where Dropwizard metric names with illegal characters (like single quotes in
jvm.memory.pools.CodeHeap-'non-profiled-nmethods'.used) were causing OpenTelemetry validation errors. The solution sanitizes metric names by stripping illegal characters using a precompiled regex Pattern. For invalid names (null, empty, no valid characters, or doesn't start with a letter), logs a warning and skips instrument creation entirely. Also logs warnings when sanitization changes a metric name or when metric names exceed 255 characters and are truncated, providing full visibility into which metrics are affected by the sanitization process.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.