|
| 1 | +package datadog.trace.instrumentation.azurefunctions; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.declaresMethod; |
| 4 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.isAnnotatedWith; |
| 5 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 6 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; |
| 7 | +import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR; |
| 8 | +import static datadog.trace.instrumentation.azurefunctions.AzureFunctionsDecorator.DECORATE; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 12 | + |
| 13 | +import com.google.auto.service.AutoService; |
| 14 | +import com.microsoft.azure.functions.ExecutionContext; |
| 15 | +import com.microsoft.azure.functions.HttpRequestMessage; |
| 16 | +import com.microsoft.azure.functions.HttpResponseMessage; |
| 17 | +import datadog.trace.agent.tooling.Instrumenter; |
| 18 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 19 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 20 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 21 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext; |
| 22 | +import net.bytebuddy.asm.Advice; |
| 23 | +import net.bytebuddy.description.type.TypeDescription; |
| 24 | +import net.bytebuddy.matcher.ElementMatcher; |
| 25 | + |
| 26 | +@AutoService(InstrumenterModule.class) |
| 27 | +public class AzureFunctionsInstrumentation extends InstrumenterModule.Tracing |
| 28 | + implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice { |
| 29 | + public AzureFunctionsInstrumentation() { |
| 30 | + super("azure-functions"); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String hierarchyMarkerType() { |
| 35 | + /* |
| 36 | + Due to the class-loading in the Azure Function environment we cannot assume that |
| 37 | + "com.microsoft.azure.functions.annotation.FunctionName" will be visible (as in defined as a |
| 38 | + resource) for any types using that annotation |
| 39 | + */ |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public ElementMatcher<TypeDescription> hierarchyMatcher() { |
| 45 | + return declaresMethod( |
| 46 | + isAnnotatedWith(named("com.microsoft.azure.functions.annotation.FunctionName"))); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String[] helperClassNames() { |
| 51 | + return new String[] { |
| 52 | + packageName + ".AzureFunctionsDecorator", packageName + ".HttpRequestMessageExtractAdapter" |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + public void methodAdvice(MethodTransformer transformer) { |
| 57 | + transformer.applyAdvice( |
| 58 | + isMethod() |
| 59 | + .and(isPublic()) |
| 60 | + .and(takesArgument(0, named("com.microsoft.azure.functions.HttpRequestMessage"))) |
| 61 | + .and(takesArgument(1, named("com.microsoft.azure.functions.ExecutionContext"))), |
| 62 | + AzureFunctionsInstrumentation.class.getName() + "$AzureFunctionsAdvice"); |
| 63 | + } |
| 64 | + |
| 65 | + public static class AzureFunctionsAdvice { |
| 66 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 67 | + public static AgentScope methodEnter( |
| 68 | + @Advice.Argument(0) final HttpRequestMessage request, |
| 69 | + @Advice.Argument(1) final ExecutionContext context) { |
| 70 | + final AgentSpanContext.Extracted extractedContext = DECORATE.extract(request); |
| 71 | + final AgentSpan span = DECORATE.startSpan(request, extractedContext); |
| 72 | + DECORATE.afterStart(span, context.getFunctionName()); |
| 73 | + DECORATE.onRequest(span, request, request, extractedContext); |
| 74 | + HTTP_RESOURCE_DECORATOR.withRoute( |
| 75 | + span, request.getHttpMethod().name(), request.getUri().getPath()); |
| 76 | + return activateSpan(span); |
| 77 | + } |
| 78 | + |
| 79 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 80 | + public static void methodExit( |
| 81 | + @Advice.Enter final AgentScope scope, |
| 82 | + @Advice.Return final HttpResponseMessage response, |
| 83 | + @Advice.Thrown final Throwable throwable) { |
| 84 | + final AgentSpan span = scope.span(); |
| 85 | + DECORATE.onError(span, throwable); |
| 86 | + DECORATE.onResponse(span, response); |
| 87 | + DECORATE.beforeFinish(span); |
| 88 | + scope.close(); |
| 89 | + span.finish(); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments