Search in sources :

Example 1 with ExportMetricsServiceRequest

use of io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest in project instrumentation-java by census-instrumentation.

the class OcAgentMetricsExporterIntegrationTest method testExportMetrics.

@Test
public void testExportMetrics() throws InterruptedException, IOException {
    // Mock a real-life scenario in production, where Agent is not enabled at first, then enabled
    // after an outage. Users should be able to see metrics shortly after Agent is up.
    registerAllViews();
    LongGauge gauge = registerGauge();
    // Register the OcAgent Exporter first.
    // Agent is not yet up and running so Exporter will just retry connection.
    OcAgentMetricsExporter.createAndRegister(OcAgentMetricsExporterConfiguration.builder().setServiceName(SERVICE_NAME).setUseInsecure(true).setRetryInterval(RETRY_INTERVAL).setExportInterval(EXPORT_INTERVAL).build());
    doWork(5, gauge.getOrCreateTimeSeries(Collections.singletonList(LabelValue.create("First work"))));
    // Wait 3s so that all metrics get exported.
    Thread.sleep(3000);
    // No interaction with Agent so far.
    assertThat(fakeOcAgentMetricsServiceGrpc.getExportMetricsServiceRequests()).isEmpty();
    // Imagine that an outage happened, now start Agent. Exporter should be able to connect to Agent
    // after the next retry interval.
    agent.start();
    // Wait 3s for Exporter to start another attempt to connect to Agent.
    Thread.sleep(3000);
    doWork(8, gauge.getOrCreateTimeSeries(Collections.singletonList(LabelValue.create("Second work"))));
    // Wait 3s so that all metrics get exported.
    Thread.sleep(3000);
    List<ExportMetricsServiceRequest> exportRequests = fakeOcAgentMetricsServiceGrpc.getExportMetricsServiceRequests();
    assertThat(exportRequests.size()).isAtLeast(2);
    ExportMetricsServiceRequest firstRequest = exportRequests.get(0);
    Node expectedNode = OcAgentNodeUtils.getNodeInfo(SERVICE_NAME);
    Node actualNode = firstRequest.getNode();
    assertThat(actualNode.getIdentifier().getHostName()).isEqualTo(expectedNode.getIdentifier().getHostName());
    assertThat(actualNode.getIdentifier().getPid()).isEqualTo(expectedNode.getIdentifier().getPid());
    assertThat(actualNode.getLibraryInfo()).isEqualTo(expectedNode.getLibraryInfo());
    assertThat(actualNode.getServiceInfo()).isEqualTo(expectedNode.getServiceInfo());
    List<Metric> metricProtos = new ArrayList<>();
    for (int i = 1; i < exportRequests.size(); i++) {
        metricProtos.addAll(exportRequests.get(i).getMetricsList());
    }
    // There should be at least one metric exported for each view and gauge (4 + 1).
    assertThat(metricProtos.size()).isAtLeast(5);
    Set<String> expectedMetrics = new HashSet<>();
    expectedMetrics.add("jobs");
    for (View view : VIEWS) {
        expectedMetrics.add(view.getName().asString());
    }
    Set<String> actualMetrics = new HashSet<>();
    for (Metric metricProto : metricProtos) {
        actualMetrics.add(metricProto.getMetricDescriptor().getName());
    }
    assertThat(actualMetrics).containsAtLeastElementsIn(expectedMetrics);
}
Also used : LongGauge(io.opencensus.metrics.LongGauge) Node(io.opencensus.proto.agent.common.v1.Node) ArrayList(java.util.ArrayList) Metric(io.opencensus.proto.metrics.v1.Metric) View(io.opencensus.stats.View) LongPoint(io.opencensus.metrics.LongGauge.LongPoint) ExportMetricsServiceRequest(io.opencensus.proto.agent.metrics.v1.ExportMetricsServiceRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ExportMetricsServiceRequest

use of io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest in project opentelemetry-java by open-telemetry.

the class OtlpHttpMetricExporterTest method testExportUncompressed.

@Test
void testExportUncompressed() {
    server.enqueue(successResponse());
    OtlpHttpMetricExporter exporter = builder.build();
    ExportMetricsServiceRequest payload = exportAndAssertResult(exporter, /* expectedResult= */
    true);
    AggregatedHttpRequest request = server.takeRequest().request();
    assertRequestCommon(request);
    assertThat(parseRequestBody(request.content().array())).isEqualTo(payload);
}
Also used : AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest) Test(org.junit.jupiter.api.Test)

Example 3 with ExportMetricsServiceRequest

use of io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest 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()));
}
Also used : ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) Meter(io.opentelemetry.api.metrics.Meter) NumberDataPoint(io.opentelemetry.proto.metrics.v1.NumberDataPoint) MetricReaderFactory(io.opentelemetry.sdk.metrics.export.MetricReaderFactory) Metric(io.opentelemetry.proto.metrics.v1.Metric) Sum(io.opentelemetry.proto.metrics.v1.Sum) InstrumentationLibraryMetrics(io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics) LongCounter(io.opentelemetry.api.metrics.LongCounter) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest)

Example 4 with ExportMetricsServiceRequest

use of io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest in project instrumentation-java by census-instrumentation.

the class OcAgentMetricsServiceExportRpcHandlerTest method export_createAndExport.

@Test
public void export_createAndExport() {
    OcAgentMetricsServiceExportRpcHandler exportRpcHandler = OcAgentMetricsServiceExportRpcHandler.create(getStub(serverName));
    ExportMetricsServiceRequest request = ExportMetricsServiceRequest.newBuilder().setNode(NODE).build();
    exportRpcHandler.onExport(request);
    assertThat(traceServiceGrpc.getExportMetricsServiceRequests()).containsExactly(request);
}
Also used : ExportMetricsServiceRequest(io.opencensus.proto.agent.metrics.v1.ExportMetricsServiceRequest) Test(org.junit.Test)

Example 5 with ExportMetricsServiceRequest

use of io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest 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());
}
Also used : ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) Meter(io.opentelemetry.api.metrics.Meter) ArrayList(java.util.ArrayList) InstrumentationLibraryMetrics(io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) LogEmitter(io.opentelemetry.sdk.logs.LogEmitter) LogRecord(io.opentelemetry.proto.logs.v1.LogRecord) Metric(io.opentelemetry.proto.metrics.v1.Metric) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ExportMetricsServiceRequest (io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest)5 Test (org.junit.jupiter.api.Test)4 AggregatedHttpRequest (com.linecorp.armeria.common.AggregatedHttpRequest)3 ResourceMetrics (io.opentelemetry.proto.metrics.v1.ResourceMetrics)3 ExportMetricsServiceRequest (io.opencensus.proto.agent.metrics.v1.ExportMetricsServiceRequest)2 Meter (io.opentelemetry.api.metrics.Meter)2 InstrumentationLibraryMetrics (io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics)2 Metric (io.opentelemetry.proto.metrics.v1.Metric)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 Status (com.google.rpc.Status)1 HttpMethod (com.linecorp.armeria.common.HttpMethod)1 HttpResponse (com.linecorp.armeria.common.HttpResponse)1 HttpStatus (com.linecorp.armeria.common.HttpStatus)1 MediaType (com.linecorp.armeria.common.MediaType)1 ServerBuilder (com.linecorp.armeria.server.ServerBuilder)1 MockWebServerExtension (com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension)1 LogCapturer (io.github.netmikey.logunit.api.LogCapturer)1 LongGauge (io.opencensus.metrics.LongGauge)1 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)1