-
Notifications
You must be signed in to change notification settings - Fork 178
[dynamic control] Add trace sampling validation #2604
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
56 changes: 56 additions & 0 deletions
56
dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/PolicyValidator.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,56 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.contrib.dynamic.policy; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| public interface PolicyValidator { | ||
| /** | ||
| * Validates a policy configuration provided as a JSON string. | ||
| * | ||
| * @param json The JSON string containing the policy configuration. | ||
| * @return The validated {@link TelemetryPolicy}, or {@code null} if the JSON does not contain a | ||
| * valid policy for this validator. | ||
| */ | ||
| @Nullable | ||
| TelemetryPolicy validate(String json); | ||
|
|
||
| /** | ||
| * Returns the type of the policy this validator handles. | ||
| * | ||
| * @return The policy type string (e.g., "trace-sampling"). | ||
| */ | ||
| String getPolicyType(); | ||
|
|
||
| /** | ||
| * Validates a policy configuration provided as a key-value pair (alias). | ||
| * | ||
| * <p>This is intended for simple configuration cases where a full JSON object is not necessary. | ||
| * | ||
| * @param key The alias key (e.g., "trace-sampling.probability"). | ||
| * @param value The value associated with the key. | ||
| * @return The validated {@link TelemetryPolicy}, or {@code null} if the key/value pair is invalid | ||
| * or not handled by this validator. | ||
| */ | ||
| @Nullable | ||
| default TelemetryPolicy validateAlias(String key, String value) { | ||
| throw new UnsupportedOperationException( | ||
| "Alias validation is not supported by validator " | ||
| + getClass().getName() | ||
| + " for key " | ||
| + key); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the alias key supported by this validator, if any. | ||
| * | ||
| * @return The alias key string, or {@code null} if aliases are not supported. | ||
| */ | ||
| @Nullable | ||
| default String getAlias() { | ||
| return null; | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
...control/src/main/java/io/opentelemetry/contrib/dynamic/policy/TraceSamplingValidator.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,75 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.contrib.dynamic.policy; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import java.util.logging.Logger; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Validator for trace sampling policies. | ||
| * | ||
| * <p>This validator handles the "trace-sampling" policy type and supports the | ||
| * "trace-sampling.probability" alias. | ||
| */ | ||
| public final class TraceSamplingValidator implements PolicyValidator { | ||
| private static final Logger logger = Logger.getLogger(TraceSamplingValidator.class.getName()); | ||
| private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
|
||
| @Override | ||
| public String getPolicyType() { | ||
| return "trace-sampling"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getAlias() { | ||
| return "trace-sampling.probability"; | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public TelemetryPolicy validate(String json) { | ||
| try { | ||
| JsonNode node = MAPPER.readTree(json); | ||
| if (node.has(getPolicyType())) { | ||
| JsonNode spec = node.get(getPolicyType()); | ||
| if (spec.has("probability")) { | ||
| JsonNode probNode = spec.get("probability"); | ||
| if (probNode.isNumber()) { | ||
| double d = probNode.asDouble(); | ||
| if (d >= 0.0 && d <= 1.0) { | ||
| return new TelemetryPolicy(getPolicyType(), spec); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch (JsonProcessingException e) { | ||
| // Not valid JSON for this validator | ||
| } | ||
| logger.info("Invalid trace-sampling JSON: " + json); | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public TelemetryPolicy validateAlias(String key, String value) { | ||
| if (getAlias() != null && getAlias().equals(key)) { | ||
| try { | ||
| double d = Double.parseDouble(value); | ||
| if (d >= 0.0 && d <= 1.0) { | ||
| JsonNode spec = MAPPER.createObjectNode().put("probability", d); | ||
| return new TelemetryPolicy(getPolicyType(), spec); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| // invalid | ||
| } | ||
| logger.info("Ignoring invalid trace-sampling.probability value: " + value); | ||
|
||
| } | ||
| return null; | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging the full JSON input at INFO level could expose sensitive data. Consider logging only a sanitized message or moving this to DEBUG level with appropriate sanitization.