io.opencensus.stats.Aggregation Java Examples

The following examples show how to use io.opencensus.stats.Aggregation. 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: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCumulativeViewDataWithEmptyBucketBoundaries() {
  Aggregation noHistogram =
      Distribution.create(BucketBoundaries.create(Collections.<Double>emptyList()));
  View view = createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, noHistogram, Arrays.asList(KEY));
  clock.setTime(Timestamp.create(1, 0));
  viewManager.registerView(view);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.1)
      .record(tagger.emptyBuilder().put(KEY, VALUE).build());
  clock.setTime(Timestamp.create(3, 0));
  ViewData viewData = viewManager.getView(VIEW_NAME);
  assertThat(viewData.getWindowData())
      .isEqualTo(CumulativeData.create(Timestamp.create(1, 0), Timestamp.create(3, 0)));
  StatsTestUtil.assertAggregationMapEquals(
      viewData.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(noHistogram, MEASURE_DOUBLE, 1.1)),
      EPSILON);
}
 
Example #3
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private void testRecordCumulative(Measure measure, Aggregation aggregation, double... values) {
  View view = createCumulativeView(VIEW_NAME, measure, aggregation, Arrays.asList(KEY));
  clock.setTime(Timestamp.create(1, 2));
  viewManager.registerView(view);
  TagContext tags = tagger.emptyBuilder().put(KEY, VALUE).build();
  for (double val : values) {
    putToMeasureMap(statsRecorder.newMeasureMap(), measure, val).record(tags);
  }
  clock.setTime(Timestamp.create(3, 4));
  ViewData viewData = viewManager.getView(VIEW_NAME);
  assertThat(viewData.getView()).isEqualTo(view);
  assertThat(viewData.getWindowData())
      .isEqualTo(CumulativeData.create(Timestamp.create(1, 2), Timestamp.create(3, 4)));
  StatsTestUtil.assertAggregationMapEquals(
      viewData.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(aggregation, measure, values)),
      EPSILON);
}
 
Example #4
Source File: MetricUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
static MetricDescriptor viewToMetricDescriptor(View view) {
  if (view.getWindow() instanceof View.AggregationWindow.Interval) {
    // Only creates Metric for cumulative stats.
    return null;
  }
  List<LabelKey> labelKeys = new ArrayList<LabelKey>();
  for (TagKey tagKey : view.getColumns()) {
    // TODO: add description
    labelKeys.add(LabelKey.create(tagKey.getName(), ""));
  }
  Measure measure = view.getMeasure();
  Aggregation aggregation = view.getAggregation();
  return MetricDescriptor.create(
      view.getName().asString(),
      view.getDescription(),
      getUnit(measure, aggregation),
      getType(measure, aggregation),
      labelKeys);
}
 
Example #5
Source File: MetricUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static Type getType(Measure measure, Aggregation aggregation) {
  return aggregation.match(
      Functions.returnConstant(
          measure.match(
              TYPE_CUMULATIVE_DOUBLE_FUNCTION, // Sum Double
              TYPE_CUMULATIVE_INT64_FUNCTION, // Sum Int64
              TYPE_UNRECOGNIZED_FUNCTION)),
      TYPE_CUMULATIVE_INT64_FUNCTION, // Count
      TYPE_CUMULATIVE_DISTRIBUTION_FUNCTION, // Distribution
      Functions.returnConstant(
          measure.match(
              TYPE_GAUGE_DOUBLE_FUNCTION, // LastValue Double
              TYPE_GAUGE_INT64_FUNCTION, // LastValue Long
              TYPE_UNRECOGNIZED_FUNCTION)),
      AGGREGATION_TYPE_DEFAULT_FUNCTION);
}
 
Example #6
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 #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: StatsTestUtil.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link AggregationData} by adding the given sequence of values, based on the
 * definition of the given {@link Aggregation}.
 *
 * @param aggregation the {@code Aggregation} to apply the values to.
 * @param values the values to add to the {@code MutableAggregation}s.
 * @return an {@code AggregationData}.
 */
static AggregationData createAggregationData(
    Aggregation aggregation, Measure measure, double... values) {
  MutableAggregation mutableAggregation =
      RecordUtils.createMutableAggregation(aggregation, measure);
  for (double value : values) {
    mutableAggregation.add(value, Collections.<String, AttachmentValue>emptyMap(), EMPTY);
  }
  return mutableAggregation.toAggregationData();
}
 
Example #9
Source File: MutableViewData.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static <T> Map<T, MutableAggregation> aggregateOnEachTagValueList(
    Multimap<T, MutableAggregation> multimap, Aggregation aggregation, Measure measure) {
  Map<T, MutableAggregation> map = Maps.newHashMap();
  for (T tagValues : multimap.keySet()) {
    // Initially empty MutableAggregations.
    MutableAggregation combinedAggregation = createMutableAggregation(aggregation, measure);
    for (MutableAggregation mutableAggregation : multimap.get(tagValues)) {
      combinedAggregation.combine(mutableAggregation, 1.0);
    }
    map.put(tagValues, combinedAggregation);
  }
  return map;
}
 
Example #10
Source File: MutableViewData.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static <T> void putFractionalMutableAggregationsToMultiMap(
    Map<T, MutableAggregation> mutableAggrMap,
    Multimap<T, MutableAggregation> multimap,
    Aggregation aggregation,
    Measure measure,
    double fraction) {
  for (Entry<T, MutableAggregation> entry : mutableAggrMap.entrySet()) {
    // Initially empty MutableAggregations.
    MutableAggregation fractionalMutableAgg = createMutableAggregation(aggregation, measure);
    fractionalMutableAgg.combine(entry.getValue(), fraction);
    multimap.put(entry.getKey(), fractionalMutableAgg);
  }
}
 
Example #11
Source File: MutableViewData.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void putBucketsIntoMultiMap(
    ArrayDeque<IntervalBucket> buckets,
    Multimap<List</*@Nullable*/ TagValue>, MutableAggregation> multimap,
    Aggregation aggregation,
    Measure measure,
    Timestamp now) {
  // Put fractional stats of the head (oldest) bucket.
  IntervalBucket head = CheckerFrameworkUtils.castNonNull(buckets.peekFirst());
  IntervalBucket tail = CheckerFrameworkUtils.castNonNull(buckets.peekLast());
  double fractionTail = tail.getFraction(now);
  // TODO(songya): decide what to do when time goes backwards
  checkArgument(
      0.0 <= fractionTail && fractionTail <= 1.0,
      "Fraction " + fractionTail + " should be within [0.0, 1.0].");
  double fractionHead = 1.0 - fractionTail;
  putFractionalMutableAggregationsToMultiMap(
      head.getTagValueAggregationMap(), multimap, aggregation, measure, fractionHead);

  // Put whole data of other buckets.
  boolean shouldSkipFirst = true;
  for (IntervalBucket bucket : buckets) {
    if (shouldSkipFirst) {
      shouldSkipFirst = false;
      continue; // skip the first bucket
    }
    for (Entry<List</*@Nullable*/ TagValue>, MutableAggregation> entry :
        bucket.getTagValueAggregationMap().entrySet()) {
      multimap.put(entry.getKey(), entry.getValue());
    }
  }
}
 
Example #12
Source File: MutableViewData.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private Map<List</*@Nullable*/ TagValue>, AggregationData> combineBucketsAndGetAggregationMap(
    Timestamp now) {
  // Need to maintain the order of inserted MutableAggregations (inserted based on time order).
  Multimap<List</*@Nullable*/ TagValue>, MutableAggregation> multimap =
      LinkedHashMultimap.create();

  ArrayDeque<IntervalBucket> shallowCopy = new ArrayDeque<IntervalBucket>(buckets);

  Aggregation aggregation = super.view.getAggregation();
  Measure measure = super.view.getMeasure();
  putBucketsIntoMultiMap(shallowCopy, multimap, aggregation, measure, now);
  Map<List</*@Nullable*/ TagValue>, MutableAggregation> singleMap =
      aggregateOnEachTagValueList(multimap, aggregation, measure);
  return createAggregationMap(singleMap, super.getView().getMeasure());
}
 
Example #13
Source File: IntervalBucket.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
IntervalBucket(Timestamp start, Duration duration, Aggregation aggregation, Measure measure) {
  this.start = checkNotNull(start, "Start");
  this.duration = checkNotNull(duration, "Duration");
  checkArgument(duration.compareTo(ZERO) > 0, "Duration must be positive");
  this.aggregation = checkNotNull(aggregation, "Aggregation");
  this.measure = checkNotNull(measure, "measure");
}
 
Example #14
Source File: MetricUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public Type apply(Aggregation arg) {
  if (arg instanceof Aggregation.Mean) {
    return Type.CUMULATIVE_DOUBLE; // Mean
  }
  throw new AssertionError();
}
 
Example #15
Source File: RecordUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public MutableAggregation apply(Aggregation arg) {
  if (arg instanceof Aggregation.Mean) {
    return MutableMean.create();
  }
  throw new IllegalArgumentException("Unknown Aggregation.");
}
 
Example #16
Source File: StatsBenchmarksUtil.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static View[] createViews(
    int size, Measure[] measures, Aggregation aggregation, TagKey... keys) {
  View[] views = new View[size];
  for (int i = 0; i < size; i++) {
    views[i] = createView(measures[i].getName(), measures[i], aggregation, keys);
  }
  return views;
}
 
Example #17
Source File: GoogleDtpInternalMetricRecorder.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private void setupView(Measure measure, TagKey... keys) {
  // Register the view. It is imperative that this step exists,
  // otherwise recorded metrics will be dropped and never exported.
  View view = View.create(
      View.Name.create(NAME_BASE + measure.getName()),
      measure.getDescription(),
      measure,
      Aggregation.Count.create(),
      ImmutableList.copyOf(keys));

  viewManager.registerView(view);
}
 
Example #18
Source File: MetricUtils.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private static String getUnit(Measure measure, Aggregation aggregation) {
  if (aggregation instanceof Count) {
    return COUNT_UNIT;
  }
  return measure.getUnit();
}
 
Example #19
Source File: RecordMultipleViewsBenchmark.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Setup
public void setup() throws Exception {
  ViewManager manager = StatsBenchmarksUtil.getViewManager(implementation);
  recorder = StatsBenchmarksUtil.getStatsRecorder(implementation);
  tagger = TagsBenchmarksUtil.getTagger(implementation);
  tagContext = createContext(numViews);

  for (int i = 0; i < numViews; i++) {
    // count
    manager.registerView(
        StatsBenchmarksUtil.createView(
            "DC" + i,
            StatsBenchmarksUtil.DOUBLE_COUNT_MEASURES[0],
            Aggregation.Count.create(),
            TagsBenchmarksUtil.TAG_KEYS.get(i)));
    manager.registerView(
        StatsBenchmarksUtil.createView(
            "LC" + i,
            StatsBenchmarksUtil.LONG_COUNT_MEASURES[0],
            Aggregation.Count.create(),
            TagsBenchmarksUtil.TAG_KEYS.get(i)));
    // sum
    manager.registerView(
        StatsBenchmarksUtil.createView(
            "DS" + i,
            StatsBenchmarksUtil.DOUBLE_SUM_MEASURES[0],
            Aggregation.Sum.create(),
            TagsBenchmarksUtil.TAG_KEYS.get(i)));
    manager.registerView(
        StatsBenchmarksUtil.createView(
            "LS" + i,
            StatsBenchmarksUtil.LONG_SUM_MEASURES[0],
            Aggregation.Sum.create(),
            TagsBenchmarksUtil.TAG_KEYS.get(i)));
    // distribution
    manager.registerView(
        StatsBenchmarksUtil.createView(
            "DD" + i,
            StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_MEASURES[0],
            StatsBenchmarksUtil.DISTRIBUTION,
            TagsBenchmarksUtil.TAG_KEYS.get(i)));
    manager.registerView(
        StatsBenchmarksUtil.createView(
            "LD" + i,
            StatsBenchmarksUtil.LONG_DISTRIBUTION_MEASURES[0],
            StatsBenchmarksUtil.DISTRIBUTION,
            TagsBenchmarksUtil.TAG_KEYS.get(i)));
    // last value
    manager.registerView(
        StatsBenchmarksUtil.createView(
            "DL" + i,
            StatsBenchmarksUtil.DOUBLE_LASTVALUE_MEASURES[0],
            Aggregation.LastValue.create(),
            TagsBenchmarksUtil.TAG_KEYS.get(i)));
    manager.registerView(
        StatsBenchmarksUtil.createView(
            "LL" + i,
            StatsBenchmarksUtil.LONG_LASTVALUE_MEASURES[0],
            Aggregation.LastValue.create(),
            TagsBenchmarksUtil.TAG_KEYS.get(i)));
  }
}
 
Example #20
Source File: StatsBenchmarksUtil.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
static View createView(String name, Measure measure, Aggregation aggregation, TagKey... keys) {
  return View.create(View.Name.create(name), "", measure, aggregation, Arrays.asList(keys));
}
 
Example #21
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 #22
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 #23
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 #24
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private final void testRecordInterval(
    Measure measure,
    Aggregation aggregation,
    double[] initialValues, /* There are 5 initial values recorded before we call getView(). */
    double value6,
    double value7,
    AggregationData expectedValues1,
    AggregationData expectedValues2,
    AggregationData expectedValues3) {
  // The interval is 10 seconds, i.e. values should expire after 10 seconds.
  // Each bucket has a duration of 2.5 seconds.
  View view =
      View.create(
          VIEW_NAME,
          VIEW_DESCRIPTION,
          measure,
          aggregation,
          Arrays.asList(KEY),
          Interval.create(TEN_SECONDS));
  long startTimeMillis = 30 * MILLIS_PER_SECOND; // start at 30s
  clock.setTime(Timestamp.fromMillis(startTimeMillis));
  viewManager.registerView(view);

  TagContext tags = tagger.emptyBuilder().put(KEY, VALUE).build();

  for (int i = 1; i <= 5; i++) {
    /*
     * Add each value in sequence, at 31s, 32s, 33s, etc.
     * 1st and 2nd values should fall into the first bucket [30.0, 32.5),
     * 3rd and 4th values should fall into the second bucket [32.5, 35.0),
     * 5th value should fall into the third bucket [35.0, 37.5).
     */
    clock.setTime(Timestamp.fromMillis(startTimeMillis + i * MILLIS_PER_SECOND));
    putToMeasureMap(statsRecorder.newMeasureMap(), measure, initialValues[i - 1]).record(tags);
  }

  clock.setTime(Timestamp.fromMillis(startTimeMillis + 8 * MILLIS_PER_SECOND));
  // 38s, no values should have expired
  StatsTestUtil.assertAggregationMapEquals(
      viewManager.getView(VIEW_NAME).getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(aggregation, measure, initialValues)),
      EPSILON);

  clock.setTime(Timestamp.fromMillis(startTimeMillis + 11 * MILLIS_PER_SECOND));
  // 41s, 40% of the values in the first bucket should have expired (1 / 2.5 = 0.4).
  StatsTestUtil.assertAggregationMapEquals(
      viewManager.getView(VIEW_NAME).getAggregationMap(),
      ImmutableMap.of(Arrays.asList(VALUE), expectedValues1),
      EPSILON);

  clock.setTime(Timestamp.fromMillis(startTimeMillis + 12 * MILLIS_PER_SECOND));
  // 42s, add a new value value1, should fall into bucket [40.0, 42.5)
  putToMeasureMap(statsRecorder.newMeasureMap(), measure, value6).record(tags);

  clock.setTime(Timestamp.fromMillis(startTimeMillis + 17 * MILLIS_PER_SECOND));
  // 47s, values in the first and second bucket should have expired, and 80% of values in the
  // third bucket should have expired. The new value should persist.
  StatsTestUtil.assertAggregationMapEquals(
      viewManager.getView(VIEW_NAME).getAggregationMap(),
      ImmutableMap.of(Arrays.asList(VALUE), expectedValues2),
      EPSILON);

  clock.setTime(Timestamp.fromMillis(60 * MILLIS_PER_SECOND));
  // 60s, all previous values should have expired, add another value value2
  putToMeasureMap(statsRecorder.newMeasureMap(), measure, value7).record(tags);
  StatsTestUtil.assertAggregationMapEquals(
      viewManager.getView(VIEW_NAME).getAggregationMap(),
      ImmutableMap.of(Arrays.asList(VALUE), expectedValues3),
      EPSILON);

  clock.setTime(Timestamp.fromMillis(100 * MILLIS_PER_SECOND));
  // 100s, all values should have expired
  assertThat(viewManager.getView(VIEW_NAME).getAggregationMap()).isEmpty();
}
 
Example #25
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);
  }
}