Search in sources :

Example 1 with ViewManager

use of io.opencensus.stats.ViewManager 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));
}
Also used : Random(java.util.Random) ViewManager(io.opencensus.stats.ViewManager) View(io.opencensus.stats.View)

Example 2 with ViewManager

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

the class Repl method registerAllViews.

private static void registerAllViews() {
    // Defining the distribution aggregations
    Aggregation latencyDistribution = Distribution.create(BucketBoundaries.create(Arrays.asList(// >=1s, >=2s, >=4s, >=6s]
    0.0, 25.0, 50.0, 75.0, 100.0, 200.0, 400.0, 600.0, 800.0, 1000.0, 2000.0, 4000.0, 6000.0)));
    Aggregation lengthsDistribution = Distribution.create(BucketBoundaries.create(Arrays.asList(// >=800B, >=1000B]
    0.0, 5.0, 10.0, 20.0, 40.0, 60.0, 80.0, 100.0, 200.0, 400.0, 600.0, 800.0, 1000.0)));
    // Define the count aggregation
    Aggregation countAggregation = Aggregation.Count.create();
    // So tagKeys
    List<TagKey> noKeys = new ArrayList<TagKey>();
    // Define the views
    View[] views = new View[] { View.create(Name.create("ocjavametrics/latency"), "The distribution of latencies", M_LATENCY_MS, latencyDistribution, Collections.singletonList(KEY_METHOD)), View.create(Name.create("ocjavametrics/lines_in"), "The number of lines read in from standard input", M_LINES_IN, countAggregation, noKeys), View.create(Name.create("ocjavametrics/errors"), "The number of errors encountered", M_ERRORS, countAggregation, Collections.singletonList(KEY_METHOD)), View.create(Name.create("ocjavametrics/line_lengths"), "The distribution of line lengths", M_LINE_LENGTHS, lengthsDistribution, noKeys) };
    // Create the view manager
    ViewManager vmgr = Stats.getViewManager();
    // Then finally register the views
    for (View view : views) {
        vmgr.registerView(view);
    }
}
Also used : Aggregation(io.opencensus.stats.Aggregation) TagKey(io.opencensus.tags.TagKey) ArrayList(java.util.ArrayList) ViewManager(io.opencensus.stats.ViewManager) View(io.opencensus.stats.View)

Aggregations

View (io.opencensus.stats.View)2 ViewManager (io.opencensus.stats.ViewManager)2 Aggregation (io.opencensus.stats.Aggregation)1 TagKey (io.opencensus.tags.TagKey)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1