|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.sampler.consistent56; |
| 7 | + |
| 8 | +import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getInvalidThreshold; |
| 9 | +import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.isValidThreshold; |
| 10 | + |
| 11 | +import io.opentelemetry.api.common.Attributes; |
| 12 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 13 | +import io.opentelemetry.api.trace.SpanKind; |
| 14 | +import io.opentelemetry.api.trace.TraceState; |
| 15 | +import io.opentelemetry.context.Context; |
| 16 | +import io.opentelemetry.sdk.trace.data.LinkData; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import java.util.stream.Stream; |
| 20 | +import javax.annotation.Nullable; |
| 21 | +import javax.annotation.concurrent.Immutable; |
| 22 | + |
| 23 | +/** |
| 24 | + * A consistent sampler that queries all its delegate samplers for their sampling threshold, and |
| 25 | + * uses the minimum threshold value received. |
| 26 | + */ |
| 27 | +@Immutable |
| 28 | +final class ConsistentAnyOf extends ConsistentSampler { |
| 29 | + |
| 30 | + private final ComposableSampler[] delegates; |
| 31 | + |
| 32 | + private final String description; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructs a new consistent AnyOf sampler using the provided delegate samplers. |
| 36 | + * |
| 37 | + * @param delegates the delegate samplers |
| 38 | + */ |
| 39 | + ConsistentAnyOf(@Nullable ComposableSampler... delegates) { |
| 40 | + if (delegates == null || delegates.length == 0) { |
| 41 | + throw new IllegalArgumentException( |
| 42 | + "At least one delegate must be specified for ConsistentAnyOf"); |
| 43 | + } |
| 44 | + |
| 45 | + this.delegates = delegates; |
| 46 | + |
| 47 | + this.description = |
| 48 | + Stream.of(delegates) |
| 49 | + .map(Object::toString) |
| 50 | + .collect(Collectors.joining(",", "ConsistentAnyOf{", "}")); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public SamplingIntent getSamplingIntent( |
| 55 | + Context parentContext, |
| 56 | + String name, |
| 57 | + SpanKind spanKind, |
| 58 | + Attributes attributes, |
| 59 | + List<LinkData> parentLinks) { |
| 60 | + |
| 61 | + SamplingIntent[] intents = new SamplingIntent[delegates.length]; |
| 62 | + int k = 0; |
| 63 | + long minimumThreshold = getInvalidThreshold(); |
| 64 | + for (ComposableSampler delegate : delegates) { |
| 65 | + SamplingIntent delegateIntent = |
| 66 | + delegate.getSamplingIntent(parentContext, name, spanKind, attributes, parentLinks); |
| 67 | + long delegateThreshold = delegateIntent.getThreshold(); |
| 68 | + if (isValidThreshold(delegateThreshold)) { |
| 69 | + if (isValidThreshold(minimumThreshold)) { |
| 70 | + minimumThreshold = Math.min(delegateThreshold, minimumThreshold); |
| 71 | + } else { |
| 72 | + minimumThreshold = delegateThreshold; |
| 73 | + } |
| 74 | + } |
| 75 | + intents[k++] = delegateIntent; |
| 76 | + } |
| 77 | + |
| 78 | + long resultingThreshold = minimumThreshold; |
| 79 | + |
| 80 | + return new SamplingIntent() { |
| 81 | + @Override |
| 82 | + public long getThreshold() { |
| 83 | + return resultingThreshold; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Attributes getAttributes() { |
| 88 | + AttributesBuilder builder = Attributes.builder(); |
| 89 | + for (SamplingIntent intent : intents) { |
| 90 | + builder = builder.putAll(intent.getAttributes()); |
| 91 | + } |
| 92 | + return builder.build(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public TraceState updateTraceState(TraceState previousState) { |
| 97 | + for (SamplingIntent intent : intents) { |
| 98 | + previousState = intent.updateTraceState(previousState); |
| 99 | + } |
| 100 | + return previousState; |
| 101 | + } |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getDescription() { |
| 107 | + return description; |
| 108 | + } |
| 109 | +} |
0 commit comments