Skip to content

Commit

Permalink
Use codePointCount instead of length (open-telemetry#6770)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshitrjpt committed Oct 15, 2024
1 parent 2bd6126 commit 965a8fa
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
import io.prometheus.metrics.model.snapshots.SummarySnapshot.SummaryDataPointSnapshot;
import io.prometheus.metrics.model.snapshots.Unit;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -79,7 +78,7 @@ final class Otel2PrometheusConverter {
private static final String OTEL_SCOPE_VERSION = "otel_scope_version";
private static final long NANOS_PER_MILLISECOND = TimeUnit.MILLISECONDS.toNanos(1);
static final int MAX_CACHE_SIZE = 10;
static final int EXEMPLAR_MAX_RUNES = 128;
static final int EXEMPLAR_MAX_CODE_POINTS = 128;

private final boolean otelScopeEnabled;
@Nullable private final Predicate<String> allowedResourceAttributesFilter;
Expand Down Expand Up @@ -410,8 +409,8 @@ private Exemplar convertExemplar(double value, ExemplarData exemplar) {
if (spanContext.isValid()) {
labels =
convertAttributes(
null,
null,
null, // resource attributes are only copied for point's attributes
null, // scope attributes are only needed for point's attributes
exemplar.getFilteredAttributes(),
"trace_id",
spanContext.getTraceId(),
Expand All @@ -420,25 +419,27 @@ private Exemplar convertExemplar(double value, ExemplarData exemplar) {
} else {
labels = convertAttributes(null, null, exemplar.getFilteredAttributes());
}
int runes = getRunes(labels);
if (runes > EXEMPLAR_MAX_RUNES) {
int codePoints = getCodePoints(labels);
if (codePoints > EXEMPLAR_MAX_CODE_POINTS) {
THROTTLING_LOGGER.log(
Level.WARNING,
"exemplar labels have " + runes + " runes, exceeding the limit of " + EXEMPLAR_MAX_RUNES);
"exemplar labels have "
+ codePoints
+ " codePoints, exceeding the limit of "
+ EXEMPLAR_MAX_CODE_POINTS);
return null;
}
return new Exemplar(value, labels, exemplar.getEpochNanos() / NANOS_PER_MILLISECOND);
}

private static int getRunes(Labels labels) {
int runes = 0;
private static int getCodePoints(Labels labels) {
int codePoints = 0;
for (Label l : labels) {
runes +=
new String(l.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8).length()
+ new String(l.getValue().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)
.length();
codePoints +=
l.getName().codePointCount(0, l.getName().length())
+ l.getValue().codePointCount(0, l.getValue().length());
}
return runes;
return codePoints;
}

private InfoSnapshot makeTargetInfo(Resource resource) {
Expand Down

0 comments on commit 965a8fa

Please sign in to comment.