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);
}
}
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());
}
}
}
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));
}
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));
}
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);
}
Aggregations