-
Notifications
You must be signed in to change notification settings - Fork 135
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
Add declarative config support for RuleBasedRoutingSampler #1440
Merged
trask
merged 7 commits into
open-telemetry:main
from
jack-berg:rule-based-sampler-provider
Sep 16, 2024
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
644b328
Add declarative config support for RuleBasedRoutingSampler
jack-berg 73607f5
remove snapshot
jack-berg 5307231
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 919f46d
remove dependency version
jack-berg ecfa7d0
Add jack-berg to component owners
jack-berg c8edcf5
PR feedback
jack-berg b43dbd7
Add jack-berg to component_owners.yml
jack-berg 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 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 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
102 changes: 102 additions & 0 deletions
102
...a/io/opentelemetry/contrib/sampler/internal/RuleBasedRoutingSamplerComponentProvider.java
This file contains 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,102 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.internal; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.contrib.sampler.RuleBasedRoutingSampler; | ||
import io.opentelemetry.contrib.sampler.RuleBasedRoutingSamplerBuilder; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; | ||
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; | ||
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.FileConfiguration; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
import java.util.List; | ||
|
||
/** | ||
* Declarative configuration SPI implementation for {@link RuleBasedRoutingSampler}. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public class RuleBasedRoutingSamplerComponentProvider implements ComponentProvider<Sampler> { | ||
|
||
private static final String ACTION_RECORD_AND_SAMPLE = "RECORD_AND_SAMPLE"; | ||
private static final String ACTION_DROP = "DROP"; | ||
|
||
@Override | ||
public Class<Sampler> getType() { | ||
return Sampler.class; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "rule_based_routing"; | ||
} | ||
|
||
@Override | ||
public Sampler create(StructuredConfigProperties config) { | ||
StructuredConfigProperties fallbackModel = config.getStructured("fallback_sampler"); | ||
if (fallbackModel == null) { | ||
throw new ConfigurationException( | ||
"rule_based_routing sampler .fallback is required but is null"); | ||
} | ||
Sampler fallbackSampler; | ||
try { | ||
fallbackSampler = FileConfiguration.createSampler(fallbackModel); | ||
} catch (ConfigurationException e) { | ||
throw new ConfigurationException( | ||
"rule_Based_routing sampler failed to create .fallback sampler", e); | ||
} | ||
|
||
String spanKindString = config.getString("span_kind", "SERVER"); | ||
SpanKind spanKind; | ||
try { | ||
spanKind = SpanKind.valueOf(spanKindString); | ||
} catch (IllegalArgumentException e) { | ||
throw new ConfigurationException( | ||
"rule_based_routing sampler .span_kind is invalid: " + spanKindString, e); | ||
} | ||
|
||
RuleBasedRoutingSamplerBuilder builder = | ||
RuleBasedRoutingSampler.builder(spanKind, fallbackSampler); | ||
|
||
List<StructuredConfigProperties> rules = config.getStructuredList("rules"); | ||
if (rules == null || rules.isEmpty()) { | ||
throw new ConfigurationException("rule_based_routing sampler .rules is required"); | ||
} | ||
|
||
for (StructuredConfigProperties rule : rules) { | ||
String attribute = rule.getString("attribute"); | ||
if (attribute == null) { | ||
throw new ConfigurationException( | ||
"rule_based_routing sampler .rules[].attribute is required"); | ||
} | ||
AttributeKey<String> attributeKey = AttributeKey.stringKey(attribute); | ||
String pattern = rule.getString("pattern"); | ||
if (pattern == null) { | ||
throw new ConfigurationException("rule_based_routing sampler .rules[].pattern is required"); | ||
} | ||
String action = rule.getString("action"); | ||
if (action == null) { | ||
throw new ConfigurationException("rule_based_routing sampler .rules[].action is required"); | ||
} | ||
if (action.equals(ACTION_RECORD_AND_SAMPLE)) { | ||
builder.recordAndSample(attributeKey, pattern); | ||
} else if (action.equals(ACTION_DROP)) { | ||
builder.drop(attributeKey, pattern); | ||
} else { | ||
throw new ConfigurationException( | ||
"rule_based_routing sampler .rules[].action is must be " | ||
+ ACTION_RECORD_AND_SAMPLE | ||
+ " or " | ||
+ ACTION_DROP); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...urces/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider
This file contains 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 @@ | ||
io.opentelemetry.contrib.sampler.internal.RuleBasedRoutingSamplerComponentProvider |
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.
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.
TODO: add usage instructions for otel java agent
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.
Doesn't work with the agent yet:
For now, add a note in the README that agent support is WIP.