Skip to content

Commit

Permalink
Add RuleBasedRoutingSampler SPI implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg committed Oct 3, 2023
1 parent ba896e1 commit c09b3ae
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
7 changes: 4 additions & 3 deletions samplers/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ description = "Sampler which makes its decision based on semantic attributes val
otelJava.moduleName.set("io.opentelemetry.contrib.sampler")

dependencies {
api("io.opentelemetry:opentelemetry-sdk")
api("io.opentelemetry:opentelemetry-sdk:1.31.0-SNAPSHOT")
api("io.opentelemetry.semconv:opentelemetry-semconv")
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
api("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:1.31.0-SNAPSHOT")
api("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi:1.31.0-SNAPSHOT")
implementation("io.opentelemetry:opentelemetry-sdk-extension-incubator:1.31.0-alpha-SNAPSHOT")
}
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";
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.opentelemetry.contrib.sampler.LinksParentAlwaysOnSamplerProvider
io.opentelemetry.contrib.sampler.RuleBasedRoutingSamplerProvider
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {

dependencyResolutionManagement {
repositories {
mavenLocal()
mavenCentral()
}
}
Expand Down

0 comments on commit c09b3ae

Please sign in to comment.