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
* @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;
// 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 SAMPLE_RATE_KEY = "_sample_rate";
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
/** 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() {
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) {
if (metrics.get() == null) {
metrics.compareAndSet(null, new ConcurrentHashMap<String, Number>());
metrics.compareAndSet(null, new ConcurrentHashMap<>(DEFAULT_METRICS));
}
if (value instanceof Float) {
metrics.get().put(key, value.doubleValue());

View File

@ -20,6 +20,7 @@ class DDSpanSerializationTest extends DDSpecification {
def jsonAdapter = new Moshi.Builder().build().adapter(Map)
final Map<String, Number> metrics = ["_sampling_priority_v1": 1]
metrics.putAll(DDSpanContext.DEFAULT_METRICS)
if (samplingPriority == PrioritySampling.UNSET) { // RateByServiceSampler sets priority
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"() {
expect:
span.metrics == [:]
span.metrics == DDSpanContext.DEFAULT_METRICS
when:
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:
span.metrics == result

View File

@ -1,5 +1,6 @@
package datadog.trace
import datadog.opentracing.DDSpanContext
import datadog.opentracing.SpanFactory
import datadog.trace.api.DDTags
import datadog.trace.util.test.DDSpecification
@ -18,7 +19,7 @@ class DDSpanContextTest extends DDSpecification {
context.serviceName == "fakeService"
context.resourceName == "fakeResource"
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:
name | extra | tags
@ -35,7 +36,7 @@ class DDSpanContextTest extends DDSpecification {
def thread = Thread.currentThread()
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:
context.getTags() == expectedTags
@ -61,7 +62,7 @@ class DDSpanContextTest extends DDSpecification {
(DDTags.THREAD_NAME): thread.name,
(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:
name | value
@ -98,4 +99,8 @@ class DDSpanContextTest extends DDSpecification {
Double | 0.5d
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.databind.ObjectMapper
import datadog.opentracing.DDSpanContext
import datadog.opentracing.SpanFactory
import datadog.trace.common.writer.ddagent.DDAgentApi
import datadog.trace.common.writer.ddagent.DDAgentResponseListener
@ -101,7 +102,8 @@ class DDAgentApiTest extends DDSpecification {
"duration" : 0,
"error" : 0,
"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",
"parent_id": 0,
"resource" : "fakeResource",
@ -115,7 +117,7 @@ class DDAgentApiTest extends DDSpecification {
"duration" : 0,
"error" : 0,
"meta" : ["thread.name": Thread.currentThread().getName(), "thread.id": "${Thread.currentThread().id}"],
"metrics" : [:],
"metrics" : ["_dd.measured": DDSpanContext.DD_MEASURED_DEFAULT],
"name" : "fakeOperation",
"parent_id": 0,
"resource" : "my-resource",

View File

@ -16,6 +16,6 @@ class LoggingWriterTest extends DDSpecification {
def "test toString"() {
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":{')
}
}