use of io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics 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.proto.metrics.v1.InstrumentationLibraryMetrics in project opentelemetry-java by open-telemetry.
the class FullConfigTest method configures.
@Test
void configures() throws Exception {
Collection<String> fields = GlobalOpenTelemetry.get().getPropagators().getTextMapPropagator().fields();
List<String> keys = new ArrayList<>();
keys.addAll(W3CTraceContextPropagator.getInstance().fields());
keys.addAll(W3CBaggagePropagator.getInstance().fields());
keys.addAll(B3Propagator.injectingSingleHeader().fields());
keys.addAll(B3Propagator.injectingMultiHeaders().fields());
keys.addAll(JaegerPropagator.getInstance().fields());
keys.addAll(OtTracePropagator.getInstance().fields());
keys.addAll(AwsXrayPropagator.getInstance().fields());
// Added by TestPropagatorProvider
keys.add("test");
assertThat(fields).containsExactlyInAnyOrderElementsOf(keys);
GlobalOpenTelemetry.get().getMeterProvider().get("test").counterBuilder("test").build().add(1);
GlobalOpenTelemetry.get().getTracer("test").spanBuilder("test").startSpan().setAttribute("cat", "meow").setAttribute("dog", "bark").end();
Meter meter = GlobalOpenTelemetry.get().getMeter("test");
meter.counterBuilder("my-metric").build().add(1, Attributes.builder().put("allowed", "bear").put("not allowed", "dog").build());
meter.counterBuilder("my-other-metric").build().add(1);
LogEmitter logEmitter = logEmitterProvider.get("test");
logEmitter.logBuilder().setBody("debug log message").setSeverity(Severity.DEBUG).emit();
logEmitter.logBuilder().setBody("info log message").setSeverity(Severity.INFO).emit();
await().untilAsserted(() -> assertThat(otlpTraceRequests).hasSize(1));
ExportTraceServiceRequest traceRequest = otlpTraceRequests.take();
assertThat(traceRequest.getResourceSpans(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
io.opentelemetry.proto.trace.v1.Span span = traceRequest.getResourceSpans(0).getInstrumentationLibrarySpans(0).getSpans(0);
// Dog dropped by attribute limit.
assertThat(span.getAttributesList()).containsExactlyInAnyOrder(KeyValue.newBuilder().setKey("configured").setValue(AnyValue.newBuilder().setBoolValue(true).build()).build(), KeyValue.newBuilder().setKey("wrapped").setValue(AnyValue.newBuilder().setIntValue(1).build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
// await on assertions since metrics may come in different order for BatchSpanProcessor,
// exporter, or the ones we
// created in the test.
await().untilAsserted(() -> {
ExportMetricsServiceRequest metricRequest = otlpMetricsRequests.take();
assertThat(metricRequest.getResourceMetrics(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
for (ResourceMetrics resourceMetrics : metricRequest.getResourceMetricsList()) {
assertThat(resourceMetrics.getInstrumentationLibraryMetricsList()).anySatisfy(ilm -> assertThat(ilm.getInstrumentationLibrary().getName()).isEqualTo("test"));
for (InstrumentationLibraryMetrics instrumentationLibraryMetrics : resourceMetrics.getInstrumentationLibraryMetricsList()) {
for (Metric metric : instrumentationLibraryMetrics.getMetricsList()) {
// SPI was loaded
// MetricExporterCustomizer filters metrics not named my-metric
assertThat(metric.getName()).isEqualTo("my-metric");
// TestMeterProviderConfigurer configures a view that only passes on attribute
// named allowed
// configured-test
assertThat(getFirstDataPointLabels(metric)).contains(KeyValue.newBuilder().setKey("allowed").setValue(AnyValue.newBuilder().setStringValue("bear").build()).build());
}
}
}
});
await().untilAsserted(() -> assertThat(otlpLogsRequests).hasSize(1));
ExportLogsServiceRequest logRequest = otlpLogsRequests.take();
assertThat(logRequest.getResourceLogs(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
// MetricExporterCustomizer filters logs not whose level is less than Severity.INFO
LogRecord log = logRequest.getResourceLogs(0).getInstrumentationLibraryLogs(0).getLogRecords(0);
assertThat(log.getBody().getStringValue()).isEqualTo("info log message");
assertThat(log.getSeverityNumberValue()).isEqualTo(Severity.INFO.getSeverityNumber());
}
use of io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics in project opentelemetry-java-instrumentation by open-telemetry.
the class AgentTestingExporterAccess method getExportedMetrics.
@SuppressWarnings("unchecked")
public static List<MetricData> getExportedMetrics() {
List<byte[]> exportRequests;
try {
exportRequests = (List<byte[]>) getMetricExportRequests.invokeExact();
} catch (Throwable t) {
throw new AssertionError("Could not invoke getMetricExportRequests", t);
}
List<ResourceMetrics> allResourceMetrics = exportRequests.stream().map(serialized -> {
try {
return ExportMetricsServiceRequest.parseFrom(serialized);
} catch (InvalidProtocolBufferException e) {
throw new AssertionError(e);
}
}).flatMap(request -> request.getResourceMetricsList().stream()).collect(toList());
List<MetricData> metrics = new ArrayList<>();
for (ResourceMetrics resourceMetrics : allResourceMetrics) {
Resource resource = resourceMetrics.getResource();
for (InstrumentationLibraryMetrics ilMetrics : resourceMetrics.getInstrumentationLibraryMetricsList()) {
InstrumentationLibrary instrumentationLibrary = ilMetrics.getInstrumentationLibrary();
for (Metric metric : ilMetrics.getMetricsList()) {
metrics.add(createMetricData(metric, io.opentelemetry.sdk.resources.Resource.create(fromProto(resource.getAttributesList())), InstrumentationLibraryInfo.create(instrumentationLibrary.getName(), instrumentationLibrary.getVersion())));
}
}
}
return metrics;
}
Aggregations