|
9 | 9 | import com.amazonaws.services.lambda.runtime.RequestHandler;
|
10 | 10 | import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
11 | 11 | import io.opentelemetry.api.trace.SpanKind;
|
12 |
| -import io.opentelemetry.api.trace.Tracer; |
13 | 12 | import io.opentelemetry.context.Scope;
|
| 13 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 14 | +import java.time.Duration; |
14 | 15 | import java.util.Collections;
|
15 | 16 | import java.util.Map;
|
| 17 | +import java.util.concurrent.TimeUnit; |
16 | 18 |
|
17 | 19 | /**
|
18 | 20 | * A base class similar to {@link RequestHandler} but will automatically trace invocations of {@link
|
|
21 | 23 | */
|
22 | 24 | public abstract class TracingRequestHandler<I, O> implements RequestHandler<I, O> {
|
23 | 25 |
|
24 |
| - private static final long DEFAULT_FLUSH_TIMEOUT_SECONDS = 1; |
| 26 | + protected static final Duration DEFAULT_FLUSH_TIMEOUT = Duration.ofSeconds(1); |
25 | 27 |
|
26 | 28 | private final AwsLambdaTracer tracer;
|
27 |
| - private final long flushTimeout; |
| 29 | + private final OpenTelemetrySdk openTelemetrySdk; |
| 30 | + private final long flushTimeoutNanos; |
28 | 31 |
|
29 |
| - /** Creates a new {@link TracingRequestHandler} which traces using the default {@link Tracer}. */ |
30 |
| - protected TracingRequestHandler(long flushTimeout) { |
31 |
| - this.tracer = new AwsLambdaTracer(); |
32 |
| - this.flushTimeout = flushTimeout; |
33 |
| - } |
34 |
| - |
35 |
| - /** Creates a new {@link TracingRequestHandler} which traces using the default {@link Tracer}. */ |
36 |
| - protected TracingRequestHandler() { |
37 |
| - this.tracer = new AwsLambdaTracer(); |
38 |
| - this.flushTimeout = DEFAULT_FLUSH_TIMEOUT_SECONDS; |
| 32 | + /** |
| 33 | + * Creates a new {@link TracingRequestHandler} which traces using the provided {@link |
| 34 | + * OpenTelemetrySdk} and has a timeout of 1s when flushing at the end of an invocation. |
| 35 | + */ |
| 36 | + protected TracingRequestHandler(OpenTelemetrySdk openTelemetrySdk) { |
| 37 | + this(openTelemetrySdk, DEFAULT_FLUSH_TIMEOUT); |
39 | 38 | }
|
40 | 39 |
|
41 | 40 | /**
|
42 |
| - * Creates a new {@link TracingRequestHandler} which traces using the specified {@link Tracer}. |
| 41 | + * Creates a new {@link TracingRequestHandler} which traces using the provided {@link |
| 42 | + * OpenTelemetrySdk} and has a timeout of {@code flushTimeout} when flushing at the end of an |
| 43 | + * invocation. |
43 | 44 | */
|
44 |
| - protected TracingRequestHandler(Tracer tracer) { |
45 |
| - this.tracer = new AwsLambdaTracer(tracer); |
46 |
| - this.flushTimeout = DEFAULT_FLUSH_TIMEOUT_SECONDS; |
| 45 | + protected TracingRequestHandler(OpenTelemetrySdk openTelemetrySdk, Duration flushTimeout) { |
| 46 | + this(openTelemetrySdk, flushTimeout, new AwsLambdaTracer(openTelemetrySdk)); |
47 | 47 | }
|
48 | 48 |
|
49 | 49 | /**
|
50 |
| - * Creates a new {@link TracingRequestHandler} which traces using the specified {@link |
51 |
| - * AwsLambdaTracer}. |
| 50 | + * Creates a new {@link TracingRequestHandler} which flushes the provided {@link |
| 51 | + * OpenTelemetrySdk}, has a timeout of {@code flushTimeout} when flushing at the end of an |
| 52 | + * invocation, and traces using the provided {@link AwsLambdaTracer}. |
52 | 53 | */
|
53 |
| - protected TracingRequestHandler(AwsLambdaTracer tracer) { |
| 54 | + protected TracingRequestHandler( |
| 55 | + OpenTelemetrySdk openTelemetrySdk, Duration flushTimeout, AwsLambdaTracer tracer) { |
| 56 | + this.openTelemetrySdk = openTelemetrySdk; |
| 57 | + this.flushTimeoutNanos = flushTimeout.toNanos(); |
54 | 58 | this.tracer = tracer;
|
55 |
| - this.flushTimeout = DEFAULT_FLUSH_TIMEOUT_SECONDS; |
56 | 59 | }
|
57 | 60 |
|
58 | 61 | private Map<String, String> getHeaders(I input) {
|
@@ -81,7 +84,10 @@ public final O handleRequest(I input, Context context) {
|
81 | 84 | } else {
|
82 | 85 | tracer.end(otelContext);
|
83 | 86 | }
|
84 |
| - LambdaUtils.forceFlush(); |
| 87 | + openTelemetrySdk |
| 88 | + .getSdkTracerProvider() |
| 89 | + .forceFlush() |
| 90 | + .join(flushTimeoutNanos, TimeUnit.NANOSECONDS); |
85 | 91 | }
|
86 | 92 | }
|
87 | 93 |
|
|
0 commit comments