use of io.opencensus.metrics.LongGauge 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);
}
use of io.opencensus.metrics.LongGauge in project instrumentation-java by census-instrumentation.
the class MetricRegistryImplTest method addLongGauge_GetMetrics.
@Test
public void addLongGauge_GetMetrics() {
LongGauge longGauge = metricRegistry.addLongGauge(NAME, METRIC_OPTIONS);
longGauge.getOrCreateTimeSeries(LABEL_VALUES);
Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
assertThat(metricCollections.size()).isEqualTo(1);
assertThat(metricCollections).containsExactly(Metric.createWithOneTimeSeries(LONG_METRIC_DESCRIPTOR, TimeSeries.createWithOnePoint(ALL_VALUES, Point.create(Value.longValue(0), TEST_TIME), null)));
}
use of io.opencensus.metrics.LongGauge in project instrumentation-java by census-instrumentation.
the class MetricRegistryImplTest method getMetrics.
@Test
public void getMetrics() {
LongGauge longGauge = metricRegistry.addLongGauge(NAME, METRIC_OPTIONS);
LongPoint longPoint = longGauge.getOrCreateTimeSeries(LABEL_VALUES);
longPoint.set(200);
DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, METRIC_OPTIONS);
DoublePoint doublePoint = doubleGauge.getOrCreateTimeSeries(LABEL_VALUES);
doublePoint.set(-300.13);
DerivedLongGauge derivedLongGauge = metricRegistry.addDerivedLongGauge(NAME_3, METRIC_OPTIONS);
derivedLongGauge.createTimeSeries(LABEL_VALUES, null, longFunction);
DerivedDoubleGauge derivedDoubleGauge = metricRegistry.addDerivedDoubleGauge(NAME_4, METRIC_OPTIONS);
derivedDoubleGauge.createTimeSeries(LABEL_VALUES, null, doubleFunction);
Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
assertThat(metricCollections.size()).isEqualTo(4);
assertThat(metricCollections).containsExactly(Metric.createWithOneTimeSeries(LONG_METRIC_DESCRIPTOR, TimeSeries.createWithOnePoint(ALL_VALUES, Point.create(Value.longValue(200), TEST_TIME), null)), Metric.createWithOneTimeSeries(DOUBLE_METRIC_DESCRIPTOR, TimeSeries.createWithOnePoint(ALL_VALUES, Point.create(Value.doubleValue(-300.13), TEST_TIME), null)), Metric.createWithOneTimeSeries(DERIVED_LONG_METRIC_DESCRIPTOR, TimeSeries.createWithOnePoint(ALL_VALUES, Point.create(Value.longValue(5), TEST_TIME), null)), Metric.createWithOneTimeSeries(DERIVED_DOUBLE_METRIC_DESCRIPTOR, TimeSeries.createWithOnePoint(ALL_VALUES, Point.create(Value.doubleValue(5.0), TEST_TIME), null)));
}
use of io.opencensus.metrics.LongGauge in project instrumentation-java by census-instrumentation.
the class OcAgentExportersQuickStart method main.
/**
* Main launcher of the example.
*/
public static void main(String[] args) throws InterruptedException {
// Always sample for demo purpose. DO NOT use in production.
configureAlwaysSample();
registerAllViews();
LongGauge gauge = registerGauge();
String endPoint = getStringOrDefaultFromArgs(args, 0, DEFAULT_ENDPOINT);
registerAgentExporters(endPoint);
try (Scope scope = tracer.spanBuilder("root").startScopedSpan()) {
int iteration = 1;
while (true) {
doWork(iteration, random.nextInt(10), gauge);
iteration++;
Thread.sleep(5000);
}
} catch (InterruptedException e) {
logger.info("Thread interrupted, exiting in 5 seconds.");
// Wait 5s so that last batch will be exported.
Thread.sleep(5000);
}
}
use of io.opencensus.metrics.LongGauge in project instrumentation-java by census-instrumentation.
the class MetricRegistryImplTest method shouldReturnSameObjectOnMultipleRegisterCall.
@Test
public void shouldReturnSameObjectOnMultipleRegisterCall() {
LongGauge longGauge = metricRegistry.addLongGauge(NAME, METRIC_OPTIONS);
LongPoint longPoint = longGauge.getOrCreateTimeSeries(LABEL_VALUES);
longPoint.set(200);
LongGauge longGauge1 = metricRegistry.addLongGauge(NAME, METRIC_OPTIONS);
LongPoint longPoint1 = longGauge1.getOrCreateTimeSeries(Collections.singletonList(LABEL_VALUE_2));
longPoint1.set(300);
assertThat(longGauge).isEqualTo(longGauge1);
Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
assertThat(metricCollections.size()).isEqualTo(1);
assertThat(metricCollections).containsExactly(Metric.create(LONG_METRIC_DESCRIPTOR, Arrays.asList(TimeSeries.createWithOnePoint(Arrays.asList(LABEL_VALUE, LABEL_VALUE_2), Point.create(Value.longValue(200), TEST_TIME), null), TimeSeries.createWithOnePoint(Arrays.asList(LABEL_VALUE_2, LABEL_VALUE_2), Point.create(Value.longValue(300), TEST_TIME), null))));
}
Aggregations