Search in sources :

Example 6 with TagKey

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);
    }
}
Also used : TagMapImpl(io.opencensus.implcore.tags.TagMapImpl) TagContextDeserializationException(io.opencensus.tags.propagation.TagContextDeserializationException) TagValueWithMetadata(io.opencensus.implcore.tags.TagValueWithMetadata) HashMap(java.util.HashMap) TagKey(io.opencensus.tags.TagKey) Nullable(javax.annotation.Nullable)

Example 7 with TagKey

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);
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) FakeTagContext(io.grpc.internal.testing.StatsTestUtils.FakeTagContext) SpanContext(io.opencensus.trace.SpanContext) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue) Span(io.opencensus.trace.Span) Map(java.util.Map) FakeTagContext(io.grpc.internal.testing.StatsTestUtils.FakeTagContext) Test(org.junit.Test)

Example 8 with TagKey

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());
}
Also used : TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue)

Example 9 with TagKey

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());
}
Also used : TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue)

Example 10 with TagKey

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);
    }
}
Also used : Aggregation(io.opencensus.stats.Aggregation) TagKey(io.opencensus.tags.TagKey) ArrayList(java.util.ArrayList) ViewManager(io.opencensus.stats.ViewManager) View(io.opencensus.stats.View)

Aggregations

TagKey (io.opencensus.tags.TagKey)14 TagValue (io.opencensus.tags.TagValue)7 TagValueWithMetadata (io.opencensus.implcore.tags.TagValueWithMetadata)4 Aggregation (io.opencensus.stats.Aggregation)4 View (io.opencensus.stats.View)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 TagMapImpl (io.opencensus.implcore.tags.TagMapImpl)2 TagContextDeserializationException (io.opencensus.tags.propagation.TagContextDeserializationException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Context (io.grpc.Context)1 FakeTagContext (io.grpc.internal.testing.StatsTestUtils.FakeTagContext)1 StatsTestUtil.createEmptyViewData (io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData)1 LabelKey (io.opencensus.metrics.LabelKey)1 Count (io.opencensus.stats.Aggregation.Count)1 Distribution (io.opencensus.stats.Aggregation.Distribution)1 LastValue (io.opencensus.stats.Aggregation.LastValue)1