diff --git a/dd-trace-core/src/main/java/datadog/trace/common/metrics/MetricKey.java b/dd-trace-core/src/main/java/datadog/trace/common/metrics/MetricKey.java index e10c081035c..0f8147faefa 100644 --- a/dd-trace-core/src/main/java/datadog/trace/common/metrics/MetricKey.java +++ b/dd-trace-core/src/main/java/datadog/trace/common/metrics/MetricKey.java @@ -2,6 +2,8 @@ import static datadog.trace.bootstrap.instrumentation.api.UTF8BytesString.EMPTY; +import datadog.trace.api.cache.DDCache; +import datadog.trace.api.cache.DDCaches; import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString; import datadog.trace.util.HashingUtils; import java.util.Collections; @@ -10,6 +12,17 @@ /** The aggregation key for tracked metrics. */ public final class MetricKey { + static final DDCache RESOURCE_CACHE = DDCaches.newFixedSizeCache(32); + static final DDCache SERVICE_CACHE = DDCaches.newFixedSizeCache(8); + static final DDCache SERVICE_SOURCE_CACHE = + DDCaches.newFixedSizeCache(16); + static final DDCache OPERATION_CACHE = DDCaches.newFixedSizeCache(64); + static final DDCache TYPE_CACHE = DDCaches.newFixedSizeCache(8); + static final DDCache KIND_CACHE = DDCaches.newFixedSizeCache(8); + static final DDCache HTTP_METHOD_CACHE = DDCaches.newFixedSizeCache(8); + static final DDCache HTTP_ENDPOINT_CACHE = + DDCaches.newFixedSizeCache(32); + private final UTF8BytesString resource; private final UTF8BytesString service; private final UTF8BytesString serviceSource; @@ -37,18 +50,18 @@ public MetricKey( List peerTags, CharSequence httpMethod, CharSequence httpEndpoint) { - this.resource = null == resource ? EMPTY : UTF8BytesString.create(resource); - this.service = null == service ? EMPTY : UTF8BytesString.create(service); - this.serviceSource = null == serviceSource ? null : UTF8BytesString.create(serviceSource); - this.operationName = null == operationName ? EMPTY : UTF8BytesString.create(operationName); - this.type = null == type ? EMPTY : UTF8BytesString.create(type); + this.resource = null == resource ? EMPTY : utf8(RESOURCE_CACHE, resource); + this.service = null == service ? EMPTY : utf8(SERVICE_CACHE, service); + this.serviceSource = null == serviceSource ? null : utf8(SERVICE_SOURCE_CACHE, serviceSource); + this.operationName = null == operationName ? EMPTY : utf8(OPERATION_CACHE, operationName); + this.type = null == type ? EMPTY : utf8(TYPE_CACHE, type); this.httpStatusCode = httpStatusCode; this.synthetics = synthetics; this.isTraceRoot = isTraceRoot; - this.spanKind = null == spanKind ? EMPTY : UTF8BytesString.create(spanKind); + this.spanKind = null == spanKind ? EMPTY : utf8(KIND_CACHE, spanKind); this.peerTags = peerTags == null ? Collections.emptyList() : peerTags; - this.httpMethod = httpMethod == null ? null : UTF8BytesString.create(httpMethod); - this.httpEndpoint = httpEndpoint == null ? null : UTF8BytesString.create(httpEndpoint); + this.httpMethod = httpMethod == null ? null : utf8(HTTP_METHOD_CACHE, httpMethod); + this.httpEndpoint = httpEndpoint == null ? null : utf8(HTTP_ENDPOINT_CACHE, httpEndpoint); int tmpHash = 0; tmpHash = HashingUtils.addToHash(tmpHash, this.isTraceRoot); @@ -66,6 +79,14 @@ public MetricKey( this.hash = tmpHash; } + static UTF8BytesString utf8(DDCache cache, CharSequence charSeq) { + if (charSeq instanceof UTF8BytesString) { + return (UTF8BytesString) charSeq; + } else { + return cache.computeIfAbsent(charSeq.toString(), UTF8BytesString::create); + } + } + public UTF8BytesString getResource() { return resource; }