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")))));
}
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");
}
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"))));
}
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)));
}
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));
}
Aggregations