|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +// Includes work from: |
| 7 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 8 | +// SPDX-License-Identifier: Apache-2.0 |
| 9 | + |
| 10 | +package io.opentelemetry.sdk.trace.internal; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.mockito.ArgumentMatchers.any; |
| 14 | +import static org.mockito.ArgumentMatchers.anyString; |
| 15 | +import static org.mockito.Mockito.mock; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +import io.opentelemetry.api.common.AttributeKey; |
| 19 | +import io.opentelemetry.api.common.Attributes; |
| 20 | +import io.opentelemetry.api.trace.SpanKind; |
| 21 | +import io.opentelemetry.api.trace.TraceId; |
| 22 | +import io.opentelemetry.api.trace.TraceState; |
| 23 | +import io.opentelemetry.context.Context; |
| 24 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 25 | +import io.opentelemetry.sdk.trace.samplers.SamplingDecision; |
| 26 | +import io.opentelemetry.sdk.trace.samplers.SamplingResult; |
| 27 | +import java.util.Collections; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +/** Unit tests for {@link AlwaysRecordSampler}. */ |
| 32 | +class AlwaysRecordSamplerTest { |
| 33 | + |
| 34 | + // Mocks |
| 35 | + private Sampler mockSampler; |
| 36 | + |
| 37 | + private AlwaysRecordSampler sampler; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setUpSamplers() { |
| 41 | + mockSampler = mock(Sampler.class); |
| 42 | + sampler = AlwaysRecordSampler.create(mockSampler); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testGetDescription() { |
| 47 | + when(mockSampler.getDescription()).thenReturn("mockDescription"); |
| 48 | + assertThat(sampler.getDescription()).isEqualTo("AlwaysRecordSampler{mockDescription}"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testRecordAndSampleSamplingDecision() { |
| 53 | + validateShouldSample(SamplingDecision.RECORD_AND_SAMPLE, SamplingDecision.RECORD_AND_SAMPLE); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testRecordOnlySamplingDecision() { |
| 58 | + validateShouldSample(SamplingDecision.RECORD_ONLY, SamplingDecision.RECORD_ONLY); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testDropSamplingDecision() { |
| 63 | + validateShouldSample(SamplingDecision.DROP, SamplingDecision.RECORD_ONLY); |
| 64 | + } |
| 65 | + |
| 66 | + private void validateShouldSample( |
| 67 | + SamplingDecision rootDecision, SamplingDecision expectedDecision) { |
| 68 | + SamplingResult rootResult = buildRootSamplingResult(rootDecision); |
| 69 | + when(mockSampler.shouldSample(any(), anyString(), anyString(), any(), any(), any())) |
| 70 | + .thenReturn(rootResult); |
| 71 | + SamplingResult actualResult = |
| 72 | + sampler.shouldSample( |
| 73 | + Context.current(), |
| 74 | + TraceId.fromLongs(1, 2), |
| 75 | + "name", |
| 76 | + SpanKind.CLIENT, |
| 77 | + Attributes.empty(), |
| 78 | + Collections.emptyList()); |
| 79 | + |
| 80 | + if (rootDecision.equals(expectedDecision)) { |
| 81 | + assertThat(actualResult).isEqualTo(rootResult); |
| 82 | + assertThat(actualResult.getDecision()).isEqualTo(rootDecision); |
| 83 | + } else { |
| 84 | + assertThat(actualResult).isNotEqualTo(rootResult); |
| 85 | + assertThat(actualResult.getDecision()).isEqualTo(expectedDecision); |
| 86 | + } |
| 87 | + |
| 88 | + assertThat(actualResult.getAttributes()).isEqualTo(rootResult.getAttributes()); |
| 89 | + TraceState traceState = TraceState.builder().build(); |
| 90 | + assertThat(actualResult.getUpdatedTraceState(traceState)) |
| 91 | + .isEqualTo(rootResult.getUpdatedTraceState(traceState)); |
| 92 | + } |
| 93 | + |
| 94 | + private static SamplingResult buildRootSamplingResult(SamplingDecision samplingDecision) { |
| 95 | + return new SamplingResult() { |
| 96 | + @Override |
| 97 | + public SamplingDecision getDecision() { |
| 98 | + return samplingDecision; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Attributes getAttributes() { |
| 103 | + return Attributes.of(AttributeKey.stringKey("key"), samplingDecision.name()); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public TraceState getUpdatedTraceState(TraceState parentTraceState) { |
| 108 | + return TraceState.builder().put("key", samplingDecision.name()).build(); |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | +} |
0 commit comments