Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
Expand Down Expand Up @@ -472,7 +474,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(
Expand Down Expand Up @@ -642,4 +646,76 @@
// Simple helper for a log message.
return snapshot.getClass().getSimpleName().replace("Snapshot", "").toLowerCase(Locale.ENGLISH);
}

private static String toLabelValue(AttributeType type, Object attributeValue) {
switch (type) {
case STRING:
case BOOLEAN:
case LONG:
case DOUBLE:
return attributeValue.toString();
case BOOLEAN_ARRAY:
case LONG_ARRAY:
case DOUBLE_ARRAY:
case STRING_ARRAY:
if (attributeValue instanceof List) {
return toJsonStr((List<?>) attributeValue);
} else {
throw new IllegalStateException(
String.format(

Check warning on line 665 in exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java

View check run for this annotation

Codecov / codecov/patch

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java#L664-L665

Added lines #L664 - L665 were not covered by tests
"Unexpected label value of %s for %s",
attributeValue.getClass().getName(), type.name()));

Check warning on line 667 in exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java

View check run for this annotation

Codecov / codecov/patch

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java#L667

Added line #L667 was not covered by tests
}
}
throw new IllegalStateException("Unrecognized AttributeType: " + type);

Check warning on line 670 in exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java

View check run for this annotation

Codecov / codecov/patch

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java#L670

Added line #L670 was not covered by tests
}

public static String toJsonStr(List<?> attributeValue) {
StringJoiner joiner = new StringJoiner(",", "[", "]");
for (int i = 0; i < attributeValue.size(); i++) {
Object value = attributeValue.get(i);
joiner.add(value instanceof String ? toJsonValidStr((String) value) : String.valueOf(value));
}
return joiner.toString();
}

public static String toJsonValidStr(String str) {
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;
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();
}
}
Loading
Loading