Add Dropwizard-views instrumentation

This commit is contained in:
Tyler Benson 2018-10-03 11:32:04 -04:00
parent ebf0d86734
commit 14846a79df
9 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,25 @@
muzzle {
pass {
group = 'io.dropwizard'
module = 'dropwizard-views'
versions = "(,)"
}
}
apply from: "${rootDir}/gradle/java.gradle"
dependencies {
compileOnly group: 'io.dropwizard', name: 'dropwizard-views', version: '0.7.0'
compile project(':dd-java-agent:agent-tooling')
compile deps.bytebuddy
compile deps.opentracing
annotationProcessor deps.autoservice
implementation deps.autoservice
testCompile project(':dd-java-agent:testing')
testCompile group: 'io.dropwizard', name: 'dropwizard-views-freemarker', version: '0.7.0'
testCompile group: 'io.dropwizard', name: 'dropwizard-views-mustache', version: '0.7.0'
}

View File

@ -0,0 +1,82 @@
package datadog.trace.instrumentation.dropwizard.view;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static io.opentracing.log.Fields.ERROR_OBJECT;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags;
import io.dropwizard.views.View;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
@AutoService(Instrumenter.class)
public final class DropwizardViewInstrumentation extends Instrumenter.Default {
public DropwizardViewInstrumentation() {
super("dropwizard", "dropwizard-view");
}
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("io.dropwizard.views.ViewRenderer")));
}
@Override
public Map<ElementMatcher, String> transformers() {
final Map<ElementMatcher, String> transformers = new HashMap<>();
transformers.put(
isMethod()
.and(named("render"))
.and(takesArgument(0, named("io.dropwizard.views.View")))
.and(isPublic()),
RenderAdvice.class.getName());
return transformers;
}
public static class RenderAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static Scope startSpan(
@Advice.This final Object obj, @Advice.Argument(0) final View view) {
final Scope scope =
GlobalTracer.get()
.buildSpan("view.render")
.withTag(DDTags.RESOURCE_NAME, "View " + view.getTemplateName())
.withTag(Tags.COMPONENT.getKey(), "dropwizard-view")
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
.withTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_SERVER)
.withTag("span.origin.type", obj.getClass().getSimpleName())
.startActive(true);
return scope;
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final Scope scope, @Advice.Thrown final Throwable throwable) {
final Span span = scope.span();
if (throwable != null) {
Tags.ERROR.set(span, Boolean.TRUE);
span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
}
scope.close();
}
}
}

View File

@ -0,0 +1,46 @@
import datadog.trace.agent.test.AgentTestRunner
import io.dropwizard.views.View
import io.dropwizard.views.freemarker.FreemarkerViewRenderer
import io.dropwizard.views.mustache.MustacheViewRenderer
import java.nio.charset.StandardCharsets
import static datadog.trace.agent.test.asserts.ListWriterAssert.assertTraces
class ViewRenderTest extends AgentTestRunner {
def "render #template succeeds with span"() {
setup:
def outputStream = new ByteArrayOutputStream()
when:
renderer.render(view, Locale.ENGLISH, outputStream)
then:
outputStream.toString().contains("This is an example of a view")
assertTraces(TEST_WRITER, 1) {
trace(0, 1) {
span(0) {
resourceName "View $template"
operationName "view.render"
tags {
"component" "dropwizard-view"
"span.origin.type" renderer.class.simpleName
"span.kind" "server"
"span.type" "web"
defaultTags()
}
}
}
}
where:
renderer | template
new FreemarkerViewRenderer() | "/views/ftl/utf8.ftl"
new MustacheViewRenderer() | "/views/mustache/utf8.mustache"
new FreemarkerViewRenderer() | "/views/ftl/utf8.ftl"
new MustacheViewRenderer() | "/views/mustache/utf8.mustache"
view = new View(template, StandardCharsets.UTF_8) {}
}
}

View File

@ -0,0 +1,10 @@
<html>
<body>
<h1>This is an example of a view containing ISO-8859-1 characters</h1>
¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢
</body>
</html>

View File

@ -0,0 +1,9 @@
<html>
<body>
<h1>This is an example of a view containing UTF-8 characters</h1>
€€€€€€€€€€€€€€€€€€
</body>
</html>

View File

@ -0,0 +1,10 @@
<html>
<body>
<h1>This is an example of a view containing ISO-8859-1 characters</h1>
¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢
</body>
</html>

View File

@ -0,0 +1,9 @@
<html>
<body>
<h1>This is an example of a view containing UTF-8 characters</h1>
€€€€€€€€€€€€€€€€€€
</body>
</html>

View File

@ -0,0 +1 @@
apply from: "${rootDir}/gradle/java.gradle"

View File

@ -15,6 +15,8 @@ include ':dd-java-agent:instrumentation:aws-java-sdk-1.11.0'
include ':dd-java-agent:instrumentation:aws-java-sdk-1.11.106'
include ':dd-java-agent:instrumentation:couchbase-2.0'
include ':dd-java-agent:instrumentation:datastax-cassandra-2.3'
include ':dd-java-agent:instrumentation:dropwizard'
include ':dd-java-agent:instrumentation:dropwizard:dropwizard-views'
include ':dd-java-agent:instrumentation:elasticsearch-rest-5'
include ':dd-java-agent:instrumentation:elasticsearch-transport-2'
include ':dd-java-agent:instrumentation:elasticsearch-transport-5'