Flush metrics in Lambda wrapper too
MODIFY
instrumentation/aws-lambda-1.0/library/src/main/java/io/opentelemetry/instrumentation/awslambda/v1_0/LambdaUtils.java
instrumentation/aws-lambda-1.0/library/src/main/java/io/opentelemetry/instrumentation/awslambda/v1_0/LambdaUtils.java
package io.opentelemetry.instrumentation.awslambda.v1_0;
-import io.opentelemetry.api.GlobalOpenTelemetry;
-import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
+import io.opentelemetry.sdk.common.CompletableResultCode;
+import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
+import java.util.Arrays;
import java.util.concurrent.TimeUnit;
final class LambdaUtils {
- static void forceFlush() {
- OpenTelemetry openTelemetry = GlobalOpenTelemetry.get();
- if (openTelemetry instanceof OpenTelemetrySdk) {
- ((OpenTelemetrySdk) openTelemetry)
- .getSdkTracerProvider()
- .forceFlush()
- .join(1, TimeUnit.SECONDS);
- }
+ static void forceFlush(OpenTelemetrySdk openTelemetrySdk, long flushTimeout, TimeUnit unit) {
+ CompletableResultCode traceFlush = openTelemetrySdk.getSdkTracerProvider().forceFlush();
+ CompletableResultCode metricsFlush = IntervalMetricReader.forceFlushGlobal();
+ CompletableResultCode.ofAll(Arrays.asList(traceFlush, metricsFlush)).join(flushTimeout, unit);
}
private LambdaUtils() {}

MODIFY
instrumentation/aws-lambda-1.0/library/src/main/java/io/opentelemetry/instrumentation/awslambda/v1_0/TracingRequestHandler.java
instrumentation/aws-lambda-1.0/library/src/main/java/io/opentelemetry/instrumentation/awslambda/v1_0/TracingRequestHandler.java
@@ -84,10 +84,7 @@ public abstract class TracingRequestHandler<I, O> implements RequestHandler<I, O
} else {
tracer.end(otelContext);
}
- openTelemetrySdk
- .getSdkTracerProvider()
- .forceFlush()
- .join(flushTimeoutNanos, TimeUnit.NANOSECONDS);
+ LambdaUtils.forceFlush(openTelemetrySdk, flushTimeoutNanos, TimeUnit.NANOSECONDS);
}
}

MODIFY
instrumentation/aws-lambda-1.0/library/src/main/java/io/opentelemetry/instrumentation/awslambda/v1_0/TracingRequestStreamHandler.java
instrumentation/aws-lambda-1.0/library/src/main/java/io/opentelemetry/instrumentation/awslambda/v1_0/TracingRequestStreamHandler.java
@@ -72,10 +72,7 @@ public abstract class TracingRequestStreamHandler implements RequestStreamHandle
context);
} catch (Throwable t) {
tracer.endExceptionally(otelContext, t);
- openTelemetrySdk
- .getSdkTracerProvider()
- .forceFlush()
- .join(flushTimeoutNanos, TimeUnit.NANOSECONDS);
+ LambdaUtils.forceFlush(openTelemetrySdk, flushTimeoutNanos, TimeUnit.NANOSECONDS);
throw t;
}
}
@@ -122,10 +119,7 @@ public abstract class TracingRequestStreamHandler implements RequestStreamHandle
public void close() throws IOException {
delegate.close();
tracer.end(otelContext);
- openTelemetrySdk
- .getSdkTracerProvider()
- .forceFlush()
- .join(flushTimeoutNanos, TimeUnit.NANOSECONDS);
+ LambdaUtils.forceFlush(openTelemetrySdk, flushTimeoutNanos, TimeUnit.NANOSECONDS);
}
}
}

Flush metrics in Lambda wrapper too (#3748)