use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.
the class TaggerImplTest method toBuilder_SkipNullTag.
@Test
public void toBuilder_SkipNullTag() {
TagContext tagContextWithNullTag = new SimpleTagContext(TAG1, null, TAG2);
TagContext newTagContext = tagger.toBuilder(tagContextWithNullTag).build();
assertThat(tagContextToList(newTagContext)).containsExactly(TAG1, TAG2);
}
use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.
the class TaggerImplTest method getCurrentTagContext_RemoveDuplicatesFromUnknownTagContext.
@Test
public void getCurrentTagContext_RemoveDuplicatesFromUnknownTagContext() {
Tag tag1 = Tag.create(K1, V1);
Tag tag2 = Tag.create(K1, V2);
TagContext tagContextWithDuplicateTags = new SimpleTagContext(tag1, tag2);
TagContext result = getResultOfGetCurrentTagContext(tagContextWithDuplicateTags);
assertThat(tagContextToList(result)).containsExactly(tag2);
}
use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.
the class TaggerImplTest method getCurrentTagContext_TaggingReenabled.
@Test
public void getCurrentTagContext_TaggingReenabled() {
TagContext tags = new SimpleTagContext(TAG1);
tagsComponent.setState(TaggingState.DISABLED);
assertThat(tagContextToList(getResultOfGetCurrentTagContext(tags))).isEmpty();
tagsComponent.setState(TaggingState.ENABLED);
assertThat(tagContextToList(getResultOfGetCurrentTagContext(tags))).containsExactly(TAG1);
}
use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.
the class HttpServerHandler method recordStats.
private void recordStats(HttpRequestContext context, Q request, int httpCode) {
double requestLatency = NANOSECONDS.toMillis(System.nanoTime() - context.requestStartTime);
String methodStr = extractor.getMethod(request);
String routeStr = extractor.getRoute(request);
TagContext startCtx = tagger.toBuilder(context.tagContext).put(HTTP_SERVER_METHOD, TagValue.create(methodStr == null ? "" : methodStr), METADATA_NO_PROPAGATION).put(HTTP_SERVER_ROUTE, TagValue.create(routeStr == null ? "" : routeStr), METADATA_NO_PROPAGATION).put(HTTP_SERVER_STATUS, TagValue.create(httpCode == 0 ? "error" : Integer.toString(httpCode)), METADATA_NO_PROPAGATION).build();
statsRecorder.newMeasureMap().put(HTTP_SERVER_LATENCY, requestLatency).put(HTTP_SERVER_RECEIVED_BYTES, context.receiveMessageSize.get()).put(HTTP_SERVER_SENT_BYTES, context.sentMessageSize.get()).record(startCtx);
}
use of io.opencensus.tags.TagContext in project grpc-java by grpc.
the class CensusModulesTest method testClientInterceptors.
// Test that Census ClientInterceptors uses the TagContext and Span out of the current Context
// to create the ClientCallTracer, and that it intercepts ClientCall.Listener.onClose() to call
// ClientCallTracer.callEnded().
private void testClientInterceptors(boolean nonDefaultContext) {
grpcServerRule.getServiceRegistry().addService(ServerServiceDefinition.builder("package1.service2").addMethod(method, new ServerCallHandler<String, String>() {
@Override
public ServerCall.Listener<String> startCall(ServerCall<String, String> call, Metadata headers) {
call.sendHeaders(new Metadata());
call.sendMessage("Hello");
call.close(Status.PERMISSION_DENIED.withDescription("No you don't"), new Metadata());
return mockServerCallListener;
}
}).build());
final AtomicReference<CallOptions> capturedCallOptions = new AtomicReference<>();
ClientInterceptor callOptionsCaptureInterceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
capturedCallOptions.set(callOptions);
return next.newCall(method, callOptions);
}
};
Channel interceptedChannel = ClientInterceptors.intercept(grpcServerRule.getChannel(), callOptionsCaptureInterceptor, censusStats.getClientInterceptor(), censusTracing.getClientInterceptor());
ClientCall<String, String> call;
if (nonDefaultContext) {
Context ctx = io.opencensus.tags.unsafe.ContextUtils.withValue(Context.ROOT, tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build());
ctx = ContextUtils.withValue(ctx, fakeClientParentSpan);
Context origCtx = ctx.attach();
try {
call = interceptedChannel.newCall(method, CALL_OPTIONS);
} finally {
ctx.detach(origCtx);
}
} else {
assertEquals(io.opencensus.tags.unsafe.ContextUtils.getValue(Context.ROOT), io.opencensus.tags.unsafe.ContextUtils.getValue(Context.current()));
assertEquals(ContextUtils.getValue(Context.current()), BlankSpan.INSTANCE);
call = interceptedChannel.newCall(method, CALL_OPTIONS);
}
// The interceptor adds tracer factory to CallOptions
assertEquals("customvalue", capturedCallOptions.get().getOption(CUSTOM_OPTION));
assertEquals(2, capturedCallOptions.get().getStreamTracerFactories().size());
assertTrue(capturedCallOptions.get().getStreamTracerFactories().get(0) instanceof CallAttemptsTracerFactory);
assertTrue(capturedCallOptions.get().getStreamTracerFactories().get(1) instanceof CensusStatsModule.CallAttemptsTracerFactory);
// Make the call
Metadata headers = new Metadata();
call.start(mockClientCallListener, headers);
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
if (nonDefaultContext) {
TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
assertEquals("extra value", extraTag.asString());
assertEquals(2, record.tags.size());
} else {
assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
assertEquals(1, record.tags.size());
}
if (nonDefaultContext) {
verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
verify(spyClientSpanBuilder).setRecordEvents(eq(true));
} else {
verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), ArgumentMatchers.<Span>isNotNull());
verify(spyClientSpanBuilder).setRecordEvents(eq(true));
}
verify(spyClientSpan, never()).end(any(EndSpanOptions.class));
// End the call
call.halfClose();
call.request(1);
verify(mockClientCallListener).onClose(statusCaptor.capture(), any(Metadata.class));
Status status = statusCaptor.getValue();
assertEquals(Status.Code.PERMISSION_DENIED, status.getCode());
assertEquals("No you don't", status.getDescription());
// The intercepting listener calls callEnded() on ClientCallTracer, which records to Census.
record = statsRecorder.pollRecord();
assertNotNull(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.PERMISSION_DENIED.toString(), statusTag.asString());
if (nonDefaultContext) {
TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
assertEquals("extra value", extraTag.asString());
} else {
assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
}
verify(spyClientSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.PERMISSION_DENIED.withDescription("No you don't")).setSampleToLocalSpanStore(false).build());
verify(spyClientSpan, never()).end();
assertZeroRetryRecorded();
}
Aggregations