use of io.opentelemetry.proto.metrics.v1.ResourceMetrics 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.ResourceMetrics 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.ResourceMetrics in project opentelemetry-java by open-telemetry.
the class MetricsRequestMarshalerTest method protoResourceMetrics.
@Test
void protoResourceMetrics() {
Resource resource = Resource.create(Attributes.of(stringKey("ka"), "va"), "http://resource.url");
io.opentelemetry.proto.resource.v1.Resource resourceProto = io.opentelemetry.proto.resource.v1.Resource.newBuilder().addAllAttributes(singletonList(KeyValue.newBuilder().setKey("ka").setValue(stringValue("va")).build())).build();
io.opentelemetry.proto.resource.v1.Resource emptyResourceProto = io.opentelemetry.proto.resource.v1.Resource.newBuilder().build();
InstrumentationLibraryInfo instrumentationLibraryInfo = InstrumentationLibraryInfo.create("name", "version", "http://url");
InstrumentationLibrary instrumentationLibraryProto = InstrumentationLibrary.newBuilder().setName("name").setVersion("version").build();
InstrumentationLibrary emptyInstrumentationLibraryProto = InstrumentationLibrary.newBuilder().setName("").setVersion("").build();
Metric metricDoubleSum = Metric.newBuilder().setName("name").setDescription("description").setUnit("1").setSum(Sum.newBuilder().setIsMonotonic(true).setAggregationTemporality(AGGREGATION_TEMPORALITY_CUMULATIVE).addDataPoints(NumberDataPoint.newBuilder().setStartTimeUnixNano(123).setTimeUnixNano(456).addAllAttributes(singletonList(KeyValue.newBuilder().setKey("k").setValue(stringValue("v")).build())).setAsDouble(5.0).build()).build()).build();
assertThat(toProtoResourceMetrics(ImmutableList.of(MetricData.createDoubleSum(resource, instrumentationLibraryInfo, "name", "description", "1", ImmutableSumData.create(/* isMonotonic= */
true, AggregationTemporality.CUMULATIVE, Collections.singletonList(ImmutableDoublePointData.create(123, 456, KV_ATTR, 5.0)))), MetricData.createDoubleSum(resource, instrumentationLibraryInfo, "name", "description", "1", ImmutableSumData.create(/* isMonotonic= */
true, AggregationTemporality.CUMULATIVE, Collections.singletonList(ImmutableDoublePointData.create(123, 456, KV_ATTR, 5.0)))), MetricData.createDoubleSum(Resource.empty(), instrumentationLibraryInfo, "name", "description", "1", ImmutableSumData.create(/* isMonotonic= */
true, AggregationTemporality.CUMULATIVE, Collections.singletonList(ImmutableDoublePointData.create(123, 456, KV_ATTR, 5.0)))), MetricData.createDoubleSum(Resource.empty(), InstrumentationLibraryInfo.empty(), "name", "description", "1", ImmutableSumData.create(/* isMonotonic= */
true, AggregationTemporality.CUMULATIVE, Collections.singletonList(ImmutableDoublePointData.create(123, 456, KV_ATTR, 5.0))))))).satisfiesExactlyInAnyOrder(resourceMetrics -> {
assertThat(resourceMetrics.getResource()).isEqualTo(resourceProto);
assertThat(resourceMetrics.getSchemaUrl()).isEqualTo("http://resource.url");
assertThat(resourceMetrics.getInstrumentationLibraryMetricsList()).containsExactlyInAnyOrder(InstrumentationLibraryMetrics.newBuilder().setInstrumentationLibrary(instrumentationLibraryProto).addAllMetrics(ImmutableList.of(metricDoubleSum, metricDoubleSum)).setSchemaUrl("http://url").build());
}, resourceMetrics -> {
assertThat(resourceMetrics.getResource()).isEqualTo(emptyResourceProto);
assertThat(resourceMetrics.getInstrumentationLibraryMetricsList()).containsExactlyInAnyOrder(InstrumentationLibraryMetrics.newBuilder().setInstrumentationLibrary(emptyInstrumentationLibraryProto).addAllMetrics(singletonList(metricDoubleSum)).build(), InstrumentationLibraryMetrics.newBuilder().setInstrumentationLibrary(instrumentationLibraryProto).addAllMetrics(singletonList(metricDoubleSum)).setSchemaUrl("http://url").build());
});
}
use of io.opentelemetry.proto.metrics.v1.ResourceMetrics in project opentelemetry-java by open-telemetry.
the class OtlpHttpMetricExporterTest method exportAndAssertResult.
private static ExportMetricsServiceRequest exportAndAssertResult(OtlpHttpMetricExporter otlpHttpMetricExporter, boolean expectedResult) {
List<MetricData> metrics = Collections.singletonList(generateFakeMetric());
CompletableResultCode resultCode = otlpHttpMetricExporter.export(metrics);
resultCode.join(10, TimeUnit.SECONDS);
assertThat(resultCode.isSuccess()).isEqualTo(expectedResult);
List<ResourceMetrics> resourceMetrics = Arrays.stream(ResourceMetricsMarshaler.create(metrics)).map(marshaler -> {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
marshaler.writeBinaryTo(bos);
return ResourceMetrics.parseFrom(bos.toByteArray());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}).collect(Collectors.toList());
return ExportMetricsServiceRequest.newBuilder().addAllResourceMetrics(resourceMetrics).build();
}
use of io.opentelemetry.proto.metrics.v1.ResourceMetrics 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