Search in sources :

Example 1 with ExportMetricsServiceRequest

use of io.opencensus.proto.agent.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.opencensus.proto.agent.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)

Aggregations

ExportMetricsServiceRequest (io.opencensus.proto.agent.metrics.v1.ExportMetricsServiceRequest)2 Test (org.junit.Test)2 LongGauge (io.opencensus.metrics.LongGauge)1 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)1 Node (io.opencensus.proto.agent.common.v1.Node)1 Metric (io.opencensus.proto.metrics.v1.Metric)1 View (io.opencensus.stats.View)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1