Skip to content

Commit 36d6362

Browse files
committed
Add service name source to stats
1 parent 8bfae7a commit 36d6362

File tree

9 files changed

+96
-45
lines changed

9 files changed

+96
-45
lines changed

dd-trace-core/src/main/java/datadog/trace/common/metrics/ConflatingMetricsAggregator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ private boolean publish(CoreSpan<?> span, boolean isTopLevel, CharSequence spanK
331331
span.getResourceName(),
332332
SERVICE_NAMES.computeIfAbsent(span.getServiceName(), UTF8_ENCODE),
333333
span.getOperationName(),
334+
span.getServiceNameSource(),
334335
span.getType(),
335336
span.getHttpStatusCode(),
336337
isSynthetic(span),

dd-trace-core/src/main/java/datadog/trace/common/metrics/MetricKey.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
public final class MetricKey {
1111
private final UTF8BytesString resource;
1212
private final UTF8BytesString service;
13+
// note: it won't participate to equals/hashcode since not part of aggregation
14+
private final UTF8BytesString serviceSource;
1315
private final UTF8BytesString operationName;
1416
private final UTF8BytesString type;
1517
private final int httpStatusCode;
@@ -21,35 +23,11 @@ public final class MetricKey {
2123
private final UTF8BytesString httpMethod;
2224
private final UTF8BytesString httpEndpoint;
2325

24-
// Constructor without httpMethod and httpEndpoint for backward compatibility
25-
public MetricKey(
26-
CharSequence resource,
27-
CharSequence service,
28-
CharSequence operationName,
29-
CharSequence type,
30-
int httpStatusCode,
31-
boolean synthetics,
32-
boolean isTraceRoot,
33-
CharSequence spanKind,
34-
List<UTF8BytesString> peerTags) {
35-
this(
36-
resource,
37-
service,
38-
operationName,
39-
type,
40-
httpStatusCode,
41-
synthetics,
42-
isTraceRoot,
43-
spanKind,
44-
peerTags,
45-
null,
46-
null);
47-
}
48-
4926
public MetricKey(
5027
CharSequence resource,
5128
CharSequence service,
5229
CharSequence operationName,
30+
CharSequence serviceSource,
5331
CharSequence type,
5432
int httpStatusCode,
5533
boolean synthetics,
@@ -60,6 +38,7 @@ public MetricKey(
6038
CharSequence httpEndpoint) {
6139
this.resource = null == resource ? EMPTY : UTF8BytesString.create(resource);
6240
this.service = null == service ? EMPTY : UTF8BytesString.create(service);
41+
this.serviceSource = null == serviceSource ? null : UTF8BytesString.create(serviceSource);
6342
this.operationName = null == operationName ? EMPTY : UTF8BytesString.create(operationName);
6443
this.type = null == type ? EMPTY : UTF8BytesString.create(type);
6544
this.httpStatusCode = httpStatusCode;
@@ -101,6 +80,10 @@ public UTF8BytesString getService() {
10180
return service;
10281
}
10382

83+
public UTF8BytesString getServiceSource() {
84+
return serviceSource;
85+
}
86+
10487
public UTF8BytesString getOperationName() {
10588
return operationName;
10689
}

dd-trace-core/src/main/java/datadog/trace/common/metrics/SerializingMetricWriter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public final class SerializingMetricWriter implements MetricWriter {
3737
private static final byte[] PEER_TAGS = "PeerTags".getBytes(ISO_8859_1);
3838
private static final byte[] HTTP_METHOD = "HTTPMethod".getBytes(ISO_8859_1);
3939
private static final byte[] HTTP_ENDPOINT = "HTTPEndpoint".getBytes(ISO_8859_1);
40+
private static final byte[] SERVICE_SOURCE = "ServiceSource".getBytes(ISO_8859_1);
4041

4142
// Constant declared here for compile-time folding
4243
public static final int TRISTATE_TRUE = TriState.TRUE.serialValue;
@@ -109,7 +110,8 @@ public void add(MetricKey key, AggregateMetric aggregate) {
109110
// Calculate dynamic map size based on optional fields
110111
final boolean hasHttpMethod = key.getHttpMethod() != null;
111112
final boolean hasHttpEndpoint = key.getHttpEndpoint() != null;
112-
final int mapSize = 15 + (hasHttpMethod ? 1 : 0) + (hasHttpEndpoint ? 1 : 0);
113+
final boolean hasServiceSource = key.getServiceSource() != null;
114+
final int mapSize = 15 + (hasServiceSource ? 1 : 0) + (hasHttpMethod ? 1 : 0) + (hasHttpEndpoint ? 1 : 0);
113115

114116
writer.startMap(mapSize);
115117

@@ -145,6 +147,10 @@ public void add(MetricKey key, AggregateMetric aggregate) {
145147
writer.writeUTF8(peerTag);
146148
}
147149

150+
if (hasServiceSource) {
151+
writer.writeUTF8(SERVICE_SOURCE);
152+
writer.writeUTF8(key.getServiceSource());
153+
}
148154
// Only include HTTPMethod if present
149155
if (hasHttpMethod) {
150156
writer.writeUTF8(HTTP_METHOD);

dd-trace-core/src/main/java/datadog/trace/core/CoreSpan.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package datadog.trace.core;
22

3+
import static datadog.trace.api.DDTags.DD_SVC_SRC;
4+
35
import datadog.trace.api.DDTraceId;
46
import java.util.Map;
57

@@ -9,6 +11,18 @@ public interface CoreSpan<T extends CoreSpan<T>> {
911

1012
String getServiceName();
1113

14+
default CharSequence getServiceNameSource() {
15+
// overridden in DDSpan for a better implementation
16+
final Object obj = getTag(DD_SVC_SRC);
17+
if (obj == null) {
18+
return null;
19+
}
20+
if (obj instanceof CharSequence) {
21+
return (CharSequence) obj;
22+
}
23+
return obj.toString();
24+
}
25+
1226
CharSequence getOperationName();
1327

1428
CharSequence getResourceName();

dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ public void setServiceName(@Nonnull String serviceName, @Nonnull CharSequence so
590590
context.setServiceName(serviceName, source);
591591
}
592592

593+
@Override
594+
public CharSequence getServiceNameSource() {
595+
return context.getServiceNameSource();
596+
}
597+
593598
@Override
594599
public final DDSpan setResourceName(final CharSequence resourceName) {
595600
return setResourceName(resourceName, ResourceNamePriorities.DEFAULT);

dd-trace-core/src/test/groovy/datadog/trace/common/metrics/AggregateMetricTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class AggregateMetricTest extends DDSpecification {
6565
given:
6666
AggregateMetric aggregate = new AggregateMetric().recordDurations(3, new AtomicLongArray(0L, 0L, 0L | ERROR_TAG | TOP_LEVEL_TAG))
6767

68-
Batch batch = new Batch().reset(new MetricKey("foo", "bar", "qux", "type", 0, false, true, "corge", [UTF8BytesString.create("grault:quux")], null, null))
68+
Batch batch = new Batch().reset(new MetricKey("foo", "bar", "qux", null, "type", 0, false, true, "corge", [UTF8BytesString.create("grault:quux")], null, null))
6969
batch.add(0L, 10)
7070
batch.add(0L, 10)
7171
batch.add(0L, 10)
@@ -140,7 +140,7 @@ class AggregateMetricTest extends DDSpecification {
140140
def "consistent under concurrent attempts to read and write"() {
141141
given:
142142
AggregateMetric aggregate = new AggregateMetric()
143-
MetricKey key = new MetricKey("foo", "bar", "qux", "type", 0, false, true, "corge", [UTF8BytesString.create("grault:quux")], null, null)
143+
MetricKey key = new MetricKey("foo", "bar", "qux", null, "type", 0, false, true, "corge", [UTF8BytesString.create("grault:quux")], null, null)
144144
BlockingDeque<Batch> queue = new LinkedBlockingDeque<>(1000)
145145
ExecutorService reader = Executors.newSingleThreadExecutor()
146146
int writerCount = 10

0 commit comments

Comments
 (0)