Search in sources :

Example 1 with Aggregation

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();
            }
        });
    }
}
Also used : AggregationData(io.opencensus.stats.AggregationData) MeanData(io.opencensus.stats.AggregationData.MeanData) View(io.opencensus.stats.View) Aggregation(io.opencensus.stats.Aggregation) TagKey(io.opencensus.tags.TagKey) List(java.util.List) TagValue(io.opencensus.tags.TagValue) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with Aggregation

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);
}
Also used : Aggregation(io.opencensus.stats.Aggregation) StatsTestUtil.createEmptyViewData(io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData) ViewData(io.opencensus.stats.ViewData) MeasureDouble(io.opencensus.stats.Measure.MeasureDouble) SumDataDouble(io.opencensus.stats.AggregationData.SumDataDouble) LastValueDataDouble(io.opencensus.stats.AggregationData.LastValueDataDouble) View(io.opencensus.stats.View) Test(org.junit.Test)

Example 3 with Aggregation

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();
}
Also used : Aggregation(io.opencensus.stats.Aggregation) ViewData(io.opencensus.stats.ViewData) CurrentState(io.opencensus.implcore.internal.CurrentState) View(io.opencensus.stats.View) Timestamp(io.opencensus.common.Timestamp) Test(org.junit.Test)

Example 4 with Aggregation

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>");
}
Also used : Sum(io.opencensus.stats.Aggregation.Sum) Count(io.opencensus.stats.Aggregation.Count) LastValue(io.opencensus.stats.Aggregation.LastValue) Aggregation(io.opencensus.stats.Aggregation) Distribution(io.opencensus.stats.Aggregation.Distribution) TagKey(io.opencensus.tags.TagKey)

Example 5 with Aggregation

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);
}
Also used : Aggregation(io.opencensus.stats.Aggregation) ArrayList(java.util.ArrayList) TagKey(io.opencensus.tags.TagKey) Measure(io.opencensus.stats.Measure) LabelKey(io.opencensus.metrics.LabelKey) View(io.opencensus.stats.View)

Aggregations

Aggregation (io.opencensus.stats.Aggregation)6 View (io.opencensus.stats.View)5 TagKey (io.opencensus.tags.TagKey)4 ViewData (io.opencensus.stats.ViewData)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Timestamp (io.opencensus.common.Timestamp)1 CurrentState (io.opencensus.implcore.internal.CurrentState)1 StatsTestUtil.createEmptyViewData (io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData)1 LabelKey (io.opencensus.metrics.LabelKey)1 Count (io.opencensus.stats.Aggregation.Count)1 Distribution (io.opencensus.stats.Aggregation.Distribution)1 LastValue (io.opencensus.stats.Aggregation.LastValue)1 Sum (io.opencensus.stats.Aggregation.Sum)1 AggregationData (io.opencensus.stats.AggregationData)1 LastValueDataDouble (io.opencensus.stats.AggregationData.LastValueDataDouble)1 MeanData (io.opencensus.stats.AggregationData.MeanData)1 SumDataDouble (io.opencensus.stats.AggregationData.SumDataDouble)1 Measure (io.opencensus.stats.Measure)1