Skip to content
Open
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
@@ -1,15 +1,25 @@
package datadog.trace.common.metrics;

import static datadog.trace.bootstrap.instrumentation.api.UTF8BytesString.EMPTY;

import datadog.trace.api.cache.DDCaches;
import datadog.trace.api.cache.DDCache;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.util.HashingUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/** The aggregation key for tracked metrics. */
public final class MetricKey {
static final DDCache<String, UTF8BytesString> RESOURCE_CACHE = DDCaches.newFixedSizeCache(32);
static final DDCache<String, UTF8BytesString> SERVICE_CACHE = DDCaches.newFixedSizeCache(8);
static final DDCache<String, UTF8BytesString> SERVICE_SOURCE_CACHE = DDCaches.newFixedSizeCache(4);
static final DDCache<String, UTF8BytesString> OPERATION_CACHE = DDCaches.newFixedSizeCache(64);
static final DDCache<String, UTF8BytesString> TYPE_CACHE = DDCaches.newFixedSizeCache(8);
static final DDCache<String, UTF8BytesString> KIND_CACHE = DDCaches.newFixedSizeCache(8);
static final DDCache<String, UTF8BytesString> HTTP_METHOD_CACHE = DDCaches.newFixedSizeCache(8);
static final DDCache<String, UTF8BytesString> HTTP_ENDPOINT_CACHE = DDCaches.newFixedSizeCache(32);

private final UTF8BytesString resource;
private final UTF8BytesString service;
private final UTF8BytesString serviceSource;
Expand Down Expand Up @@ -37,19 +47,19 @@ public MetricKey(
List<UTF8BytesString> 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 = utf8(RESOURCE_CACHE, resource);
this.service = utf8(SERVICE_CACHE, service);
this.serviceSource = utf8(SERVICE_SOURCE_CACHE, serviceSource);
this.operationName = utf8(OPERATION_CACHE, operationName);
this.type = utf8(TYPE_CACHE, type);
this.httpStatusCode = httpStatusCode;
this.synthetics = synthetics;
this.isTraceRoot = isTraceRoot;
this.spanKind = null == spanKind ? EMPTY : UTF8BytesString.create(spanKind);
this.spanKind = 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 = utf8(HTTP_METHOD_CACHE, httpMethod);
this.httpEndpoint = utf8(HTTP_ENDPOINT_CACHE, httpEndpoint);
int tmpHash = 0;
tmpHash = HashingUtils.addToHash(tmpHash, this.isTraceRoot);
tmpHash = HashingUtils.addToHash(tmpHash, this.spanKind);
Expand All @@ -65,6 +75,16 @@ public MetricKey(
tmpHash = HashingUtils.addToHash(tmpHash, this.httpMethod);
this.hash = tmpHash;
}

static UTF8BytesString utf8(DDCache<String, UTF8BytesString> cache, CharSequence charSeq) {
if ( charSeq == null ) {
return UTF8BytesString.EMPTY;
} else if ( charSeq instanceof UTF8BytesString ) {
return (UTF8BytesString)charSeq;
} else {
return cache.computeIfAbsent(charSeq.toString(), UTF8BytesString::create);
}
}

public UTF8BytesString getResource() {
return resource;
Expand Down
Loading