add ..."metrics":{"_dd.measured":1}... to every span (#1336)

* add ..."metrics":{"_dd.measured":1}... to every span

* update tests to always have "metrics":{"_dd.measured":1} in every span

Authored-by: Lev Priima <lev.priima@data.dog>
This commit is contained in:
Lev Priima 2020-03-25 11:17:47 -07:00 committed by GitHub
parent ca48af1239
commit cda39adf50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 11 deletions

View File

@ -837,7 +837,7 @@ public class Config {
* @return * @return
* @deprecated This method should only be used internally. Use the explicit getter instead. * @deprecated This method should only be used internally. Use the explicit getter instead.
*/ */
public static String getSettingFromEnvironment(final String name, final String defaultValue) { private static String getSettingFromEnvironment(final String name, final String defaultValue) {
String value; String value;
// System properties and properties provided from command line have the highest precedence // System properties and properties provided from command line have the highest precedence

View File

@ -25,8 +25,11 @@ public class DDSpanContext implements io.opentracing.SpanContext {
public static final String PRIORITY_SAMPLING_KEY = "_sampling_priority_v1"; public static final String PRIORITY_SAMPLING_KEY = "_sampling_priority_v1";
public static final String SAMPLE_RATE_KEY = "_sample_rate"; public static final String SAMPLE_RATE_KEY = "_sample_rate";
public static final String ORIGIN_KEY = "_dd.origin"; public static final String ORIGIN_KEY = "_dd.origin";
public static final String DD_MEASURED = "_dd.measured";
public static final Number DD_MEASURED_DEFAULT = 1;
private static final Map<String, Number> EMPTY_METRICS = Collections.emptyMap(); private static final Map<String, Number> DEFAULT_METRICS =
Collections.singletonMap(DD_MEASURED, DD_MEASURED_DEFAULT);
// Shared with other span contexts // Shared with other span contexts
/** For technical reasons, the ref to the original tracer */ /** For technical reasons, the ref to the original tracer */
@ -307,12 +310,12 @@ public class DDSpanContext implements io.opentracing.SpanContext {
public Map<String, Number> getMetrics() { public Map<String, Number> getMetrics() {
final Map<String, Number> metrics = this.metrics.get(); final Map<String, Number> metrics = this.metrics.get();
return metrics == null ? EMPTY_METRICS : metrics; return metrics == null ? DEFAULT_METRICS : metrics;
} }
public void setMetric(final String key, final Number value) { public void setMetric(final String key, final Number value) {
if (metrics.get() == null) { if (metrics.get() == null) {
metrics.compareAndSet(null, new ConcurrentHashMap<String, Number>()); metrics.compareAndSet(null, new ConcurrentHashMap<>(DEFAULT_METRICS));
} }
if (value instanceof Float) { if (value instanceof Float) {
metrics.get().put(key, value.doubleValue()); metrics.get().put(key, value.doubleValue());

View File

@ -20,6 +20,7 @@ class DDSpanSerializationTest extends DDSpecification {
def jsonAdapter = new Moshi.Builder().build().adapter(Map) def jsonAdapter = new Moshi.Builder().build().adapter(Map)
final Map<String, Number> metrics = ["_sampling_priority_v1": 1] final Map<String, Number> metrics = ["_sampling_priority_v1": 1]
metrics.putAll(DDSpanContext.DEFAULT_METRICS)
if (samplingPriority == PrioritySampling.UNSET) { // RateByServiceSampler sets priority if (samplingPriority == PrioritySampling.UNSET) { // RateByServiceSampler sets priority
metrics.put("_dd.agent_psr", 1.0d) metrics.put("_dd.agent_psr", 1.0d)
} }

View File

@ -279,10 +279,13 @@ class SpanDecoratorTest extends DDSpecification {
def "span metrics starts empty but added with rate limiting value of #rate"() { def "span metrics starts empty but added with rate limiting value of #rate"() {
expect: expect:
span.metrics == [:] span.metrics == DDSpanContext.DEFAULT_METRICS
when: when:
span.setTag(ANALYTICS_SAMPLE_RATE, rate) span.setTag(ANALYTICS_SAMPLE_RATE, rate)
// these 2 lines to avoid checking for {@code DDSpanContext.DD_MEASURED} for every metric:
span.context().setMetric(DDSpanContext.DD_MEASURED, 42)
span.metrics.remove(DDSpanContext.DD_MEASURED, 42)
then: then:
span.metrics == result span.metrics == result

View File

@ -1,5 +1,6 @@
package datadog.trace package datadog.trace
import datadog.opentracing.DDSpanContext
import datadog.opentracing.SpanFactory import datadog.opentracing.SpanFactory
import datadog.trace.api.DDTags import datadog.trace.api.DDTags
import datadog.trace.util.test.DDSpecification import datadog.trace.util.test.DDSpecification
@ -18,7 +19,7 @@ class DDSpanContextTest extends DDSpecification {
context.serviceName == "fakeService" context.serviceName == "fakeService"
context.resourceName == "fakeResource" context.resourceName == "fakeResource"
context.spanType == "fakeType" context.spanType == "fakeType"
context.toString() == "DDSpan [ t_id=1, s_id=1, p_id=0] trace=fakeService/fakeOperation/fakeResource metrics={} *errored* tags={${extra}${tags.containsKey(DDTags.SPAN_TYPE) ? "span.type=${context.getSpanType()}, " : ""}thread.id=${Thread.currentThread().id}, thread.name=${Thread.currentThread().name}}" context.toString() == "DDSpan [ t_id=1, s_id=1, p_id=0] trace=fakeService/fakeOperation/fakeResource metrics=${defaultMetrics()} *errored* tags={${extra}${tags.containsKey(DDTags.SPAN_TYPE) ? "span.type=${context.getSpanType()}, " : ""}thread.id=${Thread.currentThread().id}, thread.name=${Thread.currentThread().name}}"
where: where:
name | extra | tags name | extra | tags
@ -35,7 +36,7 @@ class DDSpanContextTest extends DDSpecification {
def thread = Thread.currentThread() def thread = Thread.currentThread()
def expectedTags = [(DDTags.THREAD_NAME): thread.name, (DDTags.THREAD_ID): thread.id] def expectedTags = [(DDTags.THREAD_NAME): thread.name, (DDTags.THREAD_ID): thread.id]
def expectedTrace = "DDSpan [ t_id=1, s_id=1, p_id=0] trace=$details metrics={} tags={thread.id=$thread.id, thread.name=$thread.name}" def expectedTrace = "DDSpan [ t_id=1, s_id=1, p_id=0] trace=$details metrics=${defaultMetrics()} tags={thread.id=$thread.id, thread.name=$thread.name}"
expect: expect:
context.getTags() == expectedTags context.getTags() == expectedTags
@ -61,7 +62,7 @@ class DDSpanContextTest extends DDSpecification {
(DDTags.THREAD_NAME): thread.name, (DDTags.THREAD_NAME): thread.name,
(DDTags.THREAD_ID) : thread.id (DDTags.THREAD_ID) : thread.id
] ]
context.toString() == "DDSpan [ t_id=1, s_id=1, p_id=0] trace=fakeService/fakeOperation/fakeResource metrics={} tags={$name=$value, thread.id=$thread.id, thread.name=$thread.name}" context.toString() == "DDSpan [ t_id=1, s_id=1, p_id=0] trace=fakeService/fakeOperation/fakeResource metrics=${defaultMetrics()} tags={$name=$value, thread.id=$thread.id, thread.name=$thread.name}"
where: where:
name | value name | value
@ -98,4 +99,8 @@ class DDSpanContextTest extends DDSpecification {
Double | 0.5d Double | 0.5d
Integer | 0x55 Integer | 0x55
} }
static String defaultMetrics() {
return DDSpanContext.DEFAULT_METRICS
}
} }

View File

@ -2,6 +2,7 @@ package datadog.trace.api.writer
import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import datadog.opentracing.DDSpanContext
import datadog.opentracing.SpanFactory import datadog.opentracing.SpanFactory
import datadog.trace.common.writer.ddagent.DDAgentApi import datadog.trace.common.writer.ddagent.DDAgentApi
import datadog.trace.common.writer.ddagent.DDAgentResponseListener import datadog.trace.common.writer.ddagent.DDAgentResponseListener
@ -101,7 +102,8 @@ class DDAgentApiTest extends DDSpecification {
"duration" : 0, "duration" : 0,
"error" : 0, "error" : 0,
"meta" : ["thread.name": Thread.currentThread().getName(), "thread.id": "${Thread.currentThread().id}"], "meta" : ["thread.name": Thread.currentThread().getName(), "thread.id": "${Thread.currentThread().id}"],
"metrics" : [:], //TODO : use DDSpanContext.DD_MEASURED as a key
"metrics" : ["_dd.measured": DDSpanContext.DD_MEASURED_DEFAULT],
"name" : "fakeOperation", "name" : "fakeOperation",
"parent_id": 0, "parent_id": 0,
"resource" : "fakeResource", "resource" : "fakeResource",
@ -115,7 +117,7 @@ class DDAgentApiTest extends DDSpecification {
"duration" : 0, "duration" : 0,
"error" : 0, "error" : 0,
"meta" : ["thread.name": Thread.currentThread().getName(), "thread.id": "${Thread.currentThread().id}"], "meta" : ["thread.name": Thread.currentThread().getName(), "thread.id": "${Thread.currentThread().id}"],
"metrics" : [:], "metrics" : ["_dd.measured": DDSpanContext.DD_MEASURED_DEFAULT],
"name" : "fakeOperation", "name" : "fakeOperation",
"parent_id": 0, "parent_id": 0,
"resource" : "my-resource", "resource" : "my-resource",

View File

@ -16,6 +16,6 @@ class LoggingWriterTest extends DDSpecification {
def "test toString"() { def "test toString"() {
expect: expect:
writer.toString(sampleTrace).startsWith('[{"service":"fakeService","name":"fakeOperation","resource":"fakeResource","trace_id":1,"span_id":1,"parent_id":0,"start":1000,"duration":0,"type":"fakeType","error":0,"metrics":{},"meta":{') writer.toString(sampleTrace).startsWith('[{"service":"fakeService","name":"fakeOperation","resource":"fakeResource","trace_id":1,"span_id":1,"parent_id":0,"start":1000,"duration":0,"type":"fakeType","error":0,"metrics":{"_dd.measured":1},"meta":{')
} }
} }