Skip to content

Commit

Permalink
Unify aws lambda flush handling (#12576)
Browse files Browse the repository at this point in the history
Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
laurit and trask authored Nov 6, 2024
1 parent a0abd63 commit 6a9654d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Settings for the AWS Lambda Instrumentation

| System property | Type | Default | Description |
|-------------------------------------------------|---------|---------|--------------------------------|
| `otel.instrumentation.aws-lambda.flush-timeout` | Integer | 10000 | Flush timeout in milliseconds. |
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.AwsLambdaFunctionInstrumenter;
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.AwsLambdaFunctionInstrumenterFactory;
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.WrapperConfiguration;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import java.time.Duration;

public final class AwsLambdaInstrumentationHelper {

private static final AwsLambdaFunctionInstrumenter FUNCTION_INSTRUMENTER =
AwsLambdaFunctionInstrumenterFactory.createInstrumenter(GlobalOpenTelemetry.get());
private static final Duration FLUSH_TIMEOUT =
Duration.ofMillis(
AgentInstrumentationConfig.get()
.getLong(
"otel.instrumentation.aws-lambda.flush-timeout",
WrapperConfiguration.OTEL_LAMBDA_FLUSH_TIMEOUT_DEFAULT.toMillis()));

public static AwsLambdaFunctionInstrumenter functionInstrumenter() {
return FUNCTION_INSTRUMENTER;
}

public static Duration flushTimeout() {
return FLUSH_TIMEOUT;
}

private AwsLambdaInstrumentationHelper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.awslambdacore.v1_0.AwsLambdaInstrumentationHelper.flushTimeout;
import static io.opentelemetry.javaagent.instrumentation.awslambdacore.v1_0.AwsLambdaInstrumentationHelper.functionInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
Expand Down Expand Up @@ -90,7 +91,7 @@ public static void stopSpan(
functionInstrumenter().end(functionContext, input, null, throwable);
}

OpenTelemetrySdkAccess.forceFlush(1, TimeUnit.SECONDS);
OpenTelemetrySdkAccess.forceFlush(flushTimeout().toNanos(), TimeUnit.NANOSECONDS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This package contains libraries to help instrument AWS lambda functions in your
To use the instrumentation, configure `OTEL_INSTRUMENTATION_AWS_LAMBDA_HANDLER` env property to your lambda handler method in following format `package.ClassName::methodName`
and use one of wrappers as your lambda `Handler`.

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.
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.

Available wrappers:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Settings for the AWS Lambda Instrumentation

| System property | Type | Default | Description |
|-------------------------------------------------|---------|---------|--------------------------------|
| `otel.instrumentation.aws-lambda.flush-timeout` | Integer | 10000 | Flush timeout in milliseconds. |
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,39 @@
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.AwsLambdaFunctionInstrumenter;
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.WrapperConfiguration;
import io.opentelemetry.instrumentation.awslambdaevents.v2_2.internal.AwsLambdaEventsInstrumenterFactory;
import io.opentelemetry.instrumentation.awslambdaevents.v2_2.internal.AwsLambdaSqsInstrumenterFactory;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import java.time.Duration;

public final class AwsLambdaInstrumentationHelper {

private static final io.opentelemetry.instrumentation.awslambdacore.v1_0.internal
.AwsLambdaFunctionInstrumenter
FUNCTION_INSTRUMENTER =
AwsLambdaEventsInstrumenterFactory.createInstrumenter(
GlobalOpenTelemetry.get(), AgentCommonConfig.get().getKnownHttpRequestMethods());

public static io.opentelemetry.instrumentation.awslambdacore.v1_0.internal
.AwsLambdaFunctionInstrumenter
functionInstrumenter() {
return FUNCTION_INSTRUMENTER;
}

private static final AwsLambdaFunctionInstrumenter FUNCTION_INSTRUMENTER =
AwsLambdaEventsInstrumenterFactory.createInstrumenter(
GlobalOpenTelemetry.get(), AgentCommonConfig.get().getKnownHttpRequestMethods());
private static final Instrumenter<SQSEvent, Void> MESSAGE_TRACER =
AwsLambdaSqsInstrumenterFactory.forEvent(GlobalOpenTelemetry.get());
private static final Duration FLUSH_TIMEOUT =
Duration.ofMillis(
AgentInstrumentationConfig.get()
.getLong(
"otel.instrumentation.aws-lambda.flush-timeout",
WrapperConfiguration.OTEL_LAMBDA_FLUSH_TIMEOUT_DEFAULT.toMillis()));

public static AwsLambdaFunctionInstrumenter functionInstrumenter() {
return FUNCTION_INSTRUMENTER;
}

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

public static Duration flushTimeout() {
return FLUSH_TIMEOUT;
}

private AwsLambdaInstrumentationHelper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.awslambdaevents.v2_2.AwsLambdaInstrumentationHelper.flushTimeout;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand Down Expand Up @@ -114,7 +115,7 @@ public static void stopSpan(
.end(functionContext, input, result, throwable);
}

OpenTelemetrySdkAccess.forceFlush(1, TimeUnit.SECONDS);
OpenTelemetrySdkAccess.forceFlush(flushTimeout().toNanos(), TimeUnit.NANOSECONDS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This package contains libraries to help instrument AWS lambda functions in your
To use the instrumentation, configure `OTEL_INSTRUMENTATION_AWS_LAMBDA_HANDLER` env property to your lambda handler method in following format `package.ClassName::methodName`
and use one of wrappers as your lambda `Handler`.

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.
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.

Available wrappers:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ public final class OpenTelemetrySdkAccess {
*/
public interface ForceFlusher {
/** Executes force flush. */
void run(int timeout, TimeUnit unit);
void run(long timeout, TimeUnit unit);
}

private static volatile ForceFlusher forceFlush;

/** Forces flushing of pending spans. */
/** Forces flushing of pending telemetry. */
@Deprecated
public static void forceFlush(int timeout, TimeUnit unit) {
forceFlush((long) timeout, unit);
}

/** Forces flushing of pending telemetry. */
public static void forceFlush(long timeout, TimeUnit unit) {
forceFlush.run(timeout, unit);
}

Expand Down

0 comments on commit 6a9654d

Please sign in to comment.