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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be already filled with UTF8BytesString. It can take values of the instrumentation name (that's should also be UTF8BytesString) so I suggest removing caching for this specific one. 4, is also too small

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);
Comment on lines +18 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to widen those a bit? Also, for the http endpoint, I'm wondering if we should just do that caching earlier to make other serialisation benefitting of this (i.e. in EndpointResolver)


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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are things that are supposing to check nullity of this. Now if we replace with empty is not more the same semantic and this will break some code. I suggest to let the caller return the default value so it won't break the existing logic

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