use of io.opencensus.tags.TagKey in project instrumentation-java by census-instrumentation.
the class CorrelationContextFormat method extract.
@Override
public <C> /*>>> extends @NonNull Object*/
TagContext extract(C carrier, Getter<C> getter) throws TagContextDeserializationException {
checkNotNull(carrier, "carrier");
checkNotNull(getter, "getter");
if (State.DISABLED.equals(state.getInternal())) {
return TagMapImpl.EMPTY;
}
@Nullable String correlationContext = getter.get(carrier, CORRELATION_CONTEXT);
if (correlationContext == null) {
throw new TagContextDeserializationException(CORRELATION_CONTEXT + " not present.");
}
try {
if (correlationContext.isEmpty()) {
return TagMapImpl.EMPTY;
}
Map<TagKey, TagValueWithMetadata> tags = new HashMap<>();
List<String> stringTags = TAG_SPLITTER.splitToList(correlationContext);
for (String stringTag : stringTags) {
decodeTag(stringTag, tags);
}
return new TagMapImpl(tags);
} catch (IllegalArgumentException e) {
throw new TagContextDeserializationException("Invalid TagContext: " + correlationContext, e);
}
}
use of io.opencensus.tags.TagKey in project grpc-java by grpc.
the class AbstractInteropTest method censusContextsPropagated.
@Test(timeout = 10000)
public void censusContextsPropagated() {
Assume.assumeTrue("Skip the test because server is not in the same process.", server != null);
Assume.assumeTrue(customCensusModulePresent());
Span clientParentSpan = Tracing.getTracer().spanBuilder("Test.interopTest").startSpan();
// A valid ID is guaranteed to be unique, so we can verify it is actually propagated.
assertTrue(clientParentSpan.getContext().getTraceId().isValid());
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, clientParentSpan);
Context origCtx = ctx.attach();
try {
blockingStub.unaryCall(SimpleRequest.getDefaultInstance());
Context serverCtx = contextCapture.get();
assertNotNull(serverCtx);
FakeTagContext statsCtx = (FakeTagContext) io.opencensus.tags.unsafe.ContextUtils.getValue(serverCtx);
assertNotNull(statsCtx);
Map<TagKey, TagValue> tags = statsCtx.getTags();
boolean tagFound = false;
for (Map.Entry<TagKey, TagValue> tag : tags.entrySet()) {
if (tag.getKey().equals(StatsTestUtils.EXTRA_TAG)) {
assertEquals(TagValue.create("extra value"), tag.getValue());
tagFound = true;
}
}
assertTrue("tag not found", tagFound);
Span span = ContextUtils.getValue(serverCtx);
assertNotNull(span);
SpanContext spanContext = span.getContext();
assertEquals(clientParentSpan.getContext().getTraceId(), spanContext.getTraceId());
} finally {
ctx.detach(origCtx);
}
}
use of io.opencensus.tags.TagKey in project grpc-java by grpc.
the class AbstractInteropTest method checkStartTags.
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());
}
use of io.opencensus.tags.TagKey in project grpc-java by grpc.
the class AbstractInteropTest method checkEndTags.
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());
}
use of io.opencensus.tags.TagKey in project instrumentation-java by census-instrumentation.
the class Repl method registerAllViews.
private static void registerAllViews() {
// Defining the distribution aggregations
Aggregation latencyDistribution = Distribution.create(BucketBoundaries.create(Arrays.asList(// >=1s, >=2s, >=4s, >=6s]
0.0, 25.0, 50.0, 75.0, 100.0, 200.0, 400.0, 600.0, 800.0, 1000.0, 2000.0, 4000.0, 6000.0)));
Aggregation lengthsDistribution = Distribution.create(BucketBoundaries.create(Arrays.asList(// >=800B, >=1000B]
0.0, 5.0, 10.0, 20.0, 40.0, 60.0, 80.0, 100.0, 200.0, 400.0, 600.0, 800.0, 1000.0)));
// Define the count aggregation
Aggregation countAggregation = Aggregation.Count.create();
// So tagKeys
List<TagKey> noKeys = new ArrayList<TagKey>();
// Define the views
View[] views = new View[] { View.create(Name.create("ocjavametrics/latency"), "The distribution of latencies", M_LATENCY_MS, latencyDistribution, Collections.singletonList(KEY_METHOD)), View.create(Name.create("ocjavametrics/lines_in"), "The number of lines read in from standard input", M_LINES_IN, countAggregation, noKeys), View.create(Name.create("ocjavametrics/errors"), "The number of errors encountered", M_ERRORS, countAggregation, Collections.singletonList(KEY_METHOD)), View.create(Name.create("ocjavametrics/line_lengths"), "The distribution of line lengths", M_LINE_LENGTHS, lengthsDistribution, noKeys) };
// Create the view manager
ViewManager vmgr = Stats.getViewManager();
// Then finally register the views
for (View view : views) {
vmgr.registerView(view);
}
}
Aggregations