|
| 1 | +/* |
| 2 | + * ApplicationInsights-Java |
| 3 | + * Copyright (c) Microsoft Corporation |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * MIT License |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 8 | + * software and associated documentation files (the ""Software""), to deal in the Software |
| 9 | + * without restriction, including without limitation the rights to use, copy, modify, merge, |
| 10 | + * publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 11 | + * persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 13 | + * substantial portions of the Software. |
| 14 | + * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 17 | + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 18 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | + * DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.microsoft.applicationinsights.agent.internal.init; |
| 23 | + |
| 24 | +import static io.opentelemetry.api.trace.SpanKind.INTERNAL; |
| 25 | +import static io.opentelemetry.sdk.testing.assertj.TracesAssert.assertThat; |
| 26 | +import static org.assertj.core.api.Assertions.entry; |
| 27 | +import static org.awaitility.Awaitility.await; |
| 28 | + |
| 29 | +import com.microsoft.applicationinsights.agent.internal.configuration.Configuration; |
| 30 | +import io.opentelemetry.api.common.AttributeKey; |
| 31 | +import io.opentelemetry.api.trace.Span; |
| 32 | +import io.opentelemetry.api.trace.Tracer; |
| 33 | +import io.opentelemetry.context.Context; |
| 34 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 35 | +import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions; |
| 36 | +import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; |
| 37 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 38 | +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.List; |
| 41 | +import org.junit.jupiter.api.AfterEach; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | + |
| 44 | +public class InheritedAttributesSpanProcessorTest { |
| 45 | + |
| 46 | + private final InMemorySpanExporter exporter = InMemorySpanExporter.create(); |
| 47 | + |
| 48 | + private final AttributeKey<String> oneStringKey = AttributeKey.stringKey("one"); |
| 49 | + private final AttributeKey<Long> oneLongKey = AttributeKey.longKey("one"); |
| 50 | + |
| 51 | + @AfterEach |
| 52 | + public void afterEach() { |
| 53 | + exporter.reset(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void shouldNotInheritAttribute() { |
| 58 | + Tracer tracer = newTracer(Collections.emptyList()); |
| 59 | + Span span = |
| 60 | + tracer.spanBuilder("parent").setNoParent().setAttribute(oneStringKey, "1").startSpan(); |
| 61 | + Context context = Context.root().with(span); |
| 62 | + try { |
| 63 | + tracer.spanBuilder("child").setParent(context).startSpan().end(); |
| 64 | + } finally { |
| 65 | + span.end(); |
| 66 | + } |
| 67 | + |
| 68 | + await().until(() -> exporter.getFinishedSpanItems().size() == 2); |
| 69 | + |
| 70 | + assertThat(Collections.singleton(exporter.getFinishedSpanItems())) |
| 71 | + .hasTracesSatisfyingExactly( |
| 72 | + trace -> |
| 73 | + trace.hasSpansSatisfyingExactly( |
| 74 | + childSpan -> |
| 75 | + childSpan.hasName("child").hasKind(INTERNAL).hasTotalAttributeCount(0), |
| 76 | + parentSpan -> |
| 77 | + parentSpan |
| 78 | + .hasName("parent") |
| 79 | + .hasKind(INTERNAL) |
| 80 | + .hasAttributesSatisfying( |
| 81 | + attributes -> |
| 82 | + OpenTelemetryAssertions.assertThat(attributes) |
| 83 | + .containsOnly(entry(oneStringKey, "1"))))); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void shouldInheritAttribute() { |
| 88 | + Configuration.InheritedAttribute inheritedAttribute = new Configuration.InheritedAttribute(); |
| 89 | + inheritedAttribute.key = "one"; |
| 90 | + inheritedAttribute.type = Configuration.SpanAttributeType.STRING; |
| 91 | + |
| 92 | + Tracer tracer = newTracer(Collections.singletonList(inheritedAttribute)); |
| 93 | + Span span = |
| 94 | + tracer.spanBuilder("parent").setNoParent().setAttribute(oneStringKey, "1").startSpan(); |
| 95 | + Context context = Context.root().with(span); |
| 96 | + try { |
| 97 | + tracer.spanBuilder("child").setParent(context).startSpan().end(); |
| 98 | + } finally { |
| 99 | + span.end(); |
| 100 | + } |
| 101 | + |
| 102 | + await().until(() -> exporter.getFinishedSpanItems().size() == 2); |
| 103 | + |
| 104 | + assertThat(Collections.singleton(exporter.getFinishedSpanItems())) |
| 105 | + .hasTracesSatisfyingExactly( |
| 106 | + trace -> |
| 107 | + trace.hasSpansSatisfyingExactly( |
| 108 | + childSpan -> |
| 109 | + childSpan |
| 110 | + .hasName("child") |
| 111 | + .hasKind(INTERNAL) |
| 112 | + .hasAttributesSatisfying( |
| 113 | + attributes -> |
| 114 | + OpenTelemetryAssertions.assertThat(attributes) |
| 115 | + .containsOnly(entry(oneStringKey, "1"))), |
| 116 | + parentSpan -> |
| 117 | + parentSpan |
| 118 | + .hasName("parent") |
| 119 | + .hasKind(INTERNAL) |
| 120 | + .hasAttributesSatisfying( |
| 121 | + attributes -> |
| 122 | + OpenTelemetryAssertions.assertThat(attributes) |
| 123 | + .containsOnly(entry(oneStringKey, "1"))))); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void shouldNotInheritAttributeWithSameNameButDifferentType() { |
| 128 | + Configuration.InheritedAttribute inheritedAttribute = new Configuration.InheritedAttribute(); |
| 129 | + inheritedAttribute.key = "one"; |
| 130 | + inheritedAttribute.type = Configuration.SpanAttributeType.STRING; |
| 131 | + |
| 132 | + Tracer tracer = newTracer(Collections.singletonList(inheritedAttribute)); |
| 133 | + Span span = tracer.spanBuilder("parent").setNoParent().setAttribute(oneLongKey, 1L).startSpan(); |
| 134 | + Context context = Context.root().with(span); |
| 135 | + try { |
| 136 | + tracer.spanBuilder("child").setParent(context).startSpan().end(); |
| 137 | + } finally { |
| 138 | + span.end(); |
| 139 | + } |
| 140 | + |
| 141 | + await().until(() -> exporter.getFinishedSpanItems().size() == 2); |
| 142 | + |
| 143 | + assertThat(Collections.singleton(exporter.getFinishedSpanItems())) |
| 144 | + .hasTracesSatisfyingExactly( |
| 145 | + trace -> |
| 146 | + trace.hasSpansSatisfyingExactly( |
| 147 | + childSpan -> |
| 148 | + childSpan.hasName("child").hasKind(INTERNAL).hasTotalAttributeCount(0), |
| 149 | + parentSpan -> |
| 150 | + parentSpan |
| 151 | + .hasName("parent") |
| 152 | + .hasKind(INTERNAL) |
| 153 | + .hasAttributesSatisfying( |
| 154 | + attributes -> |
| 155 | + OpenTelemetryAssertions.assertThat(attributes) |
| 156 | + .containsOnly(entry(oneLongKey, 1L))))); |
| 157 | + } |
| 158 | + |
| 159 | + private Tracer newTracer(List<Configuration.InheritedAttribute> inheritedAttributes) { |
| 160 | + OpenTelemetrySdk sdk = |
| 161 | + OpenTelemetrySdk.builder() |
| 162 | + .setTracerProvider( |
| 163 | + SdkTracerProvider.builder() |
| 164 | + .addSpanProcessor(new InheritedAttributesSpanProcessor(inheritedAttributes)) |
| 165 | + .addSpanProcessor(SimpleSpanProcessor.create(exporter)) |
| 166 | + .build()) |
| 167 | + .build(); |
| 168 | + return sdk.getTracer("test"); |
| 169 | + } |
| 170 | +} |
0 commit comments