Search in sources :

Example 61 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java-instrumentation by open-telemetry.

the class InstrumenterTest method client.

@Test
void client() {
    Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(new AttributesExtractor1(), new AttributesExtractor2()).addSpanLinksExtractor(new LinksExtractor()).newClientInstrumenter(Map::put);
    Map<String, String> request = new HashMap<>(REQUEST);
    Context context = instrumenter.start(Context.root(), request);
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(request).containsKey("traceparent");
    instrumenter.end(context, request, RESPONSE, null);
    otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasKind(SpanKind.CLIENT).hasInstrumentationLibraryInfo(InstrumentationLibraryInfo.create("test", null)).hasTraceId(spanContext.getTraceId()).hasSpanId(spanContext.getSpanId()).hasParentSpanId(SpanId.getInvalid()).hasStatus(StatusData.unset()).hasLinks(expectedSpanLink()).hasAttributesSatisfying(attributes -> assertThat(attributes).containsOnly(attributeEntry("req1", "req1_value"), attributeEntry("req2", "req2_2_value"), attributeEntry("req3", "req3_value"), attributeEntry("resp1", "resp1_value"), attributeEntry("resp2", "resp2_2_value"), attributeEntry("resp3", "resp3_value")))));
}
Also used : NetServerAttributesGetter(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter) TraceFlags(io.opentelemetry.api.trace.TraceFlags) OpenTelemetryExtension(io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) Mock(org.mockito.Mock) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) StatusData(io.opentelemetry.sdk.trace.data.StatusData) Attributes(io.opentelemetry.api.common.Attributes) HashMap(java.util.HashMap) InstrumentationVersion(io.opentelemetry.instrumentation.api.InstrumentationVersion) AtomicReference(java.util.concurrent.atomic.AtomicReference) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) TraceState(io.opentelemetry.api.trace.TraceState) SpanKey(io.opentelemetry.instrumentation.api.internal.SpanKey) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Map(java.util.Map) DbClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor) HttpServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor) HttpClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor) Nullable(javax.annotation.Nullable) OpenTelemetryAssertions.attributeEntry(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) MessageOperation(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors(java.util.stream.Collectors) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test) NetServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor) Stream(java.util.stream.Stream) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) TraceId(io.opentelemetry.api.trace.TraceId) MessagingAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessagingAttributesExtractor) LinkData(io.opentelemetry.sdk.trace.data.LinkData) RpcAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesExtractor) Collections(java.util.Collections) Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 62 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java-instrumentation by open-telemetry.

the class InstrumenterTest method shouldUseContextCustomizer.

@Test
void shouldUseContextCustomizer() {
    // given
    ContextKey<String> testKey = ContextKey.named("test");
    Instrumenter<String, String> instrumenter = Instrumenter.<String, String>builder(otelTesting.getOpenTelemetry(), "test", request -> "test span").addContextCustomizer((context, request, attributes) -> context.with(testKey, "testVal")).newInstrumenter();
    // when
    Context context = instrumenter.start(Context.root(), "request");
    // then
    assertThat(context.get(testKey)).isEqualTo("testVal");
}
Also used : NetServerAttributesGetter(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter) TraceFlags(io.opentelemetry.api.trace.TraceFlags) OpenTelemetryExtension(io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) Mock(org.mockito.Mock) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) StatusData(io.opentelemetry.sdk.trace.data.StatusData) Attributes(io.opentelemetry.api.common.Attributes) HashMap(java.util.HashMap) InstrumentationVersion(io.opentelemetry.instrumentation.api.InstrumentationVersion) AtomicReference(java.util.concurrent.atomic.AtomicReference) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) TraceState(io.opentelemetry.api.trace.TraceState) SpanKey(io.opentelemetry.instrumentation.api.internal.SpanKey) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Map(java.util.Map) DbClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor) HttpServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor) HttpClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor) Nullable(javax.annotation.Nullable) OpenTelemetryAssertions.attributeEntry(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) MessageOperation(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors(java.util.stream.Collectors) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test) NetServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor) Stream(java.util.stream.Stream) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) TraceId(io.opentelemetry.api.trace.TraceId) MessagingAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessagingAttributesExtractor) LinkData(io.opentelemetry.sdk.trace.data.LinkData) RpcAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesExtractor) Collections(java.util.Collections) Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) Test(org.junit.jupiter.api.Test)

Example 63 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java-instrumentation by open-telemetry.

the class InstrumenterTest method server_http.

@Test
void server_http() {
    Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(mockHttpServerAttributes, NetServerAttributesExtractor.create(new ConstantNetPeerIpGetter<>("2.2.2.2")), new AttributesExtractor1(), new AttributesExtractor2()).addSpanLinksExtractor(new LinksExtractor()).newServerInstrumenter(new MapGetter());
    Context context = instrumenter.start(Context.root(), REQUEST);
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(SpanKey.SERVER.fromContextOrNull(context).getSpanContext()).isEqualTo(spanContext);
    instrumenter.end(context, REQUEST, RESPONSE, null);
    otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasAttributesSatisfying(attributes -> assertThat(attributes).containsEntry(SemanticAttributes.NET_PEER_IP, "2.2.2.2"))));
}
Also used : NetServerAttributesGetter(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter) TraceFlags(io.opentelemetry.api.trace.TraceFlags) OpenTelemetryExtension(io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) Mock(org.mockito.Mock) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) StatusData(io.opentelemetry.sdk.trace.data.StatusData) Attributes(io.opentelemetry.api.common.Attributes) HashMap(java.util.HashMap) InstrumentationVersion(io.opentelemetry.instrumentation.api.InstrumentationVersion) AtomicReference(java.util.concurrent.atomic.AtomicReference) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) TraceState(io.opentelemetry.api.trace.TraceState) SpanKey(io.opentelemetry.instrumentation.api.internal.SpanKey) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Map(java.util.Map) DbClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor) HttpServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor) HttpClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor) Nullable(javax.annotation.Nullable) OpenTelemetryAssertions.attributeEntry(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) MessageOperation(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors(java.util.stream.Collectors) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test) NetServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor) Stream(java.util.stream.Stream) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) TraceId(io.opentelemetry.api.trace.TraceId) MessagingAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessagingAttributesExtractor) LinkData(io.opentelemetry.sdk.trace.data.LinkData) RpcAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesExtractor) Collections(java.util.Collections) Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 64 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java-instrumentation by open-telemetry.

the class HttpClientMetricsTest method collectsMetrics.

@Test
void collectsMetrics() {
    InMemoryMetricReader metricReader = InMemoryMetricReader.create();
    SdkMeterProvider meterProvider = SdkMeterProvider.builder().registerMetricReader(metricReader).setMinimumCollectionInterval(Duration.ZERO).build();
    RequestListener listener = HttpClientMetrics.get().create(meterProvider.get("test"));
    Attributes requestAttributes = Attributes.builder().put("http.method", "GET").put("http.url", "https://localhost:1234/").put("http.host", "host").put("http.target", "/").put("http.scheme", "https").put("net.peer.name", "localhost").put("net.peer.ip", "0.0.0.0").put("net.peer.port", 1234).build();
    Attributes responseAttributes = Attributes.builder().put("http.flavor", "2.0").put("http.server_name", "server").put("http.status_code", 200).build();
    Context parent = Context.root().with(Span.wrap(SpanContext.create("ff01020304050600ff0a0b0c0d0e0f00", "090a0b0c0d0e0f00", TraceFlags.getSampled(), TraceState.getDefault())));
    Context context1 = listener.start(parent, requestAttributes, nanos(100));
    assertThat(metricReader.collectAllMetrics()).isEmpty();
    Context context2 = listener.start(Context.root(), requestAttributes, nanos(150));
    assertThat(metricReader.collectAllMetrics()).isEmpty();
    listener.end(context1, responseAttributes, nanos(250));
    assertThat(metricReader.collectAllMetrics()).hasSize(1).anySatisfy(metric -> assertThat(metric).hasName("http.client.duration").hasUnit("ms").hasDoubleHistogram().points().satisfiesExactly(point -> {
        assertThat(point).hasSum(150).attributes().containsOnly(attributeEntry("net.peer.name", "localhost"), attributeEntry("net.peer.port", 1234), attributeEntry("http.method", "GET"), attributeEntry("http.flavor", "2.0"), attributeEntry("http.status_code", 200));
        assertThat(point).exemplars().hasSize(1);
        assertThat(point.getExemplars().get(0)).hasTraceId("ff01020304050600ff0a0b0c0d0e0f00").hasSpanId("090a0b0c0d0e0f00");
    }));
    listener.end(context2, responseAttributes, nanos(300));
    assertThat(metricReader.collectAllMetrics()).hasSize(1).anySatisfy(metric -> assertThat(metric).hasName("http.client.duration").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasSum(300)));
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) OpenTelemetryAssertions.attributeEntry(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry) TraceFlags(io.opentelemetry.api.trace.TraceFlags) Context(io.opentelemetry.context.Context) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) Attributes(io.opentelemetry.api.common.Attributes) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) RequestListener(io.opentelemetry.instrumentation.api.instrumenter.RequestListener) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) TraceState(io.opentelemetry.api.trace.TraceState) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Duration(java.time.Duration) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) RequestListener(io.opentelemetry.instrumentation.api.instrumenter.RequestListener) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 65 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java-instrumentation by open-telemetry.

the class TemporaryMetricsViewTest method shouldApplyClientDurationView.

@Test
void shouldApplyClientDurationView() {
    Attributes startAttributes = Attributes.builder().put(SemanticAttributes.HTTP_URL, "https://somehost/high/cardinality/12345?jsessionId=121454").put(SemanticAttributes.HTTP_METHOD, "GET").put(SemanticAttributes.HTTP_SCHEME, "https").put(SemanticAttributes.HTTP_HOST, "somehost").put(SemanticAttributes.HTTP_TARGET, "/high/cardinality/12345?jsessionId=121454").build();
    Attributes endAttributes = Attributes.builder().put(SemanticAttributes.HTTP_STATUS_CODE, 500).put(SemanticAttributes.NET_PEER_NAME, "somehost2").put(SemanticAttributes.NET_PEER_IP, "127.0.0.1").put(SemanticAttributes.NET_PEER_PORT, 443).build();
    OpenTelemetryAssertions.assertThat(applyClientDurationView(startAttributes, endAttributes)).containsOnly(attributeEntry(SemanticAttributes.NET_PEER_NAME.getKey(), "somehost2"), attributeEntry(SemanticAttributes.NET_PEER_PORT.getKey(), 443), attributeEntry(SemanticAttributes.HTTP_METHOD.getKey(), "GET"), attributeEntry(SemanticAttributes.HTTP_STATUS_CODE.getKey(), 500));
}
Also used : SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Aggregations

Attributes (io.opentelemetry.api.common.Attributes)175 Test (org.junit.jupiter.api.Test)128 ResourceAttributes (io.opentelemetry.semconv.resource.attributes.ResourceAttributes)45 Resource (io.opentelemetry.sdk.resources.Resource)33 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)27 Context (io.opentelemetry.context.Context)25 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)24 SpanContext (io.opentelemetry.api.trace.SpanContext)22 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)22 SpanData (io.opentelemetry.sdk.trace.data.SpanData)22 HashMap (java.util.HashMap)21 Duration (java.time.Duration)20 ExemplarData (io.opentelemetry.sdk.metrics.data.ExemplarData)18 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)18 TestClock (io.opentelemetry.sdk.testing.time.TestClock)18 AttributeKey (io.opentelemetry.api.common.AttributeKey)17 Span (io.opentelemetry.api.trace.Span)17 DoubleExemplarData (io.opentelemetry.sdk.metrics.data.DoubleExemplarData)17 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)16 Map (java.util.Map)16