Skip to content

Commit 6a9654d

Browse files
laurittrask
andauthored
Unify aws lambda flush handling (open-telemetry#12576)
Co-authored-by: Trask Stalnaker <[email protected]>
1 parent a0abd63 commit 6a9654d

File tree

9 files changed

+58
-18
lines changed

9 files changed

+58
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Settings for the AWS Lambda Instrumentation
2+
3+
| System property | Type | Default | Description |
4+
|-------------------------------------------------|---------|---------|--------------------------------|
5+
| `otel.instrumentation.aws-lambda.flush-timeout` | Integer | 10000 | Flush timeout in milliseconds. |

instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaInstrumentationHelper.java

+13
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,28 @@
88
import io.opentelemetry.api.GlobalOpenTelemetry;
99
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.AwsLambdaFunctionInstrumenter;
1010
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.AwsLambdaFunctionInstrumenterFactory;
11+
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.WrapperConfiguration;
12+
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
13+
import java.time.Duration;
1114

1215
public final class AwsLambdaInstrumentationHelper {
1316

1417
private static final AwsLambdaFunctionInstrumenter FUNCTION_INSTRUMENTER =
1518
AwsLambdaFunctionInstrumenterFactory.createInstrumenter(GlobalOpenTelemetry.get());
19+
private static final Duration FLUSH_TIMEOUT =
20+
Duration.ofMillis(
21+
AgentInstrumentationConfig.get()
22+
.getLong(
23+
"otel.instrumentation.aws-lambda.flush-timeout",
24+
WrapperConfiguration.OTEL_LAMBDA_FLUSH_TIMEOUT_DEFAULT.toMillis()));
1625

1726
public static AwsLambdaFunctionInstrumenter functionInstrumenter() {
1827
return FUNCTION_INSTRUMENTER;
1928
}
2029

30+
public static Duration flushTimeout() {
31+
return FLUSH_TIMEOUT;
32+
}
33+
2134
private AwsLambdaInstrumentationHelper() {}
2235
}

instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaRequestHandlerInstrumentation.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
99
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
10+
import static io.opentelemetry.javaagent.instrumentation.awslambdacore.v1_0.AwsLambdaInstrumentationHelper.flushTimeout;
1011
import static io.opentelemetry.javaagent.instrumentation.awslambdacore.v1_0.AwsLambdaInstrumentationHelper.functionInstrumenter;
1112
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1213
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
@@ -90,7 +91,7 @@ public static void stopSpan(
9091
functionInstrumenter().end(functionContext, input, null, throwable);
9192
}
9293

93-
OpenTelemetrySdkAccess.forceFlush(1, TimeUnit.SECONDS);
94+
OpenTelemetrySdkAccess.forceFlush(flushTimeout().toNanos(), TimeUnit.NANOSECONDS);
9495
}
9596
}
9697
}

instrumentation/aws-lambda/aws-lambda-core-1.0/library/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This package contains libraries to help instrument AWS lambda functions in your
77
To use the instrumentation, configure `OTEL_INSTRUMENTATION_AWS_LAMBDA_HANDLER` env property to your lambda handler method in following format `package.ClassName::methodName`
88
and use one of wrappers as your lambda `Handler`.
99

10-
In order to configure a span flush timeout (default is set to 1 second), please configure `OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT` env property. The value is in seconds.
10+
In order to configure a span flush timeout (default is set to 10 seconds), please configure `OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT` env property. The value is in milliseconds.
1111

1212
Available wrappers:
1313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Settings for the AWS Lambda Instrumentation
2+
3+
| System property | Type | Default | Description |
4+
|-------------------------------------------------|---------|---------|--------------------------------|
5+
| `otel.instrumentation.aws-lambda.flush-timeout` | Integer | 10000 | Flush timeout in milliseconds. |

instrumentation/aws-lambda/aws-lambda-events-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awslambdaevents/v2_2/AwsLambdaInstrumentationHelper.java

+21-12
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,39 @@
88
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
99
import io.opentelemetry.api.GlobalOpenTelemetry;
1010
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
11+
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.AwsLambdaFunctionInstrumenter;
12+
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.WrapperConfiguration;
1113
import io.opentelemetry.instrumentation.awslambdaevents.v2_2.internal.AwsLambdaEventsInstrumenterFactory;
1214
import io.opentelemetry.instrumentation.awslambdaevents.v2_2.internal.AwsLambdaSqsInstrumenterFactory;
1315
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
16+
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
17+
import java.time.Duration;
1418

1519
public final class AwsLambdaInstrumentationHelper {
1620

17-
private static final io.opentelemetry.instrumentation.awslambdacore.v1_0.internal
18-
.AwsLambdaFunctionInstrumenter
19-
FUNCTION_INSTRUMENTER =
20-
AwsLambdaEventsInstrumenterFactory.createInstrumenter(
21-
GlobalOpenTelemetry.get(), AgentCommonConfig.get().getKnownHttpRequestMethods());
22-
23-
public static io.opentelemetry.instrumentation.awslambdacore.v1_0.internal
24-
.AwsLambdaFunctionInstrumenter
25-
functionInstrumenter() {
26-
return FUNCTION_INSTRUMENTER;
27-
}
28-
21+
private static final AwsLambdaFunctionInstrumenter FUNCTION_INSTRUMENTER =
22+
AwsLambdaEventsInstrumenterFactory.createInstrumenter(
23+
GlobalOpenTelemetry.get(), AgentCommonConfig.get().getKnownHttpRequestMethods());
2924
private static final Instrumenter<SQSEvent, Void> MESSAGE_TRACER =
3025
AwsLambdaSqsInstrumenterFactory.forEvent(GlobalOpenTelemetry.get());
26+
private static final Duration FLUSH_TIMEOUT =
27+
Duration.ofMillis(
28+
AgentInstrumentationConfig.get()
29+
.getLong(
30+
"otel.instrumentation.aws-lambda.flush-timeout",
31+
WrapperConfiguration.OTEL_LAMBDA_FLUSH_TIMEOUT_DEFAULT.toMillis()));
32+
33+
public static AwsLambdaFunctionInstrumenter functionInstrumenter() {
34+
return FUNCTION_INSTRUMENTER;
35+
}
3136

3237
public static Instrumenter<SQSEvent, Void> messageInstrumenter() {
3338
return MESSAGE_TRACER;
3439
}
3540

41+
public static Duration flushTimeout() {
42+
return FLUSH_TIMEOUT;
43+
}
44+
3645
private AwsLambdaInstrumentationHelper() {}
3746
}

instrumentation/aws-lambda/aws-lambda-events-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awslambdaevents/v2_2/AwsLambdaRequestHandlerInstrumentation.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
99
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
10+
import static io.opentelemetry.javaagent.instrumentation.awslambdaevents.v2_2.AwsLambdaInstrumentationHelper.flushTimeout;
1011
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1112
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
1213
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -114,7 +115,7 @@ public static void stopSpan(
114115
.end(functionContext, input, result, throwable);
115116
}
116117

117-
OpenTelemetrySdkAccess.forceFlush(1, TimeUnit.SECONDS);
118+
OpenTelemetrySdkAccess.forceFlush(flushTimeout().toNanos(), TimeUnit.NANOSECONDS);
118119
}
119120
}
120121
}

instrumentation/aws-lambda/aws-lambda-events-2.2/library/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This package contains libraries to help instrument AWS lambda functions in your
77
To use the instrumentation, configure `OTEL_INSTRUMENTATION_AWS_LAMBDA_HANDLER` env property to your lambda handler method in following format `package.ClassName::methodName`
88
and use one of wrappers as your lambda `Handler`.
99

10-
In order to configure a span flush timeout (default is set to 1 second), please configure `OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT` env property. The value is in seconds.
10+
In order to configure a span flush timeout (default is set to 10 seconds), please configure `OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT` env property. The value is in milliseconds.
1111

1212
Available wrappers:
1313

javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/OpenTelemetrySdkAccess.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ public final class OpenTelemetrySdkAccess {
2222
*/
2323
public interface ForceFlusher {
2424
/** Executes force flush. */
25-
void run(int timeout, TimeUnit unit);
25+
void run(long timeout, TimeUnit unit);
2626
}
2727

2828
private static volatile ForceFlusher forceFlush;
2929

30-
/** Forces flushing of pending spans. */
30+
/** Forces flushing of pending telemetry. */
31+
@Deprecated
3132
public static void forceFlush(int timeout, TimeUnit unit) {
33+
forceFlush((long) timeout, unit);
34+
}
35+
36+
/** Forces flushing of pending telemetry. */
37+
public static void forceFlush(long timeout, TimeUnit unit) {
3238
forceFlush.run(timeout, unit);
3339
}
3440

0 commit comments

Comments
 (0)