io.opencensus.stats.Stats Java Examples

The following examples show how to use io.opencensus.stats.Stats. 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: StackdriverMetrics.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
StackdriverMetrics(Gradle gradle, Logger logger) {
  this.logger = logger;
  globalContext = deserializeContext();

  ensureStackdriver(gradle);

  Stats.getViewManager()
      .registerView(
          View.create(
              View.Name.create("fireci/tasklatency"),
              "The latency in milliseconds",
              M_LATENCY,
              Aggregation.LastValue.create(),
              TAG_KEYS));

  Stats.getViewManager()
      .registerView(
          View.create(
              View.Name.create("fireci/tasksuccess"),
              "Indicated success or failure.",
              M_SUCCESS,
              Aggregation.LastValue.create(),
              TAG_KEYS));
}
 
Example #2
Source File: HttpClientHandler.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link HttpClientHandler} with given parameters.
 *
 * @param tracer the Open Census tracing component.
 * @param extractor the {@code HttpExtractor} used to extract information from the
 *     request/response.
 * @param textFormat the {@code TextFormat} used in HTTP propagation.
 * @param setter the setter used when injecting information to the {@code carrier}.
 * @since 0.19
 */
public HttpClientHandler(
    Tracer tracer,
    HttpExtractor<Q, P> extractor,
    TextFormat textFormat,
    TextFormat.Setter<C> setter) {
  super(extractor);
  checkNotNull(setter, "setter");
  checkNotNull(textFormat, "textFormat");
  checkNotNull(tracer, "tracer");
  this.setter = setter;
  this.textFormat = textFormat;
  this.tracer = tracer;
  this.statsRecorder = Stats.getStatsRecorder();
  this.tagger = Tags.getTagger();
}
 
Example #3
Source File: HttpServerHandler.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link HttpServerHandler} with given parameters.
 *
 * @param tracer the Open Census tracing component.
 * @param extractor the {@code HttpExtractor} used to extract information from the
 *     request/response.
 * @param textFormat the {@code TextFormat} used in HTTP propagation.
 * @param getter the getter used when extracting information from the {@code carrier}.
 * @param publicEndpoint set to true for publicly accessible HTTP(S) server. If true then incoming
 *     tracecontext will be added as a link instead of as a parent.
 * @since 0.19
 */
public HttpServerHandler(
    Tracer tracer,
    HttpExtractor<Q, P> extractor,
    TextFormat textFormat,
    TextFormat.Getter<C> getter,
    Boolean publicEndpoint) {
  super(extractor);
  checkNotNull(tracer, "tracer");
  checkNotNull(textFormat, "textFormat");
  checkNotNull(getter, "getter");
  checkNotNull(publicEndpoint, "publicEndpoint");
  this.tracer = tracer;
  this.textFormat = textFormat;
  this.getter = getter;
  this.publicEndpoint = publicEndpoint;
  this.statsRecorder = Stats.getStatsRecorder();
  this.tagger = Tags.getTagger();
}
 
Example #4
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link CensusStatsModule} with the default OpenCensus implementation.
 */
CensusStatsModule(Supplier<Stopwatch> stopwatchSupplier,
    boolean propagateTags, boolean recordStartedRpcs, boolean recordFinishedRpcs,
    boolean recordRealTimeMetrics) {
  this(
      Tags.getTagger(),
      Tags.getTagPropagationComponent().getBinarySerializer(),
      Stats.getStatsRecorder(),
      stopwatchSupplier,
      propagateTags, recordStartedRpcs, recordFinishedRpcs, recordRealTimeMetrics);
}
 
Example #5
Source File: CensusStatsModule.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link CensusStatsModule} with the default OpenCensus implementation.
 */
CensusStatsModule(Supplier<Stopwatch> stopwatchSupplier, boolean propagateTags) {
  this(
      Tags.getTagger(),
      Tags.getTagPropagationComponent().getBinarySerializer(),
      Stats.getStatsRecorder(),
      stopwatchSupplier,
      propagateTags);
}
 
Example #6
Source File: OpenCensusTest.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
private Map.Entry<List<TagValue>, AggregationData> getMetric(String name) {
  sleep(10);
  ViewData viewData = Stats.getViewManager().getView(View.Name.create(name));
  Map<List<TagValue>, AggregationData> values = viewData.getAggregationMap();
  assertEquals(1, values.size());
  for (Map.Entry<List<TagValue>, AggregationData> entry : values.entrySet()) {
    return entry;
  }
  return null;
}
 
Example #7
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 #8
Source File: StackdriverMetrics.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/** Records failure of the execution stage named {@code name}. */
public void measureFailure(Task task) {

  TagContext ctx =
      Tags.getTagger()
          .toBuilder(globalContext)
          .put(STAGE, TagValue.create(task.getName()))
          .put(GRADLE_PROJECT, TagValue.create(task.getProject().getPath()))
          .build();
  Stats.getStatsRecorder().newMeasureMap().put(M_SUCCESS, 0).record(ctx);
}
 
Example #9
Source File: StackdriverMetrics.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/** Records success and latency of the execution stage named {@code name}. */
public void measureSuccess(Task task, long elapsedTime) {

  TagContext ctx =
      Tags.getTagger()
          .toBuilder(globalContext)
          .put(STAGE, TagValue.create(task.getName()))
          .put(GRADLE_PROJECT, TagValue.create(task.getProject().getPath()))
          .build();
  Stats.getStatsRecorder()
      .newMeasureMap()
      .put(M_SUCCESS, 1)
      .put(M_LATENCY, elapsedTime)
      .record(ctx);
}
 
Example #10
Source File: BatchWrite.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.
 */
public Function<InputT, CompletableFuture<Void>> withOpenCensusMetrics() {
  final ViewManager viewManager = Stats.getViewManager();
  ImmutableMap.<MeasureLong, Aggregation>builder().put(batchCount, COUNT_AGG)
      .put(batchBytes, BATCH_BYTES_AGG).put(batchMessages, BATCH_MESSAGES_AGG)
      .put(batchDelay, BATCH_DELAY_AGG).put(totalBytes, SUM_AGG).put(totalMessages, SUM_AGG)
      .build()
      .forEach((measure, aggregation) -> viewManager
          .registerView(View.create(View.Name.create(measure.getName()), measure.getDescription(),
              measure, aggregation, ImmutableList.of())));
  return this;
}
 
Example #11
Source File: GoogleDtpInternalMetricRecorder.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private GoogleDtpInternalMetricRecorder(GoogleCredentials credentials, String projectId)
    throws IOException {
  // 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.
  StackdriverStatsConfiguration configuration = StackdriverStatsConfiguration.builder()
      .setCredentials(credentials)
      .setProjectId(projectId)
      .setExportInterval(io.opencensus.common.Duration.create(EXPORT_INTERVAL_SECONDS, 0))
      .build();
  StackdriverStatsExporter.createAndRegister(configuration);
  this.viewManager = Stats.getViewManager();
  setupViews();
}
 
Example #12
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 #13
Source File: StatsTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void getStatsRecorder() {
  assertThat(Stats.getStatsRecorder()).isInstanceOf(StatsRecorderImpl.class);
}
 
Example #14
Source File: OpenCensusMetrics.java    From google-maps-services-java with Apache License 2.0 4 votes vote down vote up
public static void registerAllViews() {
  registerAllViews(Stats.getViewManager());
}
 
Example #15
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);
}
 
Example #16
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 #17
Source File: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
private static void registerAllViews() {
  Aggregation commandLatencyDistribution =
      Aggregation.Distribution.create(
          BucketBoundaries.create(
              Arrays.asList(
                  0.0, // >=0ms
                  25.0, // >=25ms
                  50.0, // >=50ms
                  100.0, // >=100ms
                  200.0, // >=200ms
                  400.0, // >=400ms
                  800.0, // >=800ms
                  1000.0, // >=1s
                  2000.0, // >=2s
                  5000.0, // >=5s
                  8000.0, // >=8s
                  10000.0 // >=10s
                  )));
  View[] views;
  views =
      new View[] {
        View.create(
            View.Name.create("meghanada/command_latency"),
            "The distribution of the command latencies",
            M_COMMAND_LATENCY_MS,
            commandLatencyDistribution,
            Collections.unmodifiableList(Arrays.asList(KEY_UID, KEY_COMMAND))),
        View.create(
            View.Name.create("meghanada/class_index_size"),
            "The number of class indexes",
            M_CLASS_INDEX,
            Aggregation.LastValue.create(),
            Collections.unmodifiableList(Collections.singletonList(KEY_UID))),
        View.create(
            View.Name.create("meghanada/autocomplete"),
            "The number of autocomplete count",
            M_AUTOCOMPLETE_COUNT,
            Aggregation.Sum.create(),
            Collections.unmodifiableList(Arrays.asList(KEY_UID, KEY_DESCRIPTION))),
        View.create(
            View.Name.create("meghanada/member_cache_hit_rate"),
            "The member cache hit rate",
            M_MEMBER_CACHE_HIT_RATE,
            Aggregation.LastValue.create(),
            Collections.unmodifiableList(Collections.singletonList(KEY_UID))),
        View.create(
            View.Name.create("meghanada/member_cache_load_exception_rate"),
            "The member cache load exception rate",
            M_MEMBER_CACHE_LOAD_ERROR_RATE,
            Aggregation.LastValue.create(),
            Collections.unmodifiableList(Collections.singletonList(KEY_UID))),
        View.create(
            View.Name.create("meghanada/member_cache_miss_rate"),
            "The member cache miss rate",
            M_MEMBER_CACHE_MISS_RATE,
            Aggregation.LastValue.create(),
            Collections.unmodifiableList(Collections.singletonList(KEY_UID))),
        View.create(
            View.Name.create("meghanada/vm_memory"),
            "The vm memory",
            M_MEMORY,
            Aggregation.LastValue.create(),
            Collections.unmodifiableList(Collections.singletonList(KEY_UID))),
      };

  ViewManager vmgr = Stats.getViewManager();
  for (View view : views) {
    vmgr.registerView(view);
  }
}
 
Example #18
Source File: StatsTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void getViewManager() {
  assertThat(Stats.getViewManager()).isInstanceOf(ViewManagerImpl.class);
}
 
Example #19
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 #20
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 #21
Source File: StatsTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void getStatsRecorder() {
  assertThat(Stats.getStatsRecorder()).isInstanceOf(StatsRecorderImpl.class);
}
 
Example #22
Source File: StatsTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void getViewManager() {
  assertThat(Stats.getViewManager()).isInstanceOf(ViewManagerImpl.class);
}
 
Example #23
Source File: RpcViews.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Registers all standard cumulative views.
 *
 * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
 *
 * @since 0.11.0
 * @deprecated in favor of {@link #registerAllGrpcViews()}. It is likely that there won't be stats
 *     for the old views, but you may still want to register the old views before they are
 *     completely removed.
 */
@Deprecated
public static void registerAllCumulativeViews() {
  registerAllCumulativeViews(Stats.getViewManager());
}
 
Example #24
Source File: RpcViews.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Registers views for real time metrics reporting for streaming RPCs. This views will produce
 * data only for streaming gRPC calls.
 *
 * @since 0.18
 */
public static void registerRealTimeMetricsViews() {
  registerRealTimeMetricsViews(Stats.getViewManager());
}
 
Example #25
Source File: RpcViews.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Registers all views.
 *
 * <p>This is equivalent with calling {@link #registerAllCumulativeViews()} and {@link
 * #registerAllIntervalViews()}.
 *
 * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
 *
 * @since 0.11.0
 * @deprecated in favor of {@link #registerAllGrpcViews()}.
 */
@Deprecated
public static void registerAllViews() {
  registerAllViews(Stats.getViewManager());
}
 
Example #26
Source File: RpcViews.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Registers all standard interval views.
 *
 * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
 *
 * @since 0.11.0
 * @deprecated because interval window is deprecated. There won't be interval views in the future.
 */
@Deprecated
public static void registerAllIntervalViews() {
  registerAllIntervalViews(Stats.getViewManager());
}
 
Example #27
Source File: HttpViews.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Register all default client views.
 *
 * <p>It is recommended to call this method before doing any HTTP call to avoid missing stats.
 *
 * @since 0.13
 */
public static final void registerAllClientViews() {
  registerAllClientViews(Stats.getViewManager());
}
 
Example #28
Source File: RpcViews.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Registers basic server gRPC views.
 *
 * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
 *
 * @since 0.19
 */
public static void registerServerGrpcBasicViews() {
  registerServerGrpcBasicViews(Stats.getViewManager());
}
 
Example #29
Source File: RpcViews.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Registers basic client gRPC views.
 *
 * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
 *
 * @since 0.19
 */
public static void registerClientGrpcBasicViews() {
  registerClientGrpcBasicViews(Stats.getViewManager());
}
 
Example #30
Source File: RpcViews.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Registers all basic gRPC views.
 *
 * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
 *
 * <p>This is equivalent with calling {@link #registerClientGrpcBasicViews()} and {@link
 * #registerServerGrpcBasicViews()}.
 *
 * @since 0.19
 */
public static void registerAllGrpcBasicViews() {
  registerAllGrpcBasicViews(Stats.getViewManager());
}