Java Code Examples for io.opencensus.stats.MeasureMap#record()

The following examples show how to use io.opencensus.stats.MeasureMap#record() . 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 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 2
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void record_MapDeprecatedRpcConstants() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(RecordUtils.RPC_METHOD));

  viewManager.registerView(view);
  MeasureMap statsRecord = statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0);
  statsRecord.record(new SimpleTagContext(Tag.create(RecordUtils.GRPC_CLIENT_METHOD, VALUE)));
  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)),
      1e-6);
}
 
Example 3
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 4
Source File: RecordBatchedBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Record batched double count measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedDoubleCount(Data data) {
  MeasureMap map = data.recorder.newMeasureMap();
  for (int i = 0; i < data.numValues; i++) {
    map.put(StatsBenchmarksUtil.DOUBLE_COUNT_MEASURES[i], (double) i);
  }
  map.record(data.tags);
  return map;
}
 
Example 5
Source File: RecordBatchedBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Record batched long count measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedLongCount(Data data) {
  MeasureMap map = data.recorder.newMeasureMap();
  for (int i = 0; i < data.numValues; i++) {
    map.put(StatsBenchmarksUtil.LONG_COUNT_MEASURES[i], i);
  }
  map.record(data.tags);
  return map;
}
 
Example 6
Source File: RecordBatchedBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Record batched double sum measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedDoubleSum(Data data) {
  MeasureMap map = data.recorder.newMeasureMap();
  for (int i = 0; i < data.numValues; i++) {
    map.put(StatsBenchmarksUtil.DOUBLE_SUM_MEASURES[i], (double) i);
  }
  map.record(data.tags);
  return map;
}
 
Example 7
Source File: RecordBatchedBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Record batched long sum measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedLongSum(Data data) {
  MeasureMap map = data.recorder.newMeasureMap();
  for (int i = 0; i < data.numValues; i++) {
    map.put(StatsBenchmarksUtil.LONG_SUM_MEASURES[i], i);
  }
  map.record(data.tags);
  return map;
}
 
Example 8
Source File: RecordBatchedBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Record batched double distribution measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedDoubleDistribution(Data data) {
  MeasureMap map = data.recorder.newMeasureMap();
  for (int i = 0; i < data.numValues; i++) {
    map.put(StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_MEASURES[i], (double) i);
  }
  map.record(data.tags);
  return map;
}
 
Example 9
Source File: RecordBatchedBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Record batched ling distribution measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedLongDistribution(Data data) {
  MeasureMap map = data.recorder.newMeasureMap();
  for (int i = 0; i < data.numValues; i++) {
    map.put(StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_MEASURES[i], i);
  }
  map.record(data.tags);
  return map;
}
 
Example 10
Source File: RecordBatchedBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Record batched double last value measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedDoubleLastValue(Data data) {
  MeasureMap map = data.recorder.newMeasureMap();
  for (int i = 0; i < data.numValues; i++) {
    map.put(StatsBenchmarksUtil.DOUBLE_LASTVALUE_MEASURES[i], (double) i);
  }
  map.record(data.tags);
  return map;
}
 
Example 11
Source File: RecordBatchedBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Record batched long last value measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedLongLastValue(Data data) {
  MeasureMap map = data.recorder.newMeasureMap();
  for (int i = 0; i < data.numValues; i++) {
    map.put(StatsBenchmarksUtil.LONG_LASTVALUE_MEASURES[i], i);
  }
  map.record(data.tags);
  return map;
}
 
Example 12
Source File: RecordDifferentTagValuesBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static MeasureMap record(Data data, Measure.MeasureLong measure, int value) {
  MeasureMap map = data.recorder.newMeasureMap();
  map.put(measure, value);
  for (TagContext tags : data.contexts) {
    map.record(tags);
  }
  return map;
}
 
Example 13
Source File: RecordDifferentTagValuesBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static MeasureMap record(Data data, Measure.MeasureDouble measure, double value) {
  MeasureMap map = data.recorder.newMeasureMap();
  map.put(measure, value);
  for (TagContext tags : data.contexts) {
    map.record(tags);
  }
  return map;
}
 
Example 14
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
private void recordRealTimeMetric(TagContext ctx, MeasureLong measure, long value) {
  if (recordRealTimeMetrics) {
    MeasureMap measureMap = statsRecorder.newMeasureMap().put(measure, value);
    measureMap.record(ctx);
  }
}
 
Example 15
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Record a finished stream and mark the current time as the end time.
 *
 * <p>Can be called from any thread without synchronization.  Calling it the second time or more
 * is a no-op.
 */
@Override
public void streamClosed(Status status) {
  if (streamClosedUpdater != null) {
    if (streamClosedUpdater.getAndSet(this, 1) != 0) {
      return;
    }
  } else {
    if (streamClosed != 0) {
      return;
    }
    streamClosed = 1;
  }
  if (!module.recordFinishedRpcs) {
    return;
  }
  stopwatch.stop();
  long elapsedTimeNanos = stopwatch.elapsed(TimeUnit.NANOSECONDS);
  MeasureMap measureMap = module.statsRecorder.newMeasureMap()
      // TODO(songya): remove the deprecated measure constants once they are completed removed.
      .put(DeprecatedCensusConstants.RPC_SERVER_FINISHED_COUNT, 1)
      // The latency is double value
      .put(
          DeprecatedCensusConstants.RPC_SERVER_SERVER_LATENCY,
          elapsedTimeNanos / NANOS_PER_MILLI)
      .put(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_COUNT, outboundMessageCount)
      .put(DeprecatedCensusConstants.RPC_SERVER_REQUEST_COUNT, inboundMessageCount)
      .put(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_BYTES, outboundWireSize)
      .put(DeprecatedCensusConstants.RPC_SERVER_REQUEST_BYTES, inboundWireSize)
      .put(
          DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES,
          outboundUncompressedSize)
      .put(
          DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES,
          inboundUncompressedSize);
  if (!status.isOk()) {
    measureMap.put(DeprecatedCensusConstants.RPC_SERVER_ERROR_COUNT, 1);
  }
  TagValue statusTag = TagValue.create(status.getCode().toString());
  measureMap.record(
      module
          .tagger
          .toBuilder(parentCtx)
          .putLocal(RpcMeasureConstants.GRPC_SERVER_STATUS, statusTag)
          .build());
}
 
Example 16
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Record a finished call and mark the current time as the end time.
 *
 * <p>Can be called from any thread without synchronization.  Calling it the second time or more
 * is a no-op.
 */
void callEnded(Status status) {
  if (callEndedUpdater != null) {
    if (callEndedUpdater.getAndSet(this, 1) != 0) {
      return;
    }
  } else {
    if (callEnded != 0) {
      return;
    }
    callEnded = 1;
  }
  if (!module.recordFinishedRpcs) {
    return;
  }
  stopwatch.stop();
  long roundtripNanos = stopwatch.elapsed(TimeUnit.NANOSECONDS);
  ClientTracer tracer = streamTracer;
  if (tracer == null) {
    tracer = new ClientTracer(module, startCtx);
  }
  MeasureMap measureMap = module.statsRecorder.newMeasureMap()
      // TODO(songya): remove the deprecated measure constants once they are completed removed.
      .put(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT, 1)
      // The latency is double value
      .put(
          DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY,
          roundtripNanos / NANOS_PER_MILLI)
      .put(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT, tracer.outboundMessageCount)
      .put(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_COUNT, tracer.inboundMessageCount)
      .put(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES, tracer.outboundWireSize)
      .put(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_BYTES, tracer.inboundWireSize)
      .put(
          DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES,
          tracer.outboundUncompressedSize)
      .put(
          DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES,
          tracer.inboundUncompressedSize);
  if (!status.isOk()) {
    measureMap.put(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT, 1);
  }
  TagValue statusTag = TagValue.create(status.getCode().toString());
  measureMap.record(
      module
          .tagger
          .toBuilder(startCtx)
          .putLocal(RpcMeasureConstants.GRPC_CLIENT_STATUS, statusTag)
          .build());
}
 
Example 17
Source File: CensusStatsModule.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Record a finished call and mark the current time as the end time.
 *
 * <p>Can be called from any thread without synchronization.  Calling it the second time or more
 * is a no-op.
 */
void callEnded(Status status) {
  if (callEndedUpdater != null) {
    if (callEndedUpdater.getAndSet(this, 1) != 0) {
      return;
    }
  } else {
    if (callEnded != 0) {
      return;
    }
    callEnded = 1;
  }
  if (!recordFinishedRpcs) {
    return;
  }
  stopwatch.stop();
  long roundtripNanos = stopwatch.elapsed(TimeUnit.NANOSECONDS);
  ClientTracer tracer = streamTracer;
  if (tracer == null) {
    tracer = BLANK_CLIENT_TRACER;
  }
  MeasureMap measureMap = module.statsRecorder.newMeasureMap()
      // TODO(songya): remove the deprecated measure constants once they are completed removed.
      .put(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT, 1)
      // The latency is double value
      .put(
          DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY,
          roundtripNanos / NANOS_PER_MILLI)
      .put(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT, tracer.outboundMessageCount)
      .put(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_COUNT, tracer.inboundMessageCount)
      .put(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES, tracer.outboundWireSize)
      .put(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_BYTES, tracer.inboundWireSize)
      .put(
          DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES,
          tracer.outboundUncompressedSize)
      .put(
          DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES,
          tracer.inboundUncompressedSize);
  if (!status.isOk()) {
    measureMap.put(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT, 1);
  }
  TagValue statusTag = TagValue.create(status.getCode().toString());
  measureMap.record(
      module
          .tagger
          .toBuilder(startCtx)
          .put(DeprecatedCensusConstants.RPC_STATUS, statusTag)
          .build());
}
 
Example 18
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
private void recordRealTimeMetric(TagContext ctx, MeasureDouble measure, double value) {
  if (recordRealTimeMetrics) {
    MeasureMap measureMap = statsRecorder.newMeasureMap().put(measure, value);
    measureMap.record(ctx);
  }
}
 
Example 19
Source File: ZPagesTester.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private static void recordExampleData() throws InterruptedException {
  Tracing.getExportComponent()
      .getSampledSpanStore()
      .registerSpanNamesForCollection(Collections.singletonList(SPAN_NAME));
  RpcViews.registerAllViews(); // Use old RPC constants to get interval stats.
  SpanBuilder spanBuilder =
      tracer.spanBuilder(SPAN_NAME).setRecordEvents(true).setSampler(Samplers.alwaysSample());

  try (Scope scope = spanBuilder.startScopedSpan()) {
    tracer.getCurrentSpan().addAnnotation("Starts recording.");
    MeasureMap measureMap =
        statsRecorder
            .newMeasureMap()
            // Client measurements.
            .put(RpcMeasureConstants.RPC_CLIENT_STARTED_COUNT, 1)
            .put(RpcMeasureConstants.RPC_CLIENT_FINISHED_COUNT, 1)
            .put(RpcMeasureConstants.RPC_CLIENT_ROUNDTRIP_LATENCY, 1.0)
            .put(RpcMeasureConstants.RPC_CLIENT_REQUEST_COUNT, 1)
            .put(RpcMeasureConstants.RPC_CLIENT_RESPONSE_COUNT, 1)
            .put(RpcMeasureConstants.RPC_CLIENT_REQUEST_BYTES, 1e5)
            .put(RpcMeasureConstants.RPC_CLIENT_RESPONSE_BYTES, 1e5)
            .put(RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES, 1e5)
            .put(RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES, 1e5)
            // Server measurements.
            .put(RpcMeasureConstants.RPC_SERVER_STARTED_COUNT, 1)
            .put(RpcMeasureConstants.RPC_SERVER_FINISHED_COUNT, 1)
            .put(RpcMeasureConstants.RPC_SERVER_SERVER_LATENCY, 1.0)
            .put(RpcMeasureConstants.RPC_SERVER_REQUEST_COUNT, 1)
            .put(RpcMeasureConstants.RPC_SERVER_RESPONSE_COUNT, 1)
            .put(RpcMeasureConstants.RPC_SERVER_REQUEST_BYTES, 1e5)
            .put(RpcMeasureConstants.RPC_SERVER_RESPONSE_BYTES, 1e5)
            .put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, 1e5)
            .put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, 1e5);
    measureMap.record(
        tagger
            .currentBuilder()
            .put(RpcMeasureConstants.RPC_STATUS, TagValue.create("OK"))
            .put(RpcMeasureConstants.RPC_METHOD, METHOD)
            .build());
    MeasureMap measureMapErrors =
        statsRecorder
            .newMeasureMap()
            .put(RpcMeasureConstants.RPC_CLIENT_ERROR_COUNT, 1)
            .put(RpcMeasureConstants.RPC_SERVER_ERROR_COUNT, 1);
    measureMapErrors.record(
        tagger
            .currentBuilder()
            .put(RpcMeasureConstants.RPC_STATUS, TagValue.create("UNKNOWN"))
            .put(RpcMeasureConstants.RPC_METHOD, METHOD)
            .build());

    Thread.sleep(200); // sleep for fake work.
    tracer.getCurrentSpan().addAnnotation("Finish recording.");
  }
}
 
Example 20
Source File: CensusStatsModule.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Record a finished stream and mark the current time as the end time.
 *
 * <p>Can be called from any thread without synchronization.  Calling it the second time or more
 * is a no-op.
 */
@Override
public void streamClosed(Status status) {
  if (streamClosedUpdater != null) {
    if (streamClosedUpdater.getAndSet(this, 1) != 0) {
      return;
    }
  } else {
    if (streamClosed != 0) {
      return;
    }
    streamClosed = 1;
  }
  if (!recordFinishedRpcs) {
    return;
  }
  stopwatch.stop();
  long elapsedTimeNanos = stopwatch.elapsed(TimeUnit.NANOSECONDS);
  MeasureMap measureMap = module.statsRecorder.newMeasureMap()
      // TODO(songya): remove the deprecated measure constants once they are completed removed.
      .put(DeprecatedCensusConstants.RPC_SERVER_FINISHED_COUNT, 1)
      // The latency is double value
      .put(
          DeprecatedCensusConstants.RPC_SERVER_SERVER_LATENCY,
          elapsedTimeNanos / NANOS_PER_MILLI)
      .put(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_COUNT, outboundMessageCount)
      .put(DeprecatedCensusConstants.RPC_SERVER_REQUEST_COUNT, inboundMessageCount)
      .put(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_BYTES, outboundWireSize)
      .put(DeprecatedCensusConstants.RPC_SERVER_REQUEST_BYTES, inboundWireSize)
      .put(
          DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES,
          outboundUncompressedSize)
      .put(
          DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES,
          inboundUncompressedSize);
  if (!status.isOk()) {
    measureMap.put(DeprecatedCensusConstants.RPC_SERVER_ERROR_COUNT, 1);
  }
  TagValue statusTag = TagValue.create(status.getCode().toString());
  measureMap.record(
      module
          .tagger
          .toBuilder(parentCtx)
          .put(DeprecatedCensusConstants.RPC_STATUS, statusTag)
          .build());
}