io.opencensus.contrib.grpc.metrics.RpcMeasureConstants Java Examples

The following examples show how to use io.opencensus.contrib.grpc.metrics.RpcMeasureConstants. 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: AbstractInteropTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private static void checkEndTags(
    MetricsRecord record, String methodName, Status.Code status, boolean clientSide) {
  assertNotNull("record is not null", record);

  TagKey methodNameTagKey = clientSide
      ? RpcMeasureConstants.GRPC_CLIENT_METHOD
      : RpcMeasureConstants.GRPC_SERVER_METHOD;
  TagValue methodNameTag = record.tags.get(methodNameTagKey);
  assertNotNull("method name tagged", methodNameTag);
  assertEquals("method names match", methodName, methodNameTag.asString());

  TagKey statusTagKey = clientSide
      ? RpcMeasureConstants.GRPC_CLIENT_STATUS
      : RpcMeasureConstants.GRPC_SERVER_STATUS;
  TagValue statusTag = record.tags.get(statusTagKey);
  assertNotNull("status tagged", statusTag);
  assertEquals(status.toString(), statusTag.asString());
}
 
Example #2
Source File: AbstractInteropTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private static void checkStartTags(MetricsRecord record, String methodName, boolean clientSide) {
  assertNotNull("record is not null", record);

  TagKey methodNameTagKey = clientSide
      ? RpcMeasureConstants.GRPC_CLIENT_METHOD
      : RpcMeasureConstants.GRPC_SERVER_METHOD;
  TagValue methodNameTag = record.tags.get(methodNameTagKey);
  assertNotNull("method name tagged", methodNameTag);
  assertEquals("method names match", methodName, methodNameTag.asString());
}
 
Example #3
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("NonAtomicVolatileUpdate")
public void outboundWireSize(long bytes) {
  if (outboundWireSizeUpdater != null) {
    outboundWireSizeUpdater.getAndAdd(this, bytes);
  } else {
    outboundWireSize += bytes;
  }
  module.recordRealTimeMetric(
      startCtx, RpcMeasureConstants.GRPC_CLIENT_SENT_BYTES_PER_METHOD, bytes);
}
 
Example #4
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("NonAtomicVolatileUpdate")
public void inboundWireSize(long bytes) {
  if (inboundWireSizeUpdater != null) {
    inboundWireSizeUpdater.getAndAdd(this, bytes);
  } else {
    inboundWireSize += bytes;
  }
  module.recordRealTimeMetric(
      startCtx, RpcMeasureConstants.GRPC_CLIENT_RECEIVED_BYTES_PER_METHOD, bytes);
}
 
Example #5
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("NonAtomicVolatileUpdate")
public void inboundMessage(int seqNo) {
  if (inboundMessageCountUpdater != null) {
    inboundMessageCountUpdater.getAndIncrement(this);
  } else {
    inboundMessageCount++;
  }
  module.recordRealTimeMetric(
      startCtx, RpcMeasureConstants.GRPC_CLIENT_RECEIVED_MESSAGES_PER_METHOD, 1);
}
 
Example #6
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("NonAtomicVolatileUpdate")
public void outboundMessage(int seqNo) {
  if (outboundMessageCountUpdater != null) {
    outboundMessageCountUpdater.getAndIncrement(this);
  } else {
    outboundMessageCount++;
  }
  module.recordRealTimeMetric(
      startCtx, RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1);
}
 
Example #7
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
ClientCallTracer(CensusStatsModule module, TagContext parentCtx, String fullMethodName) {
  this.module = checkNotNull(module);
  this.parentCtx = checkNotNull(parentCtx);
  TagValue methodTag = TagValue.create(fullMethodName);
  this.startCtx = module.tagger.toBuilder(parentCtx)
      .putLocal(RpcMeasureConstants.GRPC_CLIENT_METHOD, methodTag)
      .build();
  this.stopwatch = module.stopwatchSupplier.get().start();
  if (module.recordStartedRpcs) {
    module.statsRecorder.newMeasureMap()
        .put(DeprecatedCensusConstants.RPC_CLIENT_STARTED_COUNT, 1)
        .record(startCtx);
  }
}
 
Example #8
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("NonAtomicVolatileUpdate")
public void outboundWireSize(long bytes) {
  if (outboundWireSizeUpdater != null) {
    outboundWireSizeUpdater.getAndAdd(this, bytes);
  } else {
    outboundWireSize += bytes;
  }
  module.recordRealTimeMetric(
      parentCtx, RpcMeasureConstants.GRPC_SERVER_SENT_BYTES_PER_METHOD, bytes);
}
 
Example #9
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("NonAtomicVolatileUpdate")
public void inboundWireSize(long bytes) {
  if (inboundWireSizeUpdater != null) {
    inboundWireSizeUpdater.getAndAdd(this, bytes);
  } else {
    inboundWireSize += bytes;
  }
  module.recordRealTimeMetric(
      parentCtx, RpcMeasureConstants.GRPC_SERVER_RECEIVED_BYTES_PER_METHOD, bytes);
}
 
Example #10
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("NonAtomicVolatileUpdate")
public void inboundMessage(int seqNo) {
  if (inboundMessageCountUpdater != null) {
    inboundMessageCountUpdater.getAndIncrement(this);
  } else {
    inboundMessageCount++;
  }
  module.recordRealTimeMetric(
      parentCtx, RpcMeasureConstants.GRPC_SERVER_RECEIVED_MESSAGES_PER_METHOD, 1);
}
 
Example #11
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("NonAtomicVolatileUpdate")
public void outboundMessage(int seqNo) {
  if (outboundMessageCountUpdater != null) {
    outboundMessageCountUpdater.getAndIncrement(this);
  } else {
    outboundMessageCount++;
  }
  module.recordRealTimeMetric(
      parentCtx, RpcMeasureConstants.GRPC_SERVER_SENT_MESSAGES_PER_METHOD, 1);
}
 
Example #12
Source File: CensusStatsModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata headers) {
  TagContext parentCtx = headers.get(statsHeader);
  if (parentCtx == null) {
    parentCtx = tagger.empty();
  }
  TagValue methodTag = TagValue.create(fullMethodName);
  parentCtx =
      tagger
          .toBuilder(parentCtx)
          .putLocal(RpcMeasureConstants.GRPC_SERVER_METHOD, methodTag)
          .build();
  return new ServerTracer(CensusStatsModule.this, parentCtx);
}
 
Example #13
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 #14
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 #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: CensusModulesTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void clientStreamNeverCreatedStillRecordStats() {
  CensusStatsModule.ClientCallTracer callTracer =
      censusStats.newClientCallTracer(tagger.empty(), method.getFullMethodName());

  fakeClock.forwardTime(3000, MILLISECONDS);
  callTracer.callEnded(Status.DEADLINE_EXCEEDED.withDescription("3 seconds"));

  // Upstart record
  StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
  assertNotNull(record);
  assertNoServerContent(record);
  assertEquals(1, record.tags.size());
  TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
  assertEquals(method.getFullMethodName(), methodTag.asString());
  assertEquals(
      1,
      record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_STARTED_COUNT));

  // Completion record
  record = statsRecorder.pollRecord();
  assertNotNull(record);
  assertNoServerContent(record);
  methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
  assertEquals(method.getFullMethodName(), methodTag.asString());
  TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
  assertEquals(Status.Code.DEADLINE_EXCEEDED.toString(), statusTag.asString());
  assertEquals(
      1,
      record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT));
  assertEquals(
      1,
      record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT));
  assertEquals(
      0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT));
  assertEquals(
      0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES));
  assertEquals(
      0,
      record.getMetricAsLongOrFail(
          DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
  assertEquals(
      0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_COUNT));
  assertEquals(
      0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_BYTES));
  assertEquals(0,
      record.getMetricAsLongOrFail(
          DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
  assertEquals(
      3000,
      record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
  assertNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_SERVER_ELAPSED_TIME));
}