|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package co.elastic.otel.compositesampling; |
| 20 | + |
| 21 | +import io.opentelemetry.api.common.Attributes; |
| 22 | +import io.opentelemetry.api.trace.SpanKind; |
| 23 | +import io.opentelemetry.context.Context; |
| 24 | +import io.opentelemetry.contrib.sampler.consistent56.ConsistentSampler; |
| 25 | +import io.opentelemetry.sdk.trace.data.LinkData; |
| 26 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 27 | +import io.opentelemetry.sdk.trace.samplers.SamplingResult; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +public enum DynamicCompositeParentBasedTraceIdRatioBasedSampler implements Sampler { |
| 31 | + INSTANCE; |
| 32 | + |
| 33 | + static final double DEFAULT_TRACEIDRATIO_SAMPLE_RATIO = 1.0d; |
| 34 | + |
| 35 | + private static Sampler delegate = newSampler(DEFAULT_TRACEIDRATIO_SAMPLE_RATIO); |
| 36 | + |
| 37 | + public static void setRatio(double ratio) { |
| 38 | + delegate = newSampler(ratio); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public SamplingResult shouldSample( |
| 43 | + Context parentContext, |
| 44 | + String traceId, |
| 45 | + String name, |
| 46 | + SpanKind spanKind, |
| 47 | + Attributes attributes, |
| 48 | + List<LinkData> parentLinks) { |
| 49 | + return delegate.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getDescription() { |
| 54 | + return INSTANCE.getDescription(); |
| 55 | + } |
| 56 | + |
| 57 | + private static Sampler newSampler(double ratio) { |
| 58 | + return ConsistentSampler.parentBased(ConsistentSampler.probabilityBased(ratio)); |
| 59 | + } |
| 60 | +} |
0 commit comments