|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.sampler; |
| 7 | + |
| 8 | +import io.opentelemetry.api.common.AttributeKey; |
| 9 | +import io.opentelemetry.api.trace.SpanKind; |
| 10 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 11 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
| 12 | +import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider; |
| 13 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.ExtendedConfigProperties; |
| 14 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 15 | +import java.util.List; |
| 16 | +import java.util.NoSuchElementException; |
| 17 | + |
| 18 | +public class RuleBasedRoutingSamplerProvider implements ConfigurableSamplerProvider { |
| 19 | + |
| 20 | + @Override |
| 21 | + public Sampler createSampler(ConfigProperties config) { |
| 22 | + if (!(config instanceof ExtendedConfigProperties)) { |
| 23 | + throw new ConfigurationException("Only support with file configuration"); |
| 24 | + } |
| 25 | + |
| 26 | + String fallbackSamplerString = config.getString("fallback", "always_on"); |
| 27 | + Sampler fallbackSampler; |
| 28 | + if (fallbackSamplerString.equals("always_on")) { |
| 29 | + fallbackSampler = Sampler.alwaysOn(); |
| 30 | + } else if (fallbackSamplerString.equals("always_off")) { |
| 31 | + fallbackSampler = Sampler.alwaysOff(); |
| 32 | + } else { |
| 33 | + throw new IllegalArgumentException("fallback must be always_on or always_off"); |
| 34 | + } |
| 35 | + |
| 36 | + String spanKindString = config.getString("span_kind", "SERVER"); |
| 37 | + SpanKind spanKind; |
| 38 | + try { |
| 39 | + spanKind = SpanKind.valueOf(config.getString("span_kind", "SERVER")); |
| 40 | + } catch (NoSuchElementException e) { |
| 41 | + throw new ConfigurationException("Invalid span_kind: " + spanKindString, e); |
| 42 | + } |
| 43 | + |
| 44 | + RuleBasedRoutingSamplerBuilder builder = |
| 45 | + RuleBasedRoutingSampler.builder(spanKind, fallbackSampler); |
| 46 | + |
| 47 | + List<ExtendedConfigProperties> rules = |
| 48 | + ((ExtendedConfigProperties) config).getListConfigProperties("drop_rules"); |
| 49 | + if (rules == null || rules.isEmpty()) { |
| 50 | + throw new ConfigurationException("drop_rules is required"); |
| 51 | + } |
| 52 | + for (ExtendedConfigProperties rule : rules) { |
| 53 | + String attribute = rule.getString("attribute"); |
| 54 | + String pattern = rule.getString("pattern"); |
| 55 | + if (attribute == null || pattern == null) { |
| 56 | + throw new ConfigurationException("drop_rule entries require attribute and pattern fields"); |
| 57 | + } |
| 58 | + builder.drop(AttributeKey.stringKey(attribute), pattern); |
| 59 | + } |
| 60 | + |
| 61 | + return builder.build(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String getName() { |
| 66 | + return "rule_based_routing_sampler"; |
| 67 | + } |
| 68 | +} |
0 commit comments