use of io.opencensus.stats.View 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.stats.View in project instrumentation-java by census-instrumentation.
the class StackdriverQuickstart method main.
/**
* Main launcher for the Stackdriver example.
*/
public static void main(String[] args) throws IOException, InterruptedException {
// Register the view. It is imperative that this step exists,
// otherwise recorded metrics will be dropped and never exported.
View view = View.create(Name.create("task_latency_distribution"), "The distribution of the task latencies.", LATENCY_MS, Aggregation.Distribution.create(LATENCY_BOUNDARIES), Collections.<TagKey>emptyList());
// Create the view manager
ViewManager viewManager = Stats.getViewManager();
// Then finally register the views
viewManager.registerView(view);
// [START setup_exporter]
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
// Exporters use Application Default Credentials to authenticate.
// See https://developers.google.com/identity/protocols/application-default-credentials
// for more details.
StackdriverStatsExporter.createAndRegister();
// [END setup_exporter]
// Record 100 fake latency values between 0 and 5 seconds.
Random rand = new Random();
for (int i = 0; i < 100; i++) {
long ms = (long) (TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS) * rand.nextDouble());
System.out.println(String.format("Latency %d: %d", i, ms));
STATS_RECORDER.newMeasureMap().put(LATENCY_MS, ms).record();
}
// The default export interval is 60 seconds. The thread with the StackdriverStatsExporter must
// live for at least the interval past any metrics that must be collected, or some risk being
// lost if they are recorded after the last export.
System.out.println(String.format("Sleeping %d seconds before shutdown to ensure all records are flushed.", EXPORT_INTERVAL));
Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPORT_INTERVAL, TimeUnit.SECONDS));
}
use of io.opencensus.stats.View in project instrumentation-java by census-instrumentation.
the class StatszZPageHandlerTest method assertContainsViewData.
private static void assertContainsViewData(OutputStream output, ViewData viewData) {
View view = viewData.getView();
assertThat(output.toString()).contains(view.getName().asString());
assertThat(output.toString()).contains(view.getDescription());
assertThat(output.toString()).contains(view.getMeasure().getName());
for (TagKey tagKey : view.getColumns()) {
assertThat(output.toString()).contains(tagKey.getName());
}
String aggregationType = view.getAggregation().match(Functions.returnConstant("Sum"), Functions.returnConstant("Count"), Functions.returnConstant("Distribution"), Functions.returnConstant("Last Value"), new Function<Aggregation, String>() {
@Override
public String apply(Aggregation arg) {
if (arg instanceof Aggregation.Mean) {
return "Mean";
}
throw new AssertionError();
}
});
assertThat(output.toString()).contains(aggregationType);
for (Map.Entry<List<TagValue>, AggregationData> /*@Nullable*/
entry : viewData.getAggregationMap().entrySet()) {
List<TagValue> tagValues = entry.getKey();
for (TagValue tagValue : tagValues) {
String tagValueStr = tagValue == null ? "" : tagValue.asString();
assertThat(output.toString()).contains(tagValueStr);
}
entry.getValue().match(Functions.</*@Nullable*/
Void>throwAssertionError(), Functions.</*@Nullable*/
Void>throwAssertionError(), Functions.</*@Nullable*/
Void>throwAssertionError(), new Function<AggregationData.DistributionData, Void>() {
@Override
public Void apply(AggregationData.DistributionData arg) {
assertThat(output.toString()).contains(String.valueOf(arg.getCount()));
assertThat(output.toString()).contains(String.valueOf(arg.getMax()));
assertThat(output.toString()).contains(String.valueOf(arg.getMin()));
assertThat(output.toString()).contains(String.valueOf(arg.getMean()));
assertThat(output.toString()).contains(String.valueOf(arg.getSumOfSquaredDeviations()));
return null;
}
}, Functions.</*@Nullable*/
Void>throwAssertionError(), Functions.</*@Nullable*/
Void>throwAssertionError(), new Function<AggregationData, Void>() {
@Override
public Void apply(AggregationData arg) {
if (arg instanceof MeanData) {
MeanData meanData = (MeanData) arg;
assertThat(output.toString()).contains(String.valueOf(meanData.getCount()));
assertThat(output.toString()).contains(String.valueOf(meanData.getMean()));
return null;
}
throw new AssertionError();
}
});
}
}
use of io.opencensus.stats.View in project instrumentation-java by census-instrumentation.
the class StatsRecorderImplTest method record_UnregisteredMeasure.
@Test
public void record_UnregisteredMeasure() {
View view = View.create(VIEW_NAME, "description", MEASURE_DOUBLE, Sum.create(), Arrays.asList(KEY), Cumulative.create());
viewManager.registerView(view);
statsRecorder.newMeasureMap().put(MEASURE_DOUBLE_NO_VIEW_1, 1.0).put(MEASURE_DOUBLE, 2.0).put(MEASURE_DOUBLE_NO_VIEW_2, 3.0).record(new SimpleTagContext(Tag.create(KEY, VALUE)));
ViewData viewData = viewManager.getView(VIEW_NAME);
// There should be one entry.
StatsTestUtil.assertAggregationMapEquals(viewData.getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 2.0)), 1e-6);
}
use of io.opencensus.stats.View in project instrumentation-java by census-instrumentation.
the class StatsRecorderImplTest method record_WithAttachments_DistributionNoHistogram.
@Test
public void record_WithAttachments_DistributionNoHistogram() {
testClock.setTime(START_TIME);
View view = View.create(VIEW_NAME, "description", MEASURE_DOUBLE, DISTRIBUTION_NO_HISTOGRAM, Arrays.asList(KEY));
viewManager.registerView(view);
recordWithAttachments();
ViewData viewData = viewManager.getView(VIEW_NAME);
assertThat(viewData).isNotNull();
DistributionData distributionData = (DistributionData) viewData.getAggregationMap().get(Collections.singletonList(VALUE));
// Recording exemplar has no effect if there's no histogram.
assertThat(distributionData.getExemplars()).isEmpty();
}
Aggregations