DemoServlet3InstrumentationModule.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. package com.example.javaagent.instrumentation;
  6. import static java.util.Collections.singletonList;
  7. import com.google.auto.service.AutoService;
  8. import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
  9. import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  10. import io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers;
  11. import java.util.List;
  12. import net.bytebuddy.matcher.ElementMatcher;
  13. /**
  14. * This is a demo instrumentation which hooks into servlet invocation and modifies the http
  15. * response.
  16. */
  17. @AutoService(InstrumentationModule.class)
  18. public final class DemoServlet3InstrumentationModule extends InstrumentationModule {
  19. public DemoServlet3InstrumentationModule() {
  20. super("servlet-demo", "servlet-3");
  21. }
  22. /*
  23. We want this instrumentation to be applied after the standard servlet instrumentation.
  24. The latter creates a server span around http request.
  25. This instrumentation needs access to that server span.
  26. */
  27. @Override
  28. public int order() {
  29. return 1;
  30. }
  31. @Override
  32. public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
  33. return AgentElementMatchers.hasClassesNamed("javax.servlet.http.HttpServlet");
  34. }
  35. @Override
  36. public List<TypeInstrumentation> typeInstrumentations() {
  37. return singletonList(new DemoServlet3Instrumentation());
  38. }
  39. }