Skip to content

Commit

Permalink
Merge branch 'master' into haystack
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstartin committed Jul 31, 2020
2 parents 6e7d844 + 5451818 commit a444001
Show file tree
Hide file tree
Showing 177 changed files with 3,669 additions and 1,103 deletions.
23 changes: 16 additions & 7 deletions .palantir/revapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,6 @@ acceptedBreaks:
- code: "java.class.removed"
old: "interface datadog.trace.common.writer.ddagent.TraceBuffer"
justification: "internal api"
- code: "java.method.removed"
old: "method datadog.trace.common.writer.DDAgentWriter.DDAgentWriterBuilder\
\ datadog.trace.common.writer.DDAgentWriter.DDAgentWriterBuilder::serializer(datadog.trace.common.writer.ddagent.StatefulSerializer)"
justification: "internal api"
- code: "java.method.removed"
old: "method void datadog.trace.common.writer.ddagent.Monitor::onBackedUpTraceBuffer()"
justification: "internal api"
- code: "java.field.removedWithConstant"
old: "field datadog.trace.bootstrap.instrumentation.api.Tags.DD_ENV"
justification: "reverting log changes"
Expand All @@ -530,3 +523,19 @@ acceptedBreaks:
- code: "java.field.removedWithConstant"
old: "field datadog.trace.bootstrap.instrumentation.api.Tags.DD_VERSION"
justification: "reverting log changes"
- code: "java.method.removed"
old: "method datadog.trace.common.writer.DDAgentWriter.DDAgentWriterBuilder\
\ datadog.trace.common.writer.DDAgentWriter.DDAgentWriterBuilder::serializer(datadog.trace.common.writer.ddagent.StatefulSerializer)"
justification: "internal api"
- code: "java.method.removed"
old: "method void datadog.trace.common.writer.ddagent.Monitor::onBackedUpTraceBuffer()"
justification: "internal api"
"0.58.0":
com.datadoghq:dd-trace-ot:
- code: "java.class.removed"
old: "class datadog.trace.core.propagation.HaystackHttpCodec.Extractor"
justification: "internal api"
- code: "java.class.removed"
old: "interface datadog.trace.bootstrap.instrumentation.api.AgentPropagation.Getter<C\
\ extends java.lang.Object>"
justification: "internal api"
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package datadog.trace.bootstrap.instrumentation.api;

import java.util.Map;

public final class ContextVisitors {

private static final MapContextVisitor<?> MAP_CONTEXT_VISITOR = new MapContextVisitor<>();
private static final EntrySetContextVisitor<?> ENTRY_SET_CONTEXT_VISITOR =
new EntrySetContextVisitor<>();

@SuppressWarnings("unchecked")
public static <T extends Map<String, ? extends Object>>
AgentPropagation.ContextVisitor<T> objectValuesMap() {
return (AgentPropagation.ContextVisitor<T>) MAP_CONTEXT_VISITOR;
}

@SuppressWarnings("unchecked")
public static <T extends Map<String, String>>
AgentPropagation.ContextVisitor<T> stringValuesMap() {
return (AgentPropagation.ContextVisitor<T>) MAP_CONTEXT_VISITOR;
}

@SuppressWarnings("unchecked")
public static <T extends Iterable<Map.Entry<String, ? extends Object>>>
AgentPropagation.ContextVisitor<T> objectValuesEntrySet() {
return (AgentPropagation.ContextVisitor<T>) ENTRY_SET_CONTEXT_VISITOR;
}

@SuppressWarnings("unchecked")
public static <T extends Iterable<Map.Entry<String, String>>>
AgentPropagation.ContextVisitor<T> stringValuesEntrySet() {
return (AgentPropagation.ContextVisitor<T>) ENTRY_SET_CONTEXT_VISITOR;
}

private static final class MapContextVisitor<T extends Map<String, ? extends Object>>
implements AgentPropagation.ContextVisitor<T> {

@Override
public void forEachKey(T carrier, AgentPropagation.KeyClassifier classifier) {
for (Map.Entry<String, ? extends Object> entry : carrier.entrySet()) {
if (null != entry.getValue()
&& !classifier.accept(entry.getKey(), entry.getValue().toString())) {
return;
}
}
}
}

private static final class EntrySetContextVisitor<
T extends Iterable<Map.Entry<String, ? extends Object>>>
implements AgentPropagation.ContextVisitor<T> {

@Override
public void forEachKey(T carrier, AgentPropagation.KeyClassifier classifier) {
for (Map.Entry<String, ? extends Object> entry : carrier) {
if (null != entry.getValue()
&& !classifier.accept(entry.getKey(), entry.getValue().toString())) {
return;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public String create(String key) {
}
}

public static class LowerCase implements Creator<String, String> {

@Override
public String create(String key) {
return key.toLowerCase();
}
}

static final int MAXIMUM_CAPACITY = 1 << 30;

private static final class Node<K, V> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package datadog.trace.bootstrap.instrumentation.decorator;

import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class TestDecorator extends BaseDecorator {
public static final String TEST_PASS = "PASS";
public static final String TEST_FAIL = "FAIL";
public static final String TEST_SKIP = "SKIP";

protected abstract String testFramework();

protected String spanKind() {
return Tags.SPAN_KIND_TEST;
}

@Override
protected String spanType() {
return DDSpanTypes.TEST;
}

@Override
public AgentSpan afterStart(final AgentSpan span) {
assert span != null;
span.setTag(Tags.SPAN_KIND, spanKind());
span.setTag(DDTags.SPAN_TYPE, spanType());
span.setTag(DDTags.TEST_FRAMEWORK, testFramework());
return super.afterStart(span);
}

public List<String> testNames(
final Class<?> testClass, final Class<? extends Annotation> testAnnotation) {
final List<String> testNames = new ArrayList<>();

final Method[] methods = testClass.getMethods();
for (final Method method : methods) {
if (method.getAnnotation(testAnnotation) != null) {
testNames.add(method.getName());
}
}
return testNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package datadog.trace.bootstrap.instrumentation.decorator

import datadog.trace.api.DDTags
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
import datadog.trace.bootstrap.instrumentation.api.Tags

class TestDecoratorTest extends BaseDecoratorTest {

def span = Mock(AgentSpan)

def "test afterStart"() {
setup:
def decorator = newDecorator()

when:
decorator.afterStart(span)

then:
1 * span.setTag(Tags.COMPONENT, "test-component")
1 * span.setTag(Tags.SPAN_KIND, decorator.spanKind())
1 * span.setTag(DDTags.SPAN_TYPE, decorator.spanType())
1 * span.setTag(DDTags.TEST_FRAMEWORK, decorator.testFramework())
_ * span.setTag(_, _) // Want to allow other calls from child implementations.
_ * span.setServiceName(_)
_ * span.setOperationName(_)
0 * _

where:
serviceName << ["test-service", "other-service", null]
}

def "test beforeFinish"() {
when:
newDecorator().beforeFinish(span)

then:
0 * _
}


@Override
def newDecorator() {
return new TestDecorator() {
@Override
protected String testFramework() {
return "test-framework"
}

@Override
protected String[] instrumentationNames() {
return ["test1", "test2"]
}

@Override
protected String spanType() {
return "test-type"
}

@Override
protected String spanKind() {
return "test-type"
}

@Override
protected String component() {
return "test-component"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ private static void deleteTempDir(final File file) {
final boolean deleted = file.delete();
if (!deleted) {
file.deleteOnExit();
log.debug("file '{}' added to shutdown delete hook", file);
} else {
log.debug("file '{}' deleted", file);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ public boolean matches(final T target) {
if (name.equals("ch.qos.logback.core.AsyncAppenderBase$Worker")) {
return false;
}
// for inserting service, env, version in MDC of every thread
if (name.equals("ch.qos.logback.classic.util.LogbackMDCAdapter")) {
return false;
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;

import datadog.trace.api.Config;
import datadog.trace.api.CorrelationIdentifier;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.context.ScopeListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

/**
Expand Down Expand Up @@ -50,4 +56,34 @@ public void afterScopeClosed() {
log.debug("Exception removing log context context", e);
}
}

static final Map<String, String> LOG_CONTEXT_DD_TAGS;

static {
Map<String, String> logContextDDTags = new HashMap<>();
logContextDDTags.put(Tags.DD_SERVICE, Config.get().getServiceName());
final Map<String, String> mergedSpanTags = Config.get().getMergedSpanTags();
String version = "";
String env = "";
if (mergedSpanTags != null) {
version = mergedSpanTags.get("version");
if (version == null) {
version = "";
}
env = mergedSpanTags.get("env");
if (env == null) {
env = "";
}
}
logContextDDTags.put(Tags.DD_VERSION, version);
logContextDDTags.put(Tags.DD_ENV, env);
LOG_CONTEXT_DD_TAGS = Collections.unmodifiableMap(logContextDDTags);
}

public static void addDDTagsToMDC(final Method putMethod)
throws InvocationTargetException, IllegalAccessException {
for (final Map.Entry<String, String> e : LOG_CONTEXT_DD_TAGS.entrySet()) {
putMethod.invoke(null, e.getKey(), e.getValue());
}
}
}
Loading

0 comments on commit a444001

Please sign in to comment.