forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RuleBasedRoutingSampler SPI implementation
- Loading branch information
Showing
4 changed files
with
74 additions
and
3 deletions.
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
68 changes: 68 additions & 0 deletions
68
samplers/src/main/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSamplerProvider.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,68 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; | ||
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.ExtendedConfigProperties; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
public class RuleBasedRoutingSamplerProvider implements ConfigurableSamplerProvider { | ||
|
||
@Override | ||
public Sampler createSampler(ConfigProperties config) { | ||
if (!(config instanceof ExtendedConfigProperties)) { | ||
throw new ConfigurationException("Only support with file configuration"); | ||
} | ||
|
||
String fallbackSamplerString = config.getString("fallback", "always_on"); | ||
Sampler fallbackSampler; | ||
if (fallbackSamplerString.equals("always_on")) { | ||
fallbackSampler = Sampler.alwaysOn(); | ||
} else if (fallbackSamplerString.equals("always_off")) { | ||
fallbackSampler = Sampler.alwaysOff(); | ||
} else { | ||
throw new IllegalArgumentException("fallback must be always_on or always_off"); | ||
} | ||
|
||
String spanKindString = config.getString("span_kind", "SERVER"); | ||
SpanKind spanKind; | ||
try { | ||
spanKind = SpanKind.valueOf(config.getString("span_kind", "SERVER")); | ||
} catch (NoSuchElementException e) { | ||
throw new ConfigurationException("Invalid span_kind: " + spanKindString, e); | ||
} | ||
|
||
RuleBasedRoutingSamplerBuilder builder = | ||
RuleBasedRoutingSampler.builder(spanKind, fallbackSampler); | ||
|
||
List<ExtendedConfigProperties> rules = | ||
((ExtendedConfigProperties) config).getListConfigProperties("drop_rules"); | ||
if (rules == null || rules.isEmpty()) { | ||
throw new ConfigurationException("drop_rules is required"); | ||
} | ||
for (ExtendedConfigProperties rule : rules) { | ||
String attribute = rule.getString("attribute"); | ||
String pattern = rule.getString("pattern"); | ||
if (attribute == null || pattern == null) { | ||
throw new ConfigurationException("drop_rule entries require attribute and pattern fields"); | ||
} | ||
builder.drop(AttributeKey.stringKey(attribute), pattern); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "rule_based_routing_sampler"; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...TA-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
io.opentelemetry.contrib.sampler.LinksParentAlwaysOnSamplerProvider | ||
io.opentelemetry.contrib.sampler.RuleBasedRoutingSamplerProvider |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ plugins { | |
|
||
dependencyResolutionManagement { | ||
repositories { | ||
mavenLocal() | ||
mavenCentral() | ||
} | ||
} | ||
|