io.opencensus.stats.View.Name Java Examples

The following examples show how to use io.opencensus.stats.View.Name. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAllExportedViews() {
  assertThat(viewManager.getAllExportedViews()).isEmpty();
  View cumulativeView1 =
      createCumulativeView(
          View.Name.create("View 1"), MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY));
  View cumulativeView2 =
      createCumulativeView(
          View.Name.create("View 2"), MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY));
  View intervalView =
      View.create(
          View.Name.create("View 3"),
          VIEW_DESCRIPTION,
          MEASURE_DOUBLE,
          DISTRIBUTION,
          Arrays.asList(KEY),
          INTERVAL);
  viewManager.registerView(cumulativeView1);
  viewManager.registerView(cumulativeView2);
  viewManager.registerView(intervalView);

  // Only cumulative views should be exported.
  assertThat(viewManager.getAllExportedViews()).containsExactly(cumulativeView1, cumulativeView2);
}
 
Example #2
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void getAllExportedViewsResultIsUnmodifiable() {
  View view1 =
      View.create(
          View.Name.create("View 1"),
          VIEW_DESCRIPTION,
          MEASURE_DOUBLE,
          DISTRIBUTION,
          Arrays.asList(KEY),
          CUMULATIVE);
  viewManager.registerView(view1);
  Set<View> exported = viewManager.getAllExportedViews();

  View view2 =
      View.create(
          View.Name.create("View 2"),
          VIEW_DESCRIPTION,
          MEASURE_DOUBLE,
          DISTRIBUTION,
          Arrays.asList(KEY),
          CUMULATIVE);
  thrown.expect(UnsupportedOperationException.class);
  exported.add(view2);
}
 
Example #3
Source File: PubsubMessageToObjectNode.java    From gcp-ingestion with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Register a view for every measure.
 *
 * <p>If this is not called, e.g. during unit tests, recorded values will not be exported.
 */
private static void setupOpenCensus() {
  ViewManager viewManager = Stats.getViewManager();
  for (MeasureLong measure : ImmutableList.of(COERCED_TO_INT, NOT_COERCED_TO_INT,
      NOT_COERCED_TO_BOOL)) {
    viewManager.registerView(View.create(Name.create(measure.getName()),
        measure.getDescription(), measure, COUNT_AGGREGATION, ImmutableList.of()));
  }
}
 
Example #4
Source File: NoopViewManagerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getAllExportedViews() {
  ViewManager viewManager = NoopStats.newNoopViewManager();
  assertThat(viewManager.getAllExportedViews()).isEmpty();
  View cumulativeView1 =
      View.create(
          View.Name.create("View 1"),
          VIEW_DESCRIPTION,
          MEASURE,
          AGGREGATION,
          Arrays.asList(KEY),
          CUMULATIVE);
  View cumulativeView2 =
      View.create(
          View.Name.create("View 2"),
          VIEW_DESCRIPTION,
          MEASURE,
          AGGREGATION,
          Arrays.asList(KEY),
          CUMULATIVE);
  View intervalView =
      View.create(
          View.Name.create("View 3"),
          VIEW_DESCRIPTION,
          MEASURE,
          AGGREGATION,
          Arrays.asList(KEY),
          INTERVAL);
  viewManager.registerView(cumulativeView1);
  viewManager.registerView(cumulativeView2);
  viewManager.registerView(intervalView);

  // Only cumulative views should be exported.
  assertThat(viewManager.getAllExportedViews()).containsExactly(cumulativeView1, cumulativeView2);
}
 
Example #5
Source File: NoopViewManagerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getAllExportedViews_ResultIsUnmodifiable() {
  ViewManager viewManager = NoopStats.newNoopViewManager();
  View view1 =
      View.create(
          View.Name.create("View 1"), VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY));
  viewManager.registerView(view1);
  Set<View> exported = viewManager.getAllExportedViews();

  View view2 =
      View.create(
          View.Name.create("View 2"), VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY));
  thrown.expect(UnsupportedOperationException.class);
  exported.add(view2);
}
 
Example #6
Source File: Quickstart.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
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.emptyList());

  ViewManager viewManager = Stats.getViewManager();
  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));
}
 
Example #7
Source File: Repl.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private static void registerAllViews() {
  // Defining the distribution aggregations
  Aggregation latencyDistribution =
      Distribution.create(
          BucketBoundaries.create(
              Arrays.asList(
                  // [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms,
                  // >=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(
                  // [>=0B, >=5B, >=10B, >=20B, >=40B, >=60B, >=80B, >=100B, >=200B, >=400B,
                  // >=600B,
                  // >=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);
  }
}
 
Example #8
Source File: StackdriverQuickstart.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
/** 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));
}
 
Example #9
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private static View createCumulativeView(
    View.Name name, Measure measure, Aggregation aggregation, List<TagKey> keys) {
  return View.create(name, VIEW_DESCRIPTION, measure, aggregation, keys, CUMULATIVE);
}
 
Example #10
Source File: OpenCensusPluginSink.java    From ffwd with Apache License 2.0 4 votes vote down vote up
public void sendMetric(Metric metric) {
  try {
    String metricName = getOutputMetricName(metric);
    MeasureLong measure = measures.get(metricName);

    if (measure == null) {
      if (measures.size() > maxViews) {
        throw new RuntimeException("maxViews exceeded. " +
            "Please increase in configuration or decrease number of metrics.");
      }

      measure = MeasureLong.create("Events", "Number of Events", "1");
      measures.put(metricName, measure);

      // Stackdriver expects each metric to have the same set of tags so metrics
      // missing tags will be rejected. NB by default stackdriver will create
      // the metricDescription based on the first metric received so be consistant
      // from the start.
      final List<TagKey> columns = new ArrayList<TagKey>(metric.getTags().size());
      metric.getTags().keySet().forEach(tagName -> {
          columns.add(TagKey.create(sanitizeName(tagName)));
      });
      final View view =
          View.create(
              Name.create(metricName),
              metricName,
              measure,
              Sum.create(),
              columns);

      Stats.getViewManager().registerView(view);
    }
    final TagContextBuilder builder = tagger.emptyBuilder();
    metric.getTags().forEach((k, v) -> {
        builder.putPropagating(TagKey.create(sanitizeName(k)), TagValue.create(v));
    });
    final TagContext context = builder.build();

    statsRecorder.newMeasureMap().put(measure, (long) metric.getValue()).record(context);
  } catch (Exception ex) {
    log.error("Couldn't send metric %s", ex);
    throw ex;
  }
}
 
Example #11
Source File: OpenCensusMetricExporterSpi.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** */
private void addView(Measure msr) {
    View v = View.create(Name.create(msr.getName()), msr.getDescription(), msr, LastValue.create(), tags);

    Stats.getViewManager().registerView(v);
}