io.opencensus.stats.View Java Examples

The following examples show how to use io.opencensus.stats.View. 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: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void record_WithAttachments_Distribution() {
  testClock.setTime(START_TIME);
  View view =
      View.create(VIEW_NAME, "description", MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY));
  viewManager.registerView(view);
  recordWithAttachments();
  ViewData viewData = viewManager.getView(VIEW_NAME);
  assertThat(viewData).isNotNull();
  DistributionData distributionData =
      (DistributionData) viewData.getAggregationMap().get(Collections.singletonList(VALUE));
  List<Exemplar> expected =
      Arrays.asList(
          Exemplar.create(
              1.0, Timestamp.create(2, 0), Collections.singletonMap("k2", ATTACHMENT_VALUE_2)),
          Exemplar.create(
              12.0, Timestamp.create(3, 0), Collections.singletonMap("k1", ATTACHMENT_VALUE_3)));
  assertThat(distributionData.getExemplars()).containsExactlyElementsIn(expected).inOrder();
}
 
Example #2
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCumulativeViewDataWithoutBucketBoundaries() {
  View view = createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, MEAN, 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(MEAN, 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
@Test
@SuppressWarnings("deprecation")
public void registerRecordAndGetView_StatsReenabled() {
  statsComponent.setState(StatsCollectionState.DISABLED);
  statsComponent.setState(StatsCollectionState.ENABLED);
  View view = createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, MEAN, Arrays.asList(KEY));
  viewManager.registerView(view);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.1)
      .record(tagger.emptyBuilder().put(KEY, VALUE).build());
  StatsTestUtil.assertAggregationMapEquals(
      viewManager.getView(VIEW_NAME).getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE), StatsTestUtil.createAggregationData(MEAN, MEASURE_DOUBLE, 1.1)),
      EPSILON);
}
 
Example #4
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void registerViewWithStatsDisabled_RecordAndGetViewWithStatsEnabled() {
  statsComponent.setState(StatsCollectionState.DISABLED);
  View view = createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, MEAN, Arrays.asList(KEY));
  viewManager.registerView(view); // view will still be registered.

  statsComponent.setState(StatsCollectionState.ENABLED);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.1)
      .record(tagger.emptyBuilder().put(KEY, VALUE).build());
  StatsTestUtil.assertAggregationMapEquals(
      viewManager.getView(VIEW_NAME).getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE), StatsTestUtil.createAggregationData(MEAN, MEASURE_DOUBLE, 1.1)),
      EPSILON);
}
 
Example #5
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void registerDifferentViewWithSameNameWithStatsDisabled() {
  statsComponent.setState(StatsCollectionState.DISABLED);
  View view1 =
      View.create(
          VIEW_NAME,
          "View description.",
          MEASURE_DOUBLE,
          DISTRIBUTION,
          Arrays.asList(KEY),
          CUMULATIVE);
  View view2 =
      View.create(
          VIEW_NAME,
          "This is a different description.",
          MEASURE_DOUBLE,
          DISTRIBUTION,
          Arrays.asList(KEY),
          CUMULATIVE);
  testFailedToRegisterView(
      view1, view2, "A different view with the same name is already registered");
}
 
Example #6
Source File: RpczZPageHandler.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
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</*@Nullable*/ TagValue>, AggregationData> entry :
        viewData.getAggregationMap().entrySet()) {
      TagValue tagValue;
      List</*@Nullable*/ TagValue> 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());
    }
  }
}
 
Example #7
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 #8
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 #9
Source File: MeasureToViewMap.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/** Enable stats collection for the given {@link View}. */
synchronized void registerView(View view, Clock clock) {
  exportedViews = null;
  View existing = registeredViews.get(view.getName());
  if (existing != null) {
    if (existing.equals(view)) {
      // Ignore views that are already registered.
      return;
    } else {
      throw new IllegalArgumentException(
          "A different view with the same name is already registered: " + existing);
    }
  }
  Measure measure = view.getMeasure();
  Measure registeredMeasure = registeredMeasures.get(measure.getName());
  if (registeredMeasure != null && !registeredMeasure.equals(measure)) {
    throw new IllegalArgumentException(
        "A different measure with the same name is already registered: " + registeredMeasure);
  }
  registeredViews.put(view.getName(), view);
  if (registeredMeasure == null) {
    registeredMeasures.put(measure.getName(), measure);
  }
  Timestamp now = clock.now();
  mutableMap.put(view.getMeasure().getName(), MutableViewData.create(view, now));
}
 
Example #10
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 #11
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 #12
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void record_StatsDisabled() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(KEY),
          Cumulative.create());

  viewManager.registerView(view);
  statsComponent.setState(StatsCollectionState.DISABLED);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.0)
      .record(new SimpleTagContext(Tag.create(KEY, VALUE)));
  assertThat(viewManager.getView(VIEW_NAME)).isEqualTo(createEmptyViewData(view));
}
 
Example #13
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void recordTwice() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(KEY),
          Cumulative.create());
  viewManager.registerView(view);
  MeasureMap statsRecord = statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0);
  statsRecord.record(new SimpleTagContext(Tag.create(KEY, VALUE)));
  statsRecord.record(new SimpleTagContext(Tag.create(KEY, VALUE_2)));
  ViewData viewData = viewManager.getView(VIEW_NAME);

  // There should be two entries.
  StatsTestUtil.assertAggregationMapEquals(
      viewData.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0),
          Arrays.asList(VALUE_2),
          StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0)),
      1e-6);
}
 
Example #14
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void record_CurrentContextNotSet() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(KEY),
          Cumulative.create());
  viewManager.registerView(view);
  statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0).record();
  ViewData viewData = viewManager.getView(VIEW_NAME);

  // record() should have used the default TagContext, so the tag value should be null.
  assertThat(viewData.getAggregationMap().keySet())
      .containsExactly(Arrays.asList((TagValue) null));
}
 
Example #15
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #16
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void record_WithAttachments_DistributionNoHistogram() {
  testClock.setTime(START_TIME);
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          DISTRIBUTION_NO_HISTOGRAM,
          Arrays.asList(KEY));
  viewManager.registerView(view);
  recordWithAttachments();
  ViewData viewData = viewManager.getView(VIEW_NAME);
  assertThat(viewData).isNotNull();
  DistributionData distributionData =
      (DistributionData) viewData.getAggregationMap().get(Collections.singletonList(VALUE));
  // Recording exemplar has no effect if there's no histogram.
  assertThat(distributionData.getExemplars()).isEmpty();
}
 
Example #17
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void preventRegisteringDifferentViewWithSameName() {
  View view1 =
      View.create(
          VIEW_NAME,
          "View description.",
          MEASURE_DOUBLE,
          DISTRIBUTION,
          Arrays.asList(KEY),
          CUMULATIVE);
  View view2 =
      View.create(
          VIEW_NAME,
          "This is a different description.",
          MEASURE_DOUBLE,
          DISTRIBUTION,
          Arrays.asList(KEY),
          CUMULATIVE);
  testFailedToRegisterView(
      view1, view2, "A different view with the same name is already registered");
}
 
Example #18
Source File: MeasureToViewMap.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
private synchronized MutableViewData getMutableViewData(View.Name viewName) {
  View view = registeredViews.get(viewName);
  if (view == null) {
    return null;
  }
  Collection<MutableViewData> views = mutableMap.get(view.getMeasure().getName());
  for (MutableViewData viewData : views) {
    if (viewData.getView().getName().equals(viewName)) {
      return viewData;
    }
  }
  throw new AssertionError(
      "Internal error: Not recording stats for view: \""
          + viewName
          + "\" registeredViews="
          + registeredViews
          + ", mutableMap="
          + mutableMap);
}
 
Example #19
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void preventRegisteringDifferentMeasureWithSameName() {
  MeasureDouble measure1 = MeasureDouble.create("measure", "description", "1");
  MeasureLong measure2 = MeasureLong.create("measure", "description", "1");
  View view1 =
      View.create(
          VIEW_NAME, VIEW_DESCRIPTION, measure1, DISTRIBUTION, Arrays.asList(KEY), CUMULATIVE);
  View view2 =
      View.create(
          VIEW_NAME_2, VIEW_DESCRIPTION, measure2, DISTRIBUTION, Arrays.asList(KEY), CUMULATIVE);
  testFailedToRegisterView(
      view1, view2, "A different measure with the same name is already registered");
}
 
Example #20
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void registerRecordAndGetView_StatsDisabled() {
  statsComponent.setState(StatsCollectionState.DISABLED);
  View view = createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, MEAN, Arrays.asList(KEY));
  viewManager.registerView(view);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.1)
      .record(tagger.emptyBuilder().put(KEY, VALUE).build());
  assertThat(viewManager.getView(VIEW_NAME)).isEqualTo(createEmptyViewData(view));
}
 
Example #21
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private void testFailedToRegisterView(View view1, View view2, String message) {
  viewManager.registerView(view1);
  try {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage(message);
    viewManager.registerView(view2);
  } finally {
    assertThat(viewManager.getView(VIEW_NAME).getView()).isEqualTo(view1);
  }
}
 
Example #22
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleViewSameMeasure() {
  final View view1 =
      createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY));
  final View view2 =
      createCumulativeView(VIEW_NAME_2, MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY));
  clock.setTime(Timestamp.create(1, 1));
  viewManager.registerView(view1);
  clock.setTime(Timestamp.create(2, 2));
  viewManager.registerView(view2);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 5.0)
      .record(tagger.emptyBuilder().put(KEY, VALUE).build());
  clock.setTime(Timestamp.create(3, 3));
  ViewData viewData1 = viewManager.getView(VIEW_NAME);
  clock.setTime(Timestamp.create(4, 4));
  ViewData viewData2 = viewManager.getView(VIEW_NAME_2);
  assertThat(viewData1.getWindowData())
      .isEqualTo(CumulativeData.create(Timestamp.create(1, 1), Timestamp.create(3, 3)));
  StatsTestUtil.assertAggregationMapEquals(
      viewData1.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 5.0)),
      EPSILON);
  assertThat(viewData2.getWindowData())
      .isEqualTo(CumulativeData.create(Timestamp.create(2, 2), Timestamp.create(4, 4)));
  StatsTestUtil.assertAggregationMapEquals(
      viewData2.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 5.0)),
      EPSILON);
}
 
Example #23
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 #24
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void settingStateToDisabledWillClearStats_Interval() {
  View intervalView =
      View.create(
          VIEW_NAME_2,
          VIEW_DESCRIPTION,
          MEASURE_DOUBLE,
          MEAN,
          Arrays.asList(KEY),
          Interval.create(Duration.create(60, 0)));
  settingStateToDisabledWillClearStats(intervalView);
}
 
Example #25
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void record_WithAttachments_Count() {
  testClock.setTime(START_TIME);
  View view =
      View.create(VIEW_NAME, "description", MEASURE_DOUBLE, Count.create(), Arrays.asList(KEY));
  viewManager.registerView(view);
  recordWithAttachments();
  ViewData viewData = viewManager.getView(VIEW_NAME);
  assertThat(viewData).isNotNull();
  CountData countData =
      (CountData) viewData.getAggregationMap().get(Collections.singletonList(VALUE));
  // Recording exemplar does not affect views with an aggregation other than distribution.
  assertThat(countData.getCount()).isEqualTo(2L);
}
 
Example #26
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void settingStateToDisabledWillClearStats(View view) {
  Timestamp timestamp1 = Timestamp.create(1, 0);
  clock.setTime(timestamp1);
  viewManager.registerView(view);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.1)
      .record(tagger.emptyBuilder().put(KEY, VALUE).build());
  StatsTestUtil.assertAggregationMapEquals(
      viewManager.getView(view.getName()).getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(view.getAggregation(), view.getMeasure(), 1.1)),
      EPSILON);

  Timestamp timestamp2 = Timestamp.create(2, 0);
  clock.setTime(timestamp2);
  statsComponent.setState(StatsCollectionState.DISABLED); // This will clear stats.
  assertThat(viewManager.getView(view.getName())).isEqualTo(createEmptyViewData(view));

  Timestamp timestamp3 = Timestamp.create(3, 0);
  clock.setTime(timestamp3);
  statsComponent.setState(StatsCollectionState.ENABLED);

  Timestamp timestamp4 = Timestamp.create(4, 0);
  clock.setTime(timestamp4);
  // This ViewData does not have any stats, but it should not be an empty ViewData, since it has
  // non-zero TimeStamps.
  ViewData viewData = viewManager.getView(view.getName());
  assertThat(viewData.getAggregationMap()).isEmpty();
  AggregationWindowData windowData = viewData.getWindowData();
  if (windowData instanceof CumulativeData) {
    assertThat(windowData).isEqualTo(CumulativeData.create(timestamp3, timestamp4));
  } else {
    assertThat(windowData).isEqualTo(IntervalData.create(timestamp4));
  }
}
 
Example #27
Source File: HttpViewsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void registerServerViews() {
  HttpViews.registerAllServerViews(viewManager);
  for (View view : HttpViews.HTTP_SERVER_VIEWS_SET) {
    verify(viewManager).registerView(view);
  }
}
 
Example #28
Source File: ViewManagerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private void testMultipleViews_DifferentMeasures(
    Measure measure1, Measure measure2, double value1, double value2) {
  final View view1 = createCumulativeView(VIEW_NAME, measure1, DISTRIBUTION, Arrays.asList(KEY));
  final View view2 =
      createCumulativeView(VIEW_NAME_2, measure2, DISTRIBUTION, Arrays.asList(KEY));
  clock.setTime(Timestamp.create(1, 0));
  viewManager.registerView(view1);
  clock.setTime(Timestamp.create(2, 0));
  viewManager.registerView(view2);
  TagContext tags = tagger.emptyBuilder().put(KEY, VALUE).build();
  MeasureMap measureMap = statsRecorder.newMeasureMap();
  putToMeasureMap(measureMap, measure1, value1);
  putToMeasureMap(measureMap, measure2, value2);
  measureMap.record(tags);
  clock.setTime(Timestamp.create(3, 0));
  ViewData viewData1 = viewManager.getView(VIEW_NAME);
  clock.setTime(Timestamp.create(4, 0));
  ViewData viewData2 = viewManager.getView(VIEW_NAME_2);
  assertThat(viewData1.getWindowData())
      .isEqualTo(CumulativeData.create(Timestamp.create(1, 0), Timestamp.create(3, 0)));
  StatsTestUtil.assertAggregationMapEquals(
      viewData1.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(DISTRIBUTION, measure1, value1)),
      EPSILON);
  assertThat(viewData2.getWindowData())
      .isEqualTo(CumulativeData.create(Timestamp.create(2, 0), Timestamp.create(4, 0)));
  StatsTestUtil.assertAggregationMapEquals(
      viewData2.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(DISTRIBUTION, measure2, value2)),
      EPSILON);
}
 
Example #29
Source File: StatszZPageHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void groupViewsByDirectoriesAndGetMeasures(
    Set<View> views, TreeNode root, Map<String, Measure> measures, Set<View> cachedViews) {
  for (View view : views) {
    if (cachedViews.contains(view)) {
      continue;
    }
    cachedViews.add(view);

    List<String> dirs = PATH_SPLITTER.splitToList(view.getName().asString());
    TreeNode node = root;
    for (int i = 0; i < dirs.size(); i++) {
      if (node == null) {
        break; // Should never happen. Work around the nullness checker.
      }
      String dir = dirs.get(i);
      if ("".equals(dir) && i == 0) {
        continue; // In case view name starts with a '/'.
      }
      node.views++;
      if (i != dirs.size() - 1) { // Non-leaf node (directory node)
        node.children.putIfAbsent(dir, new TreeNode());
        node = node.children.get(dir);
      } else { // Leaf node (view node)
        node.children.putIfAbsent(dir, new TreeNode(view.getName()));
      }
    }

    Measure measure = view.getMeasure();
    measures.putIfAbsent(measure.getName(), measure);
  }
}
 
Example #30
Source File: OpenCensusMetrics.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
public static void registerAllViews(ViewManager viewManager) {
  View[] views_to_register =
      new View[] {
        Views.REQUEST_COUNT, Views.REQUEST_LATENCY, Views.NETWORK_LATENCY, Views.RETRY_COUNT
      };
  for (View view : views_to_register) {
    viewManager.registerView(view);
  }
}