AppServerBridge.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. package io.opentelemetry.instrumentation.api.servlet;
  6. import io.opentelemetry.context.Context;
  7. import io.opentelemetry.context.ContextKey;
  8. import javax.annotation.Nullable;
  9. /**
  10. * Helper container for Context attributes for transferring certain information between servlet
  11. * integration and app-server server handler integrations.
  12. */
  13. public class AppServerBridge {
  14. private static final ContextKey<AppServerBridge> CONTEXT_KEY =
  15. ContextKey.named("opentelemetry-servlet-app-server-bridge");
  16. /**
  17. * Attach AppServerBridge to context.
  18. *
  19. * @param ctx server context
  20. * @return new context with AppServerBridge attached.
  21. */
  22. public static Context init(Context ctx) {
  23. return init(ctx, /* shouldRecordException= */ true);
  24. }
  25. /**
  26. * Attach AppServerBridge to context.
  27. *
  28. * @param ctx server context
  29. * @param shouldRecordException whether servlet integration should record exception thrown during
  30. * servlet invocation in server span. Use <code>false</code> on servers where exceptions
  31. * thrown during servlet invocation are propagated to the method where server span is closed
  32. * and can be added to server span there and <code>true</code> otherwise.
  33. * @return new context with AppServerBridge attached.
  34. */
  35. public static Context init(Context ctx, boolean shouldRecordException) {
  36. return ctx.with(AppServerBridge.CONTEXT_KEY, new AppServerBridge(shouldRecordException));
  37. }
  38. private final boolean servletShouldRecordException;
  39. private Throwable exception;
  40. private AppServerBridge(boolean shouldRecordException) {
  41. servletShouldRecordException = shouldRecordException;
  42. }
  43. /**
  44. * Returns true, if servlet integration should record exception thrown during servlet invocation
  45. * in server span. This method should return <code>false</code> on servers where exceptions thrown
  46. * during servlet invocation are propagated to the method where server span is closed and can be
  47. * added to server span there and <code>true</code> otherwise.
  48. *
  49. * @param context server context
  50. * @return <code>true</code>, if servlet integration should record exception thrown during servlet
  51. * invocation in server span, or <code>false</code> otherwise.
  52. */
  53. public static boolean shouldRecordException(Context context) {
  54. AppServerBridge appServerBridge = context.get(AppServerBridge.CONTEXT_KEY);
  55. if (appServerBridge != null) {
  56. return appServerBridge.servletShouldRecordException;
  57. }
  58. return true;
  59. }
  60. /**
  61. * Record exception that happened during servlet invocation so that app server instrumentation can
  62. * add it to server span.
  63. *
  64. * @param context server context
  65. * @param exception exception that happened during servlet invocation
  66. */
  67. public static void recordException(Context context, Throwable exception) {
  68. AppServerBridge appServerBridge = context.get(AppServerBridge.CONTEXT_KEY);
  69. if (appServerBridge != null && appServerBridge.servletShouldRecordException) {
  70. appServerBridge.exception = exception;
  71. }
  72. }
  73. /**
  74. * Get exception that happened during servlet invocation.
  75. *
  76. * @param context server context
  77. * @return exception that happened during servlet invocation
  78. */
  79. @Nullable
  80. public static Throwable getException(Context context) {
  81. AppServerBridge appServerBridge = context.get(AppServerBridge.CONTEXT_KEY);
  82. if (appServerBridge != null) {
  83. return appServerBridge.exception;
  84. }
  85. return null;
  86. }
  87. /**
  88. * Class used as key in CallDepthThreadLocalMap for counting servlet invocation depth in
  89. * Servlet3Advice and Servlet2Advice. We can not use helper classes like Servlet3Advice and
  90. * Servlet2Advice for determining call depth of server invocation because they can be injected
  91. * into multiple class loaders.
  92. *
  93. * @return class used as a key in CallDepthThreadLocalMap for counting servlet invocation depth
  94. */
  95. public static Class<?> getCallDepthKey() {
  96. class Key {}
  97. return Key.class;
  98. }
  99. }