|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.api.instrumenter; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
| 9 | +import static java.util.Collections.emptyMap; |
| 10 | +import static java.util.Collections.singletonMap; |
| 11 | + |
| 12 | +import io.opentelemetry.api.OpenTelemetry; |
| 13 | +import io.opentelemetry.context.Context; |
| 14 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration; |
| 15 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationBuilder; |
| 16 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.ExperimentalLanguageSpecificInstrumentationModel; |
| 17 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.InstrumentationModel; |
| 18 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel; |
| 19 | +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; |
| 20 | +import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; |
| 21 | +import io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes; |
| 22 | +import java.lang.reflect.Field; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.Consumer; |
| 25 | +import java.util.stream.Stream; |
| 26 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.Arguments; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
| 30 | + |
| 31 | +class AddThreadDetailsTest { |
| 32 | + |
| 33 | + @RegisterExtension |
| 34 | + static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); |
| 35 | + |
| 36 | + public static Stream<Arguments> allEnabledAndDisabledValues() { |
| 37 | + return Stream.of( |
| 38 | + Arguments.of( |
| 39 | + true, |
| 40 | + (Consumer<SpanDataAssert>) |
| 41 | + span -> |
| 42 | + span.hasAttributesSatisfying( |
| 43 | + satisfies(ThreadIncubatingAttributes.THREAD_ID, n -> n.isNotNull()), |
| 44 | + satisfies(ThreadIncubatingAttributes.THREAD_NAME, n -> n.isNotBlank()))), |
| 45 | + Arguments.of( |
| 46 | + false, |
| 47 | + (Consumer<SpanDataAssert>) |
| 48 | + span -> |
| 49 | + span.hasAttributesSatisfying( |
| 50 | + satisfies(ThreadIncubatingAttributes.THREAD_ID, n -> n.isNull()), |
| 51 | + satisfies(ThreadIncubatingAttributes.THREAD_NAME, n -> n.isNull())))); |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest(name = "enabled={0}") |
| 55 | + @MethodSource("allEnabledAndDisabledValues") |
| 56 | + void enabled(boolean enabled, Consumer<SpanDataAssert> spanAttributesConsumer) |
| 57 | + throws NoSuchFieldException, IllegalAccessException { |
| 58 | + OpenTelemetry openTelemetry = DeclarativeConfiguration.create(model(enabled)); |
| 59 | + Instrumenter<Map<String, String>, Map<String, String>> instrumenter = |
| 60 | + Instrumenter.<Map<String, String>, Map<String, String>>builder( |
| 61 | + openTelemetry, "test", name -> "span") |
| 62 | + .buildInstrumenter(); |
| 63 | + |
| 64 | + // OpenTelemetryExtension doesn't allow passing a custom OpenTelemetry instance |
| 65 | + Field field = Instrumenter.class.getDeclaredField("tracer"); |
| 66 | + field.setAccessible(true); |
| 67 | + field.set(instrumenter, otelTesting.getOpenTelemetry().getTracer("test")); |
| 68 | + |
| 69 | + Context context = instrumenter.start(Context.root(), emptyMap()); |
| 70 | + instrumenter.end(context, emptyMap(), emptyMap(), null); |
| 71 | + |
| 72 | + otelTesting |
| 73 | + .assertTraces() |
| 74 | + .hasTracesSatisfyingExactly( |
| 75 | + trace -> trace.hasSpansSatisfyingExactly(spanAttributesConsumer)); |
| 76 | + } |
| 77 | + |
| 78 | + private static OpenTelemetryConfigurationModel model(boolean enabled) { |
| 79 | + return new DeclarativeConfigurationBuilder() |
| 80 | + .customizeModel( |
| 81 | + new OpenTelemetryConfigurationModel() |
| 82 | + .withFileFormat("1.0-rc.1") |
| 83 | + .withInstrumentationDevelopment( |
| 84 | + new InstrumentationModel() |
| 85 | + .withJava( |
| 86 | + new ExperimentalLanguageSpecificInstrumentationModel() |
| 87 | + .withAdditionalProperty( |
| 88 | + "thread_details", singletonMap("enabled", enabled))))); |
| 89 | + } |
| 90 | +} |
0 commit comments