Search in sources :

Example 1 with RequestListener

use of io.opentelemetry.instrumentation.api.instrumenter.RequestListener 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 2 with RequestListener

use of io.opentelemetry.instrumentation.api.instrumenter.RequestListener in project opentelemetry-java-instrumentation by open-telemetry.

the class RpcClientMetricsTest method collectsMetrics.

@Test
void collectsMetrics() {
    InMemoryMetricReader metricReader = InMemoryMetricReader.createDelta();
    SdkMeterProvider meterProvider = SdkMeterProvider.builder().registerMetricReader(metricReader).setMinimumCollectionInterval(Duration.ZERO).build();
    RequestListener listener = RpcClientMetrics.get().create(meterProvider.get("test"));
    Attributes requestAttributes = Attributes.builder().put(SemanticAttributes.RPC_SYSTEM, "grpc").put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService").put(SemanticAttributes.RPC_METHOD, "exampleMethod").build();
    Attributes responseAttributes1 = Attributes.builder().put(SemanticAttributes.NET_PEER_NAME, "example.com").put(SemanticAttributes.NET_PEER_IP, "127.0.0.1").put(SemanticAttributes.NET_PEER_PORT, 8080).put(SemanticAttributes.NET_TRANSPORT, "ip_tcp").build();
    Attributes responseAttributes2 = Attributes.builder().put(SemanticAttributes.NET_PEER_IP, "127.0.0.1").put(SemanticAttributes.NET_PEER_PORT, 8080).put(SemanticAttributes.NET_TRANSPORT, "ip_tcp").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, responseAttributes1, nanos(250));
    assertThat(metricReader.collectAllMetrics()).hasSize(1).anySatisfy(metric -> assertThat(metric).hasName("rpc.client.duration").hasUnit("ms").hasDoubleHistogram().points().satisfiesExactly(point -> {
        assertThat(point).hasSum(150).attributes().containsOnly(attributeEntry("rpc.system", "grpc"), attributeEntry("rpc.service", "myservice.EchoService"), attributeEntry("rpc.method", "exampleMethod"), attributeEntry("net.peer.name", "example.com"), attributeEntry("net.peer.port", 8080), attributeEntry("net.transport", "ip_tcp"));
        assertThat(point).exemplars().hasSize(1);
        assertThat(point.getExemplars().get(0)).hasTraceId("ff01020304050600ff0a0b0c0d0e0f00").hasSpanId("090a0b0c0d0e0f00");
    }));
    listener.end(context2, responseAttributes2, nanos(300));
    assertThat(metricReader.collectAllMetrics()).hasSize(1).anySatisfy(metric -> assertThat(metric).hasName("rpc.client.duration").hasUnit("ms").hasDoubleHistogram().points().satisfiesExactly(point -> {
        assertThat(point).hasSum(150).attributes().containsOnly(attributeEntry("rpc.system", "grpc"), attributeEntry("rpc.service", "myservice.EchoService"), attributeEntry("rpc.method", "exampleMethod"), attributeEntry("net.peer.ip", "127.0.0.1"), attributeEntry("net.peer.port", 8080), attributeEntry("net.transport", "ip_tcp"));
    }));
}
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) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) 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) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 3 with RequestListener

use of io.opentelemetry.instrumentation.api.instrumenter.RequestListener in project opentelemetry-java-instrumentation by open-telemetry.

the class RpcServerMetricsTest method collectsMetrics.

@Test
void collectsMetrics() {
    InMemoryMetricReader metricReader = InMemoryMetricReader.createDelta();
    SdkMeterProvider meterProvider = SdkMeterProvider.builder().registerMetricReader(metricReader).setMinimumCollectionInterval(Duration.ZERO).build();
    RequestListener listener = RpcServerMetrics.get().create(meterProvider.get("test"));
    Attributes requestAttributes = Attributes.builder().put(SemanticAttributes.RPC_SYSTEM, "grpc").put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService").put(SemanticAttributes.RPC_METHOD, "exampleMethod").build();
    Attributes responseAttributes1 = Attributes.builder().put(SemanticAttributes.NET_HOST_NAME, "example.com").put(SemanticAttributes.NET_HOST_IP, "127.0.0.1").put(SemanticAttributes.NET_HOST_PORT, 8080).put(SemanticAttributes.NET_TRANSPORT, "ip_tcp").build();
    Attributes responseAttributes2 = Attributes.builder().put(SemanticAttributes.NET_HOST_IP, "127.0.0.1").put(SemanticAttributes.NET_HOST_PORT, 8080).put(SemanticAttributes.NET_TRANSPORT, "ip_tcp").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, responseAttributes1, nanos(250));
    assertThat(metricReader.collectAllMetrics()).hasSize(1).anySatisfy(metric -> assertThat(metric).hasName("rpc.server.duration").hasUnit("ms").hasDoubleHistogram().points().satisfiesExactly(point -> {
        assertThat(point).hasSum(150).attributes().containsOnly(attributeEntry("rpc.system", "grpc"), attributeEntry("rpc.service", "myservice.EchoService"), attributeEntry("rpc.method", "exampleMethod"), attributeEntry("net.host.name", "example.com"), attributeEntry("net.transport", "ip_tcp"));
        assertThat(point).exemplars().hasSize(1);
        assertThat(point.getExemplars().get(0)).hasTraceId("ff01020304050600ff0a0b0c0d0e0f00").hasSpanId("090a0b0c0d0e0f00");
    }));
    listener.end(context2, responseAttributes2, nanos(300));
    assertThat(metricReader.collectAllMetrics()).hasSize(1).anySatisfy(metric -> assertThat(metric).hasName("rpc.server.duration").hasUnit("ms").hasDoubleHistogram().points().satisfiesExactly(point -> {
        assertThat(point).hasSum(150).attributes().containsOnly(attributeEntry("rpc.system", "grpc"), attributeEntry("rpc.service", "myservice.EchoService"), attributeEntry("rpc.method", "exampleMethod"), attributeEntry("net.host.ip", "127.0.0.1"), attributeEntry("net.transport", "ip_tcp"));
    }));
}
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) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) 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) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 4 with RequestListener

use of io.opentelemetry.instrumentation.api.instrumenter.RequestListener in project opentelemetry-java-instrumentation by open-telemetry.

the class HttpServerMetricsTest method collectsHttpRouteFromEndAttributes.

@Test
void collectsHttpRouteFromEndAttributes() {
    // given
    InMemoryMetricReader metricReader = InMemoryMetricReader.create();
    SdkMeterProvider meterProvider = SdkMeterProvider.builder().registerMetricReader(metricReader).setMinimumCollectionInterval(Duration.ZERO).build();
    RequestListener listener = HttpServerMetrics.get().create(meterProvider.get("test"));
    Attributes requestAttributes = Attributes.builder().put("http.host", "host").put("http.scheme", "https").build();
    Attributes responseAttributes = Attributes.builder().put("http.route", "/test/{id}").build();
    Context parentContext = Context.root();
    // when
    Context context = listener.start(parentContext, requestAttributes, nanos(100));
    listener.end(context, responseAttributes, nanos(200));
    // then
    assertThat(metricReader.collectAllMetrics()).anySatisfy(metric -> assertThat(metric).hasName("http.server.duration").hasUnit("ms").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasSum(100).attributes().containsOnly(attributeEntry("http.scheme", "https"), attributeEntry("http.host", "host"), attributeEntry("http.route", "/test/{id}"))));
}
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 5 with RequestListener

use of io.opentelemetry.instrumentation.api.instrumenter.RequestListener in project opentelemetry-java-instrumentation by open-telemetry.

the class HttpServerMetricsTest method collectsMetrics.

@Test
void collectsMetrics() {
    InMemoryMetricReader metricReader = InMemoryMetricReader.create();
    SdkMeterProvider meterProvider = SdkMeterProvider.builder().registerMetricReader(metricReader).setMinimumCollectionInterval(Duration.ZERO).build();
    RequestListener listener = HttpServerMetrics.get().create(meterProvider.get("test"));
    Attributes requestAttributes = Attributes.builder().put("http.method", "GET").put("http.host", "host").put("http.target", "/").put("http.scheme", "https").put("net.host.name", "localhost").put("net.host.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()).hasSize(1).anySatisfy(metric -> assertThat(metric).hasName("http.server.active_requests").hasDescription("The number of concurrent HTTP requests that are currently in-flight").hasUnit("requests").hasLongSum().points().satisfiesExactly(point -> {
        assertThat(point).hasValue(1).attributes().containsOnly(attributeEntry("http.host", "host"), attributeEntry("http.method", "GET"), attributeEntry("http.scheme", "https"));
        assertThat(point).exemplars().hasSize(1);
        assertThat(point.getExemplars().get(0)).hasTraceId("ff01020304050600ff0a0b0c0d0e0f00").hasSpanId("090a0b0c0d0e0f00");
    }));
    Context context2 = listener.start(Context.root(), requestAttributes, nanos(150));
    assertThat(metricReader.collectAllMetrics()).hasSize(1).anySatisfy(metric -> assertThat(metric).hasName("http.server.active_requests").hasLongSum().points().satisfiesExactly(point -> assertThat(point).hasValue(2)));
    listener.end(context1, responseAttributes, nanos(250));
    assertThat(metricReader.collectAllMetrics()).hasSize(2).anySatisfy(metric -> assertThat(metric).hasName("http.server.active_requests").hasLongSum().points().satisfiesExactly(point -> assertThat(point).hasValue(1))).anySatisfy(metric -> assertThat(metric).hasName("http.server.duration").hasUnit("ms").hasDoubleHistogram().points().satisfiesExactly(point -> {
        assertThat(point).hasSum(150).attributes().containsOnly(attributeEntry("http.scheme", "https"), attributeEntry("http.host", "host"), attributeEntry("http.method", "GET"), attributeEntry("http.status_code", 200), attributeEntry("http.flavor", "2.0"));
        assertThat(point).exemplars().hasSize(1);
        assertThat(point.getExemplars().get(0)).hasTraceId("ff01020304050600ff0a0b0c0d0e0f00").hasSpanId("090a0b0c0d0e0f00");
    }));
    listener.end(context2, responseAttributes, nanos(300));
    assertThat(metricReader.collectAllMetrics()).hasSize(2).anySatisfy(metric -> assertThat(metric).hasName("http.server.active_requests").hasLongSum().points().satisfiesExactly(point -> assertThat(point).hasValue(0))).anySatisfy(metric -> assertThat(metric).hasName("http.server.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)

Aggregations

Attributes (io.opentelemetry.api.common.Attributes)5 Span (io.opentelemetry.api.trace.Span)5 SpanContext (io.opentelemetry.api.trace.SpanContext)5 TraceFlags (io.opentelemetry.api.trace.TraceFlags)5 TraceState (io.opentelemetry.api.trace.TraceState)5 Context (io.opentelemetry.context.Context)5 RequestListener (io.opentelemetry.instrumentation.api.instrumenter.RequestListener)5 SdkMeterProvider (io.opentelemetry.sdk.metrics.SdkMeterProvider)5 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)5 OpenTelemetryAssertions.attributeEntry (io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry)5 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)5 Duration (java.time.Duration)5 TimeUnit (java.util.concurrent.TimeUnit)5 Test (org.junit.jupiter.api.Test)5 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)2