use of com.google.instrumentation.stats.StatsContext in project grpc-java by grpc.
the class StatsTraceContext method newServerContext.
/**
* Creates a {@code StatsTraceContext} for an incoming RPC, using the StatsContext deserialized
* from the headers.
*
* <p>The current time is used as the start time of the RPC.
*/
public static StatsTraceContext newServerContext(String methodName, StatsContextFactory statsFactory, Metadata headers, Supplier<Stopwatch> stopwatchSupplier) {
Metadata.Key<StatsContext> statsHeader = createStatsHeader(statsFactory);
StatsContext parentCtx = headers.get(statsHeader);
if (parentCtx == null) {
parentCtx = statsFactory.getDefault();
}
return new StatsTraceContext(Side.SERVER, methodName, parentCtx, stopwatchSupplier, statsHeader);
}
use of com.google.instrumentation.stats.StatsContext in project grpc-java by grpc.
the class StatsTraceContextTest method tagPropagation.
/**
* Tags that are propagated by the {@link StatsContextFactory} are properly propagated via
* the headers.
*/
@Test
public void tagPropagation() {
String methodName = MethodDescriptor.generateFullMethodName("Service1", "method3");
// EXTRA_TAG is propagated by the FakeStatsContextFactory. Note that not all tags are
// propagated. The StatsContextFactory decides which tags are to propagated. gRPC facilitates
// the propagation by putting them in the headers.
StatsContext parentCtx = statsCtxFactory.getDefault().with(StatsTestUtils.EXTRA_TAG, TagValue.create("extra-tag-value-897"));
StatsTraceContext clientCtx = StatsTraceContext.newClientContextForTesting(methodName, statsCtxFactory, parentCtx, fakeClock.getStopwatchSupplier());
Metadata headers = new Metadata();
clientCtx.propagateToHeaders(headers);
// The server gets the propagated tag from the headers, and puts it on the server-side
// StatsContext.
StatsTraceContext serverCtx = StatsTraceContext.newServerContext(methodName, statsCtxFactory, headers, fakeClock.getStopwatchSupplier());
serverCtx.callEnded(Status.OK);
clientCtx.callEnded(Status.OK);
StatsTestUtils.MetricsRecord serverRecord = statsCtxFactory.pollRecord();
assertNotNull(serverRecord);
assertNoClientContent(serverRecord);
TagValue serverMethodTag = serverRecord.tags.get(RpcConstants.RPC_SERVER_METHOD);
assertEquals(methodName, serverMethodTag.toString());
TagValue serverStatusTag = serverRecord.tags.get(RpcConstants.RPC_STATUS);
assertEquals(Status.Code.OK.toString(), serverStatusTag.toString());
assertNull(serverRecord.getMetric(RpcConstants.RPC_SERVER_ERROR_COUNT));
TagValue serverPropagatedTag = serverRecord.tags.get(StatsTestUtils.EXTRA_TAG);
assertEquals("extra-tag-value-897", serverPropagatedTag.toString());
StatsTestUtils.MetricsRecord clientRecord = statsCtxFactory.pollRecord();
assertNotNull(clientRecord);
assertNoServerContent(clientRecord);
TagValue clientMethodTag = clientRecord.tags.get(RpcConstants.RPC_CLIENT_METHOD);
assertEquals(methodName, clientMethodTag.toString());
TagValue clientStatusTag = clientRecord.tags.get(RpcConstants.RPC_STATUS);
assertEquals(Status.Code.OK.toString(), clientStatusTag.toString());
assertNull(clientRecord.getMetric(RpcConstants.RPC_CLIENT_ERROR_COUNT));
TagValue clientPropagatedTag = clientRecord.tags.get(StatsTestUtils.EXTRA_TAG);
assertEquals("extra-tag-value-897", clientPropagatedTag.toString());
}
use of com.google.instrumentation.stats.StatsContext in project grpc-java by grpc.
the class ServerImplTest method basicExchangeSuccessful.
@Test
public void basicExchangeSuccessful() throws Exception {
createAndStartServer(NO_FILTERS);
final Metadata.Key<String> metadataKey = Metadata.Key.of("inception", Metadata.ASCII_STRING_MARSHALLER);
final Metadata.Key<StatsContext> statsHeaderKey = StatsTraceContext.createStatsHeader(statsCtxFactory);
final AtomicReference<ServerCall<String, Integer>> callReference = new AtomicReference<ServerCall<String, Integer>>();
MethodDescriptor<String, Integer> method = MethodDescriptor.<String, Integer>newBuilder().setType(MethodDescriptor.MethodType.UNKNOWN).setFullMethodName("Waiter/serve").setRequestMarshaller(STRING_MARSHALLER).setResponseMarshaller(INTEGER_MARSHALLER).build();
mutableFallbackRegistry.addService(ServerServiceDefinition.builder(new ServiceDescriptor("Waiter", method)).addMethod(method, new ServerCallHandler<String, Integer>() {
@Override
public ServerCall.Listener<String> startCall(ServerCall<String, Integer> call, Metadata headers) {
assertEquals("Waiter/serve", call.getMethodDescriptor().getFullMethodName());
assertNotNull(call);
assertNotNull(headers);
assertEquals("value", headers.get(metadataKey));
callReference.set(call);
return callListener;
}
}).build());
ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
Metadata requestHeaders = new Metadata();
requestHeaders.put(metadataKey, "value");
StatsContext statsContextOnClient = statsCtxFactory.getDefault().with(StatsTestUtils.EXTRA_TAG, TagValue.create("extraTagValue"));
requestHeaders.put(statsHeaderKey, statsContextOnClient);
StatsTraceContext statsTraceCtx = transportListener.methodDetermined("Waiter/serve", requestHeaders);
assertNotNull(statsTraceCtx);
when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
transportListener.streamCreated(stream, "Waiter/serve", requestHeaders);
verify(stream).setListener(streamListenerCaptor.capture());
ServerStreamListener streamListener = streamListenerCaptor.getValue();
assertNotNull(streamListener);
verify(stream, atLeast(1)).statsTraceContext();
assertEquals(1, executor.runDueTasks());
ServerCall<String, Integer> call = callReference.get();
assertNotNull(call);
verify(stream).getAuthority();
String order = "Lots of pizza, please";
streamListener.messageRead(STRING_MARSHALLER.stream(order));
assertEquals(1, executor.runDueTasks());
verify(callListener).onMessage(order);
Metadata responseHeaders = new Metadata();
responseHeaders.put(metadataKey, "response value");
call.sendHeaders(responseHeaders);
verify(stream).writeHeaders(responseHeaders);
verify(stream).setCompressor(isA(Compressor.class));
call.sendMessage(314);
ArgumentCaptor<InputStream> inputCaptor = ArgumentCaptor.forClass(InputStream.class);
verify(stream).writeMessage(inputCaptor.capture());
verify(stream).flush();
assertEquals(314, INTEGER_MARSHALLER.parse(inputCaptor.getValue()).intValue());
// All full; no dessert.
streamListener.halfClosed();
assertEquals(1, executor.runDueTasks());
verify(callListener).onHalfClose();
call.sendMessage(50);
verify(stream, times(2)).writeMessage(inputCaptor.capture());
verify(stream, times(2)).flush();
assertEquals(50, INTEGER_MARSHALLER.parse(inputCaptor.getValue()).intValue());
Metadata trailers = new Metadata();
trailers.put(metadataKey, "another value");
Status status = Status.OK.withDescription("A okay");
call.close(status, trailers);
verify(stream).close(status, trailers);
streamListener.closed(Status.OK);
assertEquals(1, executor.runDueTasks());
verify(callListener).onComplete();
verify(stream, atLeast(1)).statsTraceContext();
verifyNoMoreInteractions(stream);
verifyNoMoreInteractions(callListener);
// Check stats
StatsTestUtils.MetricsRecord record = statsCtxFactory.pollRecord();
assertNotNull(record);
TagValue methodTag = record.tags.get(RpcConstants.RPC_SERVER_METHOD);
assertNotNull(methodTag);
assertEquals("Waiter/serve", methodTag.toString());
TagValue statusTag = record.tags.get(RpcConstants.RPC_STATUS);
assertNotNull(statusTag);
assertEquals(Status.Code.OK.toString(), statusTag.toString());
TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
assertNotNull(extraTag);
assertEquals("extraTagValue", extraTag.toString());
assertNull(record.getMetric(RpcConstants.RPC_CLIENT_REQUEST_BYTES));
assertNull(record.getMetric(RpcConstants.RPC_CLIENT_RESPONSE_BYTES));
assertNull(record.getMetric(RpcConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
assertNull(record.getMetric(RpcConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
// The test doesn't invoke MessageFramer and MessageDeframer which keep the sizes.
// Thus the sizes reported to stats would be zero.
assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_REQUEST_BYTES));
assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_RESPONSE_BYTES));
assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
}
use of com.google.instrumentation.stats.StatsContext in project instrumentation-java by census-instrumentation.
the class StatsRunner method main.
/** Main method. */
public static void main(String[] args) {
System.out.println("Hello Stats World");
System.out.println("Default Tags: " + DEFAULT);
System.out.println("Current Tags: " + factory.getCurrentStatsContext());
StatsContext tags1 = DEFAULT.with(K1, V1, K2, V2);
try (NonThrowingCloseable scopedStatsCtx1 = factory.withStatsContext(tags1)) {
System.out.println(" Current Tags: " + factory.getCurrentStatsContext());
System.out.println(" Current == Default + tags1: " + factory.getCurrentStatsContext().equals(tags1));
StatsContext tags2 = tags1.with(K3, V3, K4, V4);
try (NonThrowingCloseable scopedStatsCtx2 = factory.withStatsContext(tags2)) {
System.out.println(" Current Tags: " + factory.getCurrentStatsContext());
System.out.println(" Current == Default + tags1 + tags2: " + factory.getCurrentStatsContext().equals(tags2));
factory.getCurrentStatsContext().record(MeasurementMap.of(M1, 0.2, M2, 0.4));
}
}
System.out.println("Current == Default: " + factory.getCurrentStatsContext().equals(DEFAULT));
}
Aggregations