|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.sdk.trace; |
| 7 | + |
| 8 | +import static io.opentelemetry.api.common.AttributeKey.booleanArrayKey; |
| 9 | +import static io.opentelemetry.api.common.AttributeKey.booleanKey; |
| 10 | +import static io.opentelemetry.api.common.AttributeKey.doubleArrayKey; |
| 11 | +import static io.opentelemetry.api.common.AttributeKey.longArrayKey; |
| 12 | +import static io.opentelemetry.api.common.AttributeKey.longKey; |
| 13 | +import static io.opentelemetry.api.common.AttributeKey.stringArrayKey; |
| 14 | +import static io.opentelemetry.api.common.AttributeKey.stringKey; |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.mockito.Mockito.times; |
| 17 | +import static org.mockito.Mockito.verify; |
| 18 | + |
| 19 | +import io.opentelemetry.api.common.Attributes; |
| 20 | +import io.opentelemetry.api.trace.Span; |
| 21 | +import io.opentelemetry.api.trace.SpanContext; |
| 22 | +import io.opentelemetry.api.trace.StatusCode; |
| 23 | +import io.opentelemetry.api.trace.TraceFlags; |
| 24 | +import io.opentelemetry.api.trace.TraceState; |
| 25 | +import io.opentelemetry.sdk.trace.internal.metrics.SpanMetrics; |
| 26 | +import java.time.Instant; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 32 | + |
| 33 | +@ExtendWith(MockitoExtension.class) |
| 34 | +class NonRecordingSpanTest { |
| 35 | + |
| 36 | + private static final SpanContext DUMMY_CONTEXT = |
| 37 | + SpanContext.create( |
| 38 | + "a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8", |
| 39 | + "c1c2c3c4c5c6c7c8", |
| 40 | + TraceFlags.getDefault(), |
| 41 | + TraceState.getDefault()); |
| 42 | + |
| 43 | + @Mock SpanMetrics.Recording mockRecording; |
| 44 | + |
| 45 | + @Test |
| 46 | + void notRecording() { |
| 47 | + assertThat(new NonRecordingSpan(DUMMY_CONTEXT, mockRecording).isRecording()).isFalse(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void recordingFinishedOnEnd() { |
| 52 | + Span span = new NonRecordingSpan(DUMMY_CONTEXT, mockRecording); |
| 53 | + span.end(); |
| 54 | + verify(mockRecording, times(1)).recordSpanEnd(); |
| 55 | + span.end(0, TimeUnit.NANOSECONDS); |
| 56 | + verify(mockRecording, times(2)).recordSpanEnd(); |
| 57 | + span.end(Instant.EPOCH); |
| 58 | + verify(mockRecording, times(3)).recordSpanEnd(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void doNotCrash() { |
| 63 | + Span span = new NonRecordingSpan(DUMMY_CONTEXT, mockRecording); |
| 64 | + span.setAttribute(stringKey("MyStringAttributeKey"), "MyStringAttributeValue"); |
| 65 | + span.setAttribute(booleanKey("MyBooleanAttributeKey"), true); |
| 66 | + span.setAttribute(longKey("MyLongAttributeKey"), 123L); |
| 67 | + span.setAttribute(longKey("MyLongAttributeKey"), 123); |
| 68 | + span.setAttribute("NullString", null); |
| 69 | + span.setAttribute("EmptyString", ""); |
| 70 | + span.setAttribute("long", 1); |
| 71 | + span.setAttribute("double", 1.0); |
| 72 | + span.setAttribute("boolean", true); |
| 73 | + span.setAttribute(stringArrayKey("NullArrayString"), null); |
| 74 | + span.setAttribute(booleanArrayKey("NullArrayBoolean"), null); |
| 75 | + span.setAttribute(longArrayKey("NullArrayLong"), null); |
| 76 | + span.setAttribute(doubleArrayKey("NullArrayDouble"), null); |
| 77 | + span.setAttribute((String) null, null); |
| 78 | + span.setAllAttributes(null); |
| 79 | + span.setAllAttributes(Attributes.empty()); |
| 80 | + span.setAllAttributes( |
| 81 | + Attributes.of(stringKey("MyStringAttributeKey"), "MyStringAttributeValue")); |
| 82 | + span.addEvent("event"); |
| 83 | + span.addEvent("event", 0, TimeUnit.NANOSECONDS); |
| 84 | + span.addEvent("event", Instant.EPOCH); |
| 85 | + span.addEvent("event", Attributes.of(booleanKey("MyBooleanAttributeKey"), true)); |
| 86 | + span.addEvent( |
| 87 | + "event", Attributes.of(booleanKey("MyBooleanAttributeKey"), true), 0, TimeUnit.NANOSECONDS); |
| 88 | + span.setStatus(StatusCode.OK); |
| 89 | + span.setStatus(StatusCode.OK, "null"); |
| 90 | + span.recordException(new IllegalStateException()); |
| 91 | + span.recordException(new IllegalStateException(), Attributes.empty()); |
| 92 | + span.updateName("name"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void defaultSpan_ToString() { |
| 97 | + Span span = new NonRecordingSpan(DUMMY_CONTEXT, mockRecording); |
| 98 | + assertThat(span.toString()) |
| 99 | + .isEqualTo( |
| 100 | + "NonRecordingSpan{ImmutableSpanContext{traceId=a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8, " |
| 101 | + + "spanId=c1c2c3c4c5c6c7c8, traceFlags=00, " |
| 102 | + + "traceState=ArrayBasedTraceState{entries=[]}, remote=false, valid=true}}"); |
| 103 | + } |
| 104 | +} |
0 commit comments