-
Notifications
You must be signed in to change notification settings - Fork 10
feat(otel): Parent Workflow span onto shared execution trace #647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ConfiguredSamplerResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.otel; | ||
|
|
||
| import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Reconstructs the configured trace sampler from OpenTelemetry autoconfiguration settings. | ||
| * | ||
| * <p>On the Java-agent path the agent-configured tracer provider is not usable from the application classloader (its | ||
| * {@code SdkTracerProvider} type has a different class identity), so the sampler object cannot be read directly. The | ||
| * autoconfiguration <em>settings</em> that produced it, however, are process-global and readable: the OpenTelemetry | ||
| * Java agent is configured entirely through the zero-code autoconfigure module via the {@code otel.traces.sampler} / | ||
| * {@code otel.traces.sampler.arg} system properties or the {@code OTEL_TRACES_SAMPLER} / | ||
| * {@code OTEL_TRACES_SAMPLER_ARG} environment variables. This resolver reads those and rebuilds the equivalent | ||
| * {@link Sampler} using the SDK's own factories, so the reconstructed sampler matches the agent's semantics and can be | ||
| * evaluated with the real trace ID, span name, and attributes. | ||
| * | ||
| * <p>Only the locally reproducible sampler kinds are rebuilt: {@code always_on}, {@code always_off}, | ||
| * {@code traceidratio}, and their {@code parentbased_*} variants. Remote samplers ({@code xray}, {@code jaeger_remote}, | ||
| * {@code parentbased_jaeger_remote}) depend on runtime state fetched from a backend and cannot be reproduced locally; | ||
| * custom samplers registered by name or by a customizer SPI live in the agent and are likewise out of reach. For those, | ||
| * and when nothing is configured, this returns {@code null} so the caller applies its documented default. | ||
| */ | ||
| final class ConfiguredSamplerResolver { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(ConfiguredSamplerResolver.class); | ||
|
|
||
| private static final String SAMPLER_PROPERTY = "otel.traces.sampler"; | ||
| private static final String SAMPLER_ARG_PROPERTY = "otel.traces.sampler.arg"; | ||
| private static final String SAMPLER_ENV = "OTEL_TRACES_SAMPLER"; | ||
| private static final String SAMPLER_ARG_ENV = "OTEL_TRACES_SAMPLER_ARG"; | ||
|
|
||
| // Per the OpenTelemetry spec, traceidratio defaults to sampling everything when no ratio arg is provided. | ||
| private static final double DEFAULT_RATIO = 1.0d; | ||
|
|
||
| private ConfiguredSamplerResolver() {} | ||
|
|
||
| /** | ||
| * The sampler described by the OTel autoconfiguration settings, or {@code null} when nothing is configured or the | ||
| * configured sampler cannot be reproduced locally (remote or custom samplers). | ||
| */ | ||
| static Sampler resolve() { | ||
| return resolve(configValue(SAMPLER_PROPERTY, SAMPLER_ENV), configValue(SAMPLER_ARG_PROPERTY, SAMPLER_ARG_ENV)); | ||
| } | ||
|
|
||
| /** Package-visible for testing: builds the sampler from explicit values without reading the environment. */ | ||
| static Sampler resolve(String samplerName, String samplerArg) { | ||
| if (samplerName == null || samplerName.isBlank()) { | ||
| return null; | ||
| } | ||
| return switch (samplerName.trim()) { | ||
| case "always_on" -> Sampler.alwaysOn(); | ||
| case "always_off" -> Sampler.alwaysOff(); | ||
| case "traceidratio" -> Sampler.traceIdRatioBased(ratio(samplerArg)); | ||
| case "parentbased_always_on" -> Sampler.parentBased(Sampler.alwaysOn()); | ||
| case "parentbased_always_off" -> Sampler.parentBased(Sampler.alwaysOff()); | ||
| case "parentbased_traceidratio" -> Sampler.parentBased(Sampler.traceIdRatioBased(ratio(samplerArg))); | ||
| // Remote samplers (xray, jaeger_remote, parentbased_jaeger_remote) and any custom/unknown name cannot be | ||
| // reproduced from configuration alone. | ||
| default -> { | ||
| logger.debug( | ||
| "Configured sampler '{}' cannot be reproduced locally; using the default decision.", | ||
| samplerName); | ||
| yield null; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Parses and normalizes the ratio argument to a value {@code Sampler.traceIdRatioBased} accepts, i.e. within | ||
| * {@code [0.0, 1.0]}. {@code Double.parseDouble} would otherwise accept negatives, values above 1, NaN, and | ||
| * infinities, and passing those to the sampler throws {@code IllegalArgumentException} — an exception that, during | ||
| * {@code onInvocationStart}, is swallowed by the plugin runner and silently disables telemetry for the invocation. | ||
| * Following the spec's documented clamping, a finite value is clamped into range; an unparseable or non-finite | ||
| * value falls back to the default (sample everything). Every non-exact value is logged. | ||
| */ | ||
| private static double ratio(String samplerArg) { | ||
| if (samplerArg == null || samplerArg.isBlank()) { | ||
| return DEFAULT_RATIO; | ||
| } | ||
| double parsed; | ||
| try { | ||
| parsed = Double.parseDouble(samplerArg.trim()); | ||
| } catch (NumberFormatException e) { | ||
| logger.debug( | ||
| "Unparseable {} value '{}'; defaulting the sampling ratio to {}.", | ||
| SAMPLER_ARG_ENV, | ||
| samplerArg, | ||
| DEFAULT_RATIO); | ||
| return DEFAULT_RATIO; | ||
| } | ||
| if (!Double.isFinite(parsed)) { | ||
| logger.debug( | ||
| "Non-finite {} value '{}'; defaulting the sampling ratio to {}.", | ||
| SAMPLER_ARG_ENV, | ||
| samplerArg, | ||
| DEFAULT_RATIO); | ||
| return DEFAULT_RATIO; | ||
| } | ||
| if (parsed < 0.0d) { | ||
| logger.debug("{} value '{}' is below 0; clamping the sampling ratio to 0.", SAMPLER_ARG_ENV, samplerArg); | ||
| return 0.0d; | ||
| } | ||
| if (parsed > 1.0d) { | ||
| logger.debug("{} value '{}' is above 1; clamping the sampling ratio to 1.", SAMPLER_ARG_ENV, samplerArg); | ||
| return 1.0d; | ||
| } | ||
| return parsed; | ||
| } | ||
|
|
||
| /** Reads a setting, preferring the system property (matching OTel autoconfigure precedence) then the env var. */ | ||
| private static String configValue(String systemProperty, String environmentVariable) { | ||
| var value = System.getProperty(systemProperty); | ||
| if (value == null || value.isBlank()) { | ||
| value = System.getenv(environmentVariable); | ||
| } | ||
| return value; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.