use of io.opencensus.stats.Aggregation 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.Aggregation in project instrumentation-java by census-instrumentation.
the class ViewManagerImplTest method testGetCumulativeViewDataWithEmptyBucketBoundaries.
@Test
public void testGetCumulativeViewDataWithEmptyBucketBoundaries() {
Aggregation noHistogram = Distribution.create(BucketBoundaries.create(Collections.<Double>emptyList()));
View view = createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, noHistogram, Arrays.asList(KEY));
clock.setTime(Timestamp.create(1, 0));
viewManager.registerView(view);
statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.1).record(tagger.emptyBuilder().put(KEY, VALUE).build());
clock.setTime(Timestamp.create(3, 0));
ViewData viewData = viewManager.getView(VIEW_NAME);
assertThat(viewData.getWindowData()).isEqualTo(CumulativeData.create(Timestamp.create(1, 0), Timestamp.create(3, 0)));
StatsTestUtil.assertAggregationMapEquals(viewData.getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), StatsTestUtil.createAggregationData(noHistogram, MEASURE_DOUBLE, 1.1)), EPSILON);
}
use of io.opencensus.stats.Aggregation in project instrumentation-java by census-instrumentation.
the class MutableViewDataTest method testTimeRewindsOnDistributionViewNoThrow.
@Test
public void testTimeRewindsOnDistributionViewNoThrow() {
// First we set up some buckets THEN we rewind time for giggles.
Aggregation latencyDistribution = Distribution.create(BucketBoundaries.create(Arrays.asList(0.0, 25.0, 100.0, 200.0, 400.0, 800.0, 10000.0)));
View tester = View.create(View.Name.create("view"), "Description", MeasureDouble.create("name", "desc", "us"), latencyDistribution, Collections.singletonList(TagKey.create("KEY")));
Timestamp start = Timestamp.create(10000000, 0);
Timestamp validPointTime = Timestamp.create(10000010, 0);
CurrentState.State state = CurrentState.State.ENABLED;
MutableViewData viewData = MutableViewData.create(tester, start);
// Create a data points to get thrown away.
viewData.record(TagMapImpl.EMPTY, 1.0, validPointTime, Collections.<String, AttachmentValue>emptyMap());
// Rewind time and look for explosions.
Timestamp thePast = Timestamp.create(0, 0);
ViewData result = viewData.toViewData(thePast, state);
assertThat(result.getAggregationMap()).isEmpty();
}
use of io.opencensus.stats.Aggregation in project instrumentation-java by census-instrumentation.
the class StatszZPageHandler method emitViewDataTableHeader.
private static void emitViewDataTableHeader(View view, PrintWriter out, Formatter formatter) {
out.write("<thead>");
out.write("<tr>");
for (TagKey tagKey : view.getColumns()) {
formatter.format("<th class=\"borderRL\">TagKey: %s (string)</th>", tagKey.getName());
}
String unit = view.getMeasure().getUnit();
view.getAggregation().match(new Function<Sum, Void>() {
@Override
public Void apply(Sum arg) {
formatter.format("<th class=\"borderL\">%s, %s</th>", TABLE_HEADER_SUM, unit);
return null;
}
}, new Function<Count, Void>() {
@Override
public Void apply(Count arg) {
formatter.format("<th class=\"borderL\">%s</th>", TABLE_HEADER_COUNT);
return null;
}
}, new Function<Distribution, Void>() {
@Override
public Void apply(Distribution arg) {
formatter.format("<th>%s, %s</th>", TABLE_HEADER_MEAN, unit);
formatter.format("<th class=\"borderL\">%s</th>", TABLE_HEADER_COUNT);
formatter.format("<th class=\"borderL\">%s, %s</th>", TABLE_HEADER_MAX, unit);
formatter.format("<th class=\"borderL\">%s, %s</th>", TABLE_HEADER_MIN, unit);
formatter.format("<th class=\"borderL\">%s</th>", TABLE_HEADER_DEV);
formatter.format("<th class=\"borderL\">%s</th>", TABLE_HEADER_HISTOGRAM);
return null;
}
}, new Function<LastValue, Void>() {
@Override
public Void apply(LastValue arg) {
formatter.format("<th class=\"borderL\">%s, %s</th>", TABLE_HEADER_LAST_VALUE, unit);
return null;
}
}, new Function<Aggregation, Void>() {
@Override
public Void apply(Aggregation arg) {
// deprecated RPC views.
if (arg instanceof Aggregation.Mean) {
formatter.format("<th>%s, %s</th>", TABLE_HEADER_MEAN, unit);
formatter.format("<th class=\"borderL\">%s</th>", TABLE_HEADER_COUNT);
return null;
}
throw new IllegalArgumentException("Unknown Aggregation.");
}
});
out.write("</tr>");
out.write("</thead>");
}
use of io.opencensus.stats.Aggregation in project instrumentation-java by census-instrumentation.
the class MetricUtils method viewToMetricDescriptor.
@javax.annotation.Nullable
static MetricDescriptor viewToMetricDescriptor(View view) {
if (view.getWindow() instanceof View.AggregationWindow.Interval) {
// Only creates Metric for cumulative stats.
return null;
}
List<LabelKey> labelKeys = new ArrayList<LabelKey>();
for (TagKey tagKey : view.getColumns()) {
// TODO: add description
labelKeys.add(LabelKey.create(tagKey.getName(), ""));
}
Measure measure = view.getMeasure();
Aggregation aggregation = view.getAggregation();
return MetricDescriptor.create(view.getName().asString(), view.getDescription(), getUnit(measure, aggregation), getType(measure, aggregation), labelKeys);
}
Aggregations