Search in sources :

Example 16 with ViewData

use of io.opencensus.stats.ViewData in project instrumentation-java by census-instrumentation.

the class StatszZPageHandler method emitHtmlBody.

private void emitHtmlBody(Map<String, String> queryMap, PrintWriter out, Formatter formatter) {
    synchronized (monitor) {
        groupViewsByDirectoriesAndGetMeasures(viewManager.getAllExportedViews(), root, measures, cachedViews);
        out.write("<p class=\"header\">" + "<img class=\"oc\" src=\"https://opencensus.io/img/logo-sm.svg\" />" + "Open<span>Census</span></p>");
        out.write("<link href=\"https://fonts.googleapis.com/css?family=Open+Sans:300\"" + "rel=\"stylesheet\">\n");
        out.write("<link href=\"https://fonts.googleapis.com/css?family=Roboto\"" + "rel=\"stylesheet\">\n");
        out.write("<h1><a href='?'>StatsZ</a></h1>");
        out.write("<p></p>");
        String path = queryMap.get(QUERY_PATH);
        TreeNode current = findNode(path);
        emitDirectoryTable(current, path, out, formatter);
        if (current != null && current.viewName != null) {
            ViewData viewData = viewManager.getView(current.viewName);
            emitViewData(viewData, current.viewName, out, formatter);
        }
        emitMeasureTable(measures, out, formatter);
    }
}
Also used : ViewData(io.opencensus.stats.ViewData)

Example 17 with ViewData

use of io.opencensus.stats.ViewData in project instrumentation-java by census-instrumentation.

the class RpczZPageHandler method getStatsSnapshots.

private void getStatsSnapshots(Map<String, StatsSnapshot> map, List<View> views) {
    for (View view : views) {
        ViewData viewData = viewManager.getView(view.getName());
        if (viewData == null) {
            continue;
        }
        for (Entry<List<TagValue>, AggregationData> /*@Nullable*/
        entry : viewData.getAggregationMap().entrySet()) {
            TagValue tagValue;
            List<TagValue> /*@Nullable*/
            tagValues = entry.getKey();
            if (tagValues.size() == 1) {
                tagValue = tagValues.get(0);
            } else {
                // Error count views have two tag key: status and method.
                tagValue = tagValues.get(1);
            }
            String method = tagValue == null ? "" : tagValue.asString();
            StatsSnapshot snapshot = map.get(method);
            if (snapshot == null) {
                snapshot = new StatsSnapshot();
                map.put(method, snapshot);
            }
            getStats(snapshot, entry.getValue(), view, viewData.getWindowData());
        }
    }
}
Also used : ViewData(io.opencensus.stats.ViewData) AggregationData(io.opencensus.stats.AggregationData) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) TagValue(io.opencensus.tags.TagValue) View(io.opencensus.stats.View)

Example 18 with ViewData

use of io.opencensus.stats.ViewData in project instrumentation-java by census-instrumentation.

the class QuickStart method main.

/**
 * Main launcher for the QuickStart example.
 */
public static void main(String[] args) throws InterruptedException {
    TagContextBuilder tagContextBuilder = tagger.currentBuilder().put(FRONTEND_KEY, TagValue.create("mobile-ios9.3.5"));
    SpanBuilder spanBuilder = tracer.spanBuilder("my.org/ProcessVideo").setRecordEvents(true).setSampler(Samplers.alwaysSample());
    viewManager.registerView(VIDEO_SIZE_VIEW);
    LoggingTraceExporter.register();
    // Record the processed video size.
    try (Scope scopedTags = tagContextBuilder.buildScoped();
        Scope scopedSpan = spanBuilder.startScopedSpan()) {
        tracer.getCurrentSpan().addAnnotation("Start processing video.");
        // Sleep for [0,10] milliseconds to fake work.
        Thread.sleep(new Random().nextInt(10) + 1);
        statsRecorder.newMeasureMap().put(VIDEO_SIZE, 25 * MiB).record();
        tracer.getCurrentSpan().addAnnotation("Finished processing video.");
    } catch (Exception e) {
        tracer.getCurrentSpan().addAnnotation("Exception thrown when processing video.");
        tracer.getCurrentSpan().setStatus(Status.UNKNOWN);
        logger.severe(e.getMessage());
    }
    logger.info("Wait longer than the reporting duration...");
    // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
    // TODO(songya): remove the gap once we add a shutdown hook for exporting unflushed spans.
    Thread.sleep(5100);
    ViewData viewData = viewManager.getView(VIDEO_SIZE_VIEW_NAME);
    logger.info(String.format("Recorded stats for %s:\n %s", VIDEO_SIZE_VIEW_NAME.asString(), viewData));
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) Scope(io.opencensus.common.Scope) Random(java.util.Random) ViewData(io.opencensus.stats.ViewData) TagContextBuilder(io.opencensus.tags.TagContextBuilder)

Example 19 with ViewData

use of io.opencensus.stats.ViewData in project instrumentation-java by census-instrumentation.

the class StatsRecorderImplTest method record_CurrentContextSet.

@Test
public void record_CurrentContextSet() {
    View view = View.create(VIEW_NAME, "description", MEASURE_DOUBLE, Sum.create(), Arrays.asList(KEY), Cumulative.create());
    viewManager.registerView(view);
    TagContext tags = new SimpleTagContext(Tag.create(KEY, VALUE));
    Context orig = ContextUtils.withValue(Context.current(), tags).attach();
    try {
        statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0).record();
    } finally {
        Context.current().detach(orig);
    }
    ViewData viewData = viewManager.getView(VIEW_NAME);
    // record() should have used the given TagContext.
    assertThat(viewData.getAggregationMap().keySet()).containsExactly(Arrays.asList(VALUE));
}
Also used : SimpleTagContext(io.opencensus.implcore.stats.StatsTestUtil.SimpleTagContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) SimpleTagContext(io.opencensus.implcore.stats.StatsTestUtil.SimpleTagContext) TagContext(io.opencensus.tags.TagContext) SimpleTagContext(io.opencensus.implcore.stats.StatsTestUtil.SimpleTagContext) StatsTestUtil.createEmptyViewData(io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData) ViewData(io.opencensus.stats.ViewData) View(io.opencensus.stats.View) Test(org.junit.Test)

Example 20 with ViewData

use of io.opencensus.stats.ViewData in project instrumentation-java by census-instrumentation.

the class ViewManagerImplTest method testRecordWithEmptyStatsContext.

@Test
public void testRecordWithEmptyStatsContext() {
    viewManager.registerView(createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY)));
    // DEFAULT doesn't have tags, but the view has tag key "KEY".
    statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 10.0).record(tagger.empty());
    ViewData viewData = viewManager.getView(VIEW_NAME);
    assertAggregationMapEquals(viewData.getAggregationMap(), ImmutableMap.of(// "unknown/not set".
    Arrays.asList(RecordUtils.UNKNOWN_TAG_VALUE), // Should record stats with default tag value: "KEY" : "unknown/not set".
    createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 10.0)), EPSILON);
}
Also used : StatsTestUtil.createEmptyViewData(io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData) ViewData(io.opencensus.stats.ViewData) Test(org.junit.Test)

Aggregations

ViewData (io.opencensus.stats.ViewData)27 StatsTestUtil.createEmptyViewData (io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData)21 Test (org.junit.Test)20 View (io.opencensus.stats.View)18 TagContext (io.opencensus.tags.TagContext)5 SimpleTagContext (io.opencensus.implcore.stats.StatsTestUtil.SimpleTagContext)4 Timestamp (io.opencensus.common.Timestamp)3 MeasureMap (io.opencensus.stats.MeasureMap)3 CurrentState (io.opencensus.implcore.internal.CurrentState)2 Aggregation (io.opencensus.stats.Aggregation)2 DistributionData (io.opencensus.stats.AggregationData.DistributionData)2 TagValue (io.opencensus.tags.TagValue)2 ImmutableList (com.google.common.collect.ImmutableList)1 Context (io.grpc.Context)1 Scope (io.opencensus.common.Scope)1 Exemplar (io.opencensus.metrics.data.Exemplar)1 AggregationData (io.opencensus.stats.AggregationData)1 CountData (io.opencensus.stats.AggregationData.CountData)1 LastValueDataDouble (io.opencensus.stats.AggregationData.LastValueDataDouble)1 SumDataDouble (io.opencensus.stats.AggregationData.SumDataDouble)1