-
Notifications
You must be signed in to change notification settings - Fork 882
Prometheus label conversion refactored to align with spec #7291
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import static java.util.Objects.requireNonNull; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.AttributeType; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.sdk.common.InstrumentationScopeInfo; | ||
|
@@ -472,7 +473,9 @@ | |
|
||
Map<String, String> labelNameToValue = new HashMap<>(); | ||
attributes.forEach( | ||
(key, value) -> labelNameToValue.put(sanitizeLabelName(key.getKey()), value.toString())); | ||
(key, value) -> | ||
labelNameToValue.put( | ||
sanitizeLabelName(key.getKey()), toLabelValue(key.getType(), value))); | ||
|
||
for (int i = 0; i < additionalAttributes.length; i += 2) { | ||
labelNameToValue.putIfAbsent( | ||
|
@@ -642,4 +645,78 @@ | |
// Simple helper for a log message. | ||
return snapshot.getClass().getSimpleName().replace("Snapshot", "").toLowerCase(Locale.ENGLISH); | ||
} | ||
|
||
private static String toLabelValue(AttributeType type, Object attributeValue) { | ||
if (AttributeType.STRING.equals(type)) { | ||
return attributeValue.toString(); | ||
} else if (AttributeType.BOOLEAN.equals(type)) { | ||
return attributeValue.toString(); | ||
} else if (AttributeType.LONG.equals(type)) { | ||
return attributeValue.toString(); | ||
} else if (AttributeType.DOUBLE.equals(type)) { | ||
return attributeValue.toString(); | ||
} else if (AttributeType.STRING_ARRAY.equals(type)) { | ||
if (attributeValue instanceof List) { | ||
return ((List<?>) attributeValue) | ||
.stream() | ||
.map(String.class::cast) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about calling |
||
.map(Otel2PrometheusConverter::toJsonValidStr) | ||
.collect(Collectors.toList()) | ||
.toString(); | ||
} else { | ||
LOGGER.log( | ||
Check warning on line 667 in exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java
|
||
Level.WARNING, | ||
"Unexpected label value for AttributeType.STRING_ARRAY, toString() is being used as fallback value..."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also log |
||
return attributeValue.toString(); | ||
Check warning on line 670 in exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java
|
||
} | ||
} else if (AttributeType.BOOLEAN_ARRAY.equals(type)) { | ||
return attributeValue.toString(); | ||
} else if (AttributeType.LONG_ARRAY.equals(type)) { | ||
return attributeValue.toString(); | ||
} else if (AttributeType.DOUBLE_ARRAY.equals(type)) { | ||
return attributeValue.toString(); | ||
} else { | ||
throw new IllegalStateException(("Unrecognized AttributeType: " + type)); | ||
Check warning on line 679 in exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java
|
||
} | ||
} | ||
|
||
public static String toJsonValidStr(String str) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is only relevant for |
||
StringBuilder sb = new StringBuilder(); | ||
sb.append('"'); | ||
for (int i = 0; i < str.length(); i++) { | ||
char c = str.charAt(i); | ||
|
||
switch (c) { | ||
case '"': | ||
sb.append("\\\""); | ||
break; | ||
Check warning on line 692 in exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java
|
||
case '\\': | ||
sb.append("\\\\"); | ||
break; | ||
case '\b': | ||
sb.append("\\b"); | ||
break; | ||
case '\f': | ||
sb.append("\\f"); | ||
break; | ||
case '\n': | ||
sb.append("\\n"); | ||
break; | ||
case '\r': | ||
sb.append("\\r"); | ||
break; | ||
case '\t': | ||
sb.append("\\t"); | ||
break; | ||
default: | ||
if (c <= 0x1F) { | ||
sb.append(String.format(Locale.ROOT, "\\u%04X", (int) c)); | ||
} else { | ||
sb.append(c); | ||
} | ||
} | ||
} | ||
sb.append('"'); | ||
return sb.toString(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch
here? Many cases could be grouped into one