use of io.opentelemetry.sdk.metrics.SdkMeterProvider in project opentelemetry-java by open-telemetry.
the class OpenTelemetrySdkBuilder method build.
/**
* Returns a new {@link OpenTelemetrySdk} built with the configuration of this {@link
* OpenTelemetrySdkBuilder}. This SDK is not registered as the global {@link
* io.opentelemetry.api.OpenTelemetry}. It is recommended that you register one SDK using {@link
* OpenTelemetrySdkBuilder#buildAndRegisterGlobal()} for use by instrumentation that requires
* access to a global instance of {@link io.opentelemetry.api.OpenTelemetry}.
*
* @see GlobalOpenTelemetry
*/
public OpenTelemetrySdk build() {
SdkTracerProvider tracerProvider = this.tracerProvider;
if (tracerProvider == null) {
tracerProvider = SdkTracerProvider.builder().build();
}
SdkMeterProvider meterProvider = this.meterProvider;
if (meterProvider == null) {
meterProvider = SdkMeterProvider.builder().build();
}
SdkLogEmitterProvider logEmitterProvider = this.logEmitterProvider;
if (logEmitterProvider == null) {
logEmitterProvider = SdkLogEmitterProvider.builder().build();
}
return new OpenTelemetrySdk(tracerProvider, meterProvider, logEmitterProvider, propagators);
}
use of io.opentelemetry.sdk.metrics.SdkMeterProvider in project opentelemetry-java by open-telemetry.
the class OtlpExporterIntegrationTest method testMetricExport.
private static void testMetricExport(MetricExporter metricExporter) {
MetricReaderFactory reader = PeriodicMetricReader.builder(metricExporter).setInterval(Duration.ofSeconds(5)).newMetricReaderFactory();
SdkMeterProvider meterProvider = SdkMeterProvider.builder().setResource(RESOURCE).registerMetricReader(reader).build();
Meter meter = meterProvider.meterBuilder(OtlpExporterIntegrationTest.class.getName()).build();
LongCounter longCounter = meter.counterBuilder("my-counter").build();
longCounter.add(100, Attributes.builder().put("key", "value").build());
try {
await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.metricRequests).hasSize(1));
} finally {
meterProvider.close();
}
ExportMetricsServiceRequest request = grpcServer.metricRequests.get(0);
assertThat(request.getResourceMetricsCount()).isEqualTo(1);
ResourceMetrics resourceMetrics = request.getResourceMetrics(0);
assertThat(resourceMetrics.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
assertThat(resourceMetrics.getInstrumentationLibraryMetricsCount()).isEqualTo(1);
InstrumentationLibraryMetrics ilMetrics = resourceMetrics.getInstrumentationLibraryMetrics(0);
assertThat(ilMetrics.getInstrumentationLibrary().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
assertThat(ilMetrics.getMetricsCount()).isEqualTo(1);
Metric metric = ilMetrics.getMetrics(0);
assertThat(metric.getName()).isEqualTo("my-counter");
assertThat(metric.getDataCase()).isEqualTo(Metric.DataCase.SUM);
Sum sum = metric.getSum();
assertThat(sum.getAggregationTemporality()).isEqualTo(AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE);
assertThat(sum.getDataPointsCount()).isEqualTo(1);
NumberDataPoint dataPoint = sum.getDataPoints(0);
assertThat(dataPoint.getAsInt()).isEqualTo(100);
assertThat(dataPoint.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
}
use of io.opentelemetry.sdk.metrics.SdkMeterProvider 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.sdk.metrics.SdkMeterProvider 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"));
}));
}
use of io.opentelemetry.sdk.metrics.SdkMeterProvider 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"));
}));
}
Aggregations