Search in sources :

Example 16 with TagValue

use of io.opencensus.tags.TagValue in project grpc-java by grpc.

the class CensusModulesTest method subtestServerBasicStatsNoHeaders.

private void subtestServerBasicStatsNoHeaders(boolean recordStarts, boolean recordFinishes, boolean recordRealTime) {
    CensusStatsModule localCensusStats = new CensusStatsModule(tagger, tagCtxSerializer, statsRecorder, fakeClock.getStopwatchSupplier(), true, recordStarts, recordFinishes, recordRealTime, true);
    ServerStreamTracer.Factory tracerFactory = localCensusStats.getServerTracerFactory();
    ServerStreamTracer tracer = tracerFactory.newServerStreamTracer(method.getFullMethodName(), new Metadata());
    if (recordStarts) {
        StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
        assertNotNull(record);
        assertNoClientContent(record);
        assertEquals(1, record.tags.size());
        TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), methodTag.asString());
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_STARTED_COUNT));
    } else {
        assertNull(statsRecorder.pollRecord());
    }
    Context filteredContext = tracer.filterContext(Context.ROOT);
    TagContext statsCtx = io.opencensus.tags.unsafe.ContextUtils.getValue(filteredContext);
    assertEquals(tagger.emptyBuilder().putLocal(RpcMeasureConstants.GRPC_SERVER_METHOD, TagValue.create(method.getFullMethodName())).build(), statsCtx);
    tracer.inboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.inboundWireSize(34);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_BYTES_PER_METHOD, 34, recordRealTime, false);
    tracer.inboundUncompressedSize(67);
    fakeClock.forwardTime(100, MILLISECONDS);
    tracer.outboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.outboundWireSize(1028);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_BYTES_PER_METHOD, 1028, recordRealTime, false);
    tracer.outboundUncompressedSize(1128);
    fakeClock.forwardTime(16, MILLISECONDS);
    tracer.inboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.inboundWireSize(154);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_BYTES_PER_METHOD, 154, recordRealTime, false);
    tracer.inboundUncompressedSize(552);
    tracer.outboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.outboundWireSize(99);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_BYTES_PER_METHOD, 99, recordRealTime, false);
    tracer.outboundUncompressedSize(865);
    fakeClock.forwardTime(24, MILLISECONDS);
    tracer.streamClosed(Status.CANCELLED);
    if (recordFinishes) {
        StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
        assertNotNull(record);
        assertNoClientContent(record);
        TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), methodTag.asString());
        TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_STATUS);
        assertEquals(Status.Code.CANCELLED.toString(), statusTag.asString());
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_FINISHED_COUNT));
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_ERROR_COUNT));
        assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_COUNT));
        assertEquals(1028 + 99, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_BYTES));
        assertEquals(1128 + 865, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
        assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_REQUEST_COUNT));
        assertEquals(34 + 154, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_REQUEST_BYTES));
        assertEquals(67 + 552, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
        assertEquals(100 + 16 + 24, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_SERVER_LATENCY));
    } else {
        assertNull(statsRecorder.pollRecord());
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) ServerStreamTracer(io.grpc.ServerStreamTracer) TagContext(io.opencensus.tags.TagContext) Metadata(io.grpc.Metadata) TagValue(io.opencensus.tags.TagValue)

Example 17 with TagValue

use of io.opencensus.tags.TagValue in project instrumentation-java by census-instrumentation.

the class ViewDataTest method testViewDataEquals.

@Test
public void testViewDataEquals() {
    View cumulativeView = View.create(NAME, DESCRIPTION, MEASURE_DOUBLE, DISTRIBUTION, TAG_KEYS, CUMULATIVE);
    View intervalView = View.create(NAME, DESCRIPTION, MEASURE_DOUBLE, DISTRIBUTION, TAG_KEYS, INTERVAL_HOUR);
    new EqualsTester().addEqualityGroup(ViewData.create(cumulativeView, ENTRIES, CumulativeData.create(Timestamp.fromMillis(1000), Timestamp.fromMillis(2000))), ViewData.create(cumulativeView, ENTRIES, CumulativeData.create(Timestamp.fromMillis(1000), Timestamp.fromMillis(2000)))).addEqualityGroup(ViewData.create(cumulativeView, ENTRIES, CumulativeData.create(Timestamp.fromMillis(1000), Timestamp.fromMillis(3000)))).addEqualityGroup(ViewData.create(intervalView, ENTRIES, IntervalData.create(Timestamp.fromMillis(2000))), ViewData.create(intervalView, ENTRIES, IntervalData.create(Timestamp.fromMillis(2000)))).addEqualityGroup(ViewData.create(intervalView, Collections.<List<TagValue>, AggregationData>emptyMap(), IntervalData.create(Timestamp.fromMillis(2000)))).testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) TagValue(io.opencensus.tags.TagValue) Test(org.junit.Test)

Example 18 with TagValue

use of io.opencensus.tags.TagValue in project instrumentation-java by census-instrumentation.

the class StatszZPageHandler method emitViewDataRow.

private static void emitViewDataRow(View view, Entry<List</*@Nullable*/
TagValue>, AggregationData> entry, PrintWriter out, Formatter formatter) {
    out.write("<tr>");
    for (/*@Nullable*/
    TagValue tagValue : entry.getKey()) {
        String tagValueStr = tagValue == null ? "" : tagValue.asString();
        formatter.format("<td class=\"borderRL\">%s</td>", tagValueStr);
    }
    entry.getValue().match(new Function<SumDataDouble, Void>() {

        @Override
        public Void apply(SumDataDouble arg) {
            formatter.format("<td class=\"borderLL\">%.3f</td>", arg.getSum());
            return null;
        }
    }, new Function<SumDataLong, Void>() {

        @Override
        public Void apply(SumDataLong arg) {
            formatter.format("<td class=\"borderLL\">%d</td>", arg.getSum());
            return null;
        }
    }, new Function<CountData, Void>() {

        @Override
        public Void apply(CountData arg) {
            formatter.format("<td class=\"borderLL\">%d</td>", arg.getCount());
            return null;
        }
    }, new Function<DistributionData, Void>() {

        @Override
        public Void apply(DistributionData arg) {
            checkArgument(view.getAggregation() instanceof Distribution, "Distribution expected.");
            formatter.format("<td>%.3f</td>", arg.getMean());
            formatter.format("<td class=\"borderLL\">%d</td>", arg.getCount());
            formatter.format("<td class=\"borderLL\">%.3f</td>", arg.getMax());
            formatter.format("<td class=\"borderLL\">%.3f</td>", arg.getMin());
            formatter.format("<td class=\"borderLL\">%.3f</td>", arg.getSumOfSquaredDeviations());
            emitHistogramBuckets(((Distribution) view.getAggregation()).getBucketBoundaries().getBoundaries(), arg.getBucketCounts(), out, formatter);
            return null;
        }
    }, new Function<LastValueDataDouble, Void>() {

        @Override
        public Void apply(LastValueDataDouble arg) {
            formatter.format("<td>%.3f</td>", arg.getLastValue());
            return null;
        }
    }, new Function<LastValueDataLong, Void>() {

        @Override
        public Void apply(LastValueDataLong arg) {
            formatter.format("<td>%d</td>", arg.getLastValue());
            return null;
        }
    }, new Function<AggregationData, Void>() {

        @Override
        public Void apply(AggregationData arg) {
            if (arg instanceof AggregationData.MeanData) {
                AggregationData.MeanData meanData = (AggregationData.MeanData) arg;
                formatter.format("<td>%.3f</td>", meanData.getMean());
                formatter.format("<td class=\"borderLL\">%d</td>", meanData.getCount());
                return null;
            }
            throw new IllegalArgumentException("Unknown Aggregation.");
        }
    });
    out.write("</tr>");
}
Also used : SumDataLong(io.opencensus.stats.AggregationData.SumDataLong) CountData(io.opencensus.stats.AggregationData.CountData) AggregationData(io.opencensus.stats.AggregationData) LastValueDataDouble(io.opencensus.stats.AggregationData.LastValueDataDouble) DistributionData(io.opencensus.stats.AggregationData.DistributionData) Distribution(io.opencensus.stats.Aggregation.Distribution) SumDataDouble(io.opencensus.stats.AggregationData.SumDataDouble) TagValue(io.opencensus.tags.TagValue) LastValueDataLong(io.opencensus.stats.AggregationData.LastValueDataLong)

Example 19 with TagValue

use of io.opencensus.tags.TagValue in project instrumentation-java by census-instrumentation.

the class BinarySerializationUtils method parseTags.

private static Map<TagKey, TagValueWithMetadata> parseTags(ByteBuffer buffer) throws TagContextDeserializationException {
    Map<TagKey, TagValueWithMetadata> tags = new HashMap<TagKey, TagValueWithMetadata>();
    int limit = buffer.limit();
    // Here chars are equivalent to bytes, since we're using ascii chars.
    int totalChars = 0;
    while (buffer.position() < limit) {
        int type = buffer.get();
        if (type == TAG_FIELD_ID) {
            TagKey key = createTagKey(decodeString(buffer));
            TagValue val = createTagValue(key, decodeString(buffer));
            totalChars += key.getName().length();
            totalChars += val.asString().length();
            tags.put(key, TagValueWithMetadata.create(val, METADATA_UNLIMITED_PROPAGATION));
        } else {
            // TODO(sebright): Consider storing the rest of the byte array in the TagContext.
            break;
        }
    }
    if (totalChars > TAGCONTEXT_SERIALIZED_SIZE_LIMIT) {
        throw new TagContextDeserializationException("Size of TagContext exceeds the maximum serialized size " + TAGCONTEXT_SERIALIZED_SIZE_LIMIT);
    }
    return tags;
}
Also used : TagContextDeserializationException(io.opencensus.tags.propagation.TagContextDeserializationException) TagValueWithMetadata(io.opencensus.implcore.tags.TagValueWithMetadata) HashMap(java.util.HashMap) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue)

Example 20 with TagValue

use of io.opencensus.tags.TagValue in project instrumentation-java by census-instrumentation.

the class CorrelationContextFormat method decodeTag.

// Decodes tag key, value and metadata from the encoded string tag, then puts it into the tag map.
// The format of encoded string tag is name1=value1;properties1=p1;properties2=p2.
private static void decodeTag(String stringTag, Map<TagKey, TagValueWithMetadata> tags) {
    String keyWithValue;
    int firstPropertyIndex = stringTag.indexOf(TAG_PROPERTIES_DELIMITER);
    if (firstPropertyIndex != -1) {
        // Tag with properties.
        keyWithValue = stringTag.substring(0, firstPropertyIndex);
    // TODO(songya): support decoding tag properties.
    } else {
        // Tag without properties.
        keyWithValue = stringTag;
    }
    List<String> keyValuePair = TAG_KEY_VALUE_SPLITTER.splitToList(keyWithValue);
    checkArgument(keyValuePair.size() == 2, "Malformed tag " + stringTag);
    TagKey key = TagKey.create(keyValuePair.get(0).trim());
    TagValue value = TagValue.create(keyValuePair.get(1).trim());
    TagValueWithMetadata valueWithMetadata = TagValueWithMetadata.create(value, METADATA_UNLIMITED_PROPAGATION);
    tags.put(key, valueWithMetadata);
}
Also used : TagValueWithMetadata(io.opencensus.implcore.tags.TagValueWithMetadata) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue)

Aggregations

TagValue (io.opencensus.tags.TagValue)22 StatsTestUtils (io.grpc.internal.testing.StatsTestUtils)8 Metadata (io.grpc.Metadata)7 TagKey (io.opencensus.tags.TagKey)7 Test (org.junit.Test)7 ClientStreamTracer (io.grpc.ClientStreamTracer)5 CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)5 Context (io.grpc.Context)4 SpanContext (io.opencensus.trace.SpanContext)4 Status (io.grpc.Status)3 AggregationData (io.opencensus.stats.AggregationData)3 View (io.opencensus.stats.View)3 TagContext (io.opencensus.tags.TagContext)3 CallOptions (io.grpc.CallOptions)2 ServerStreamTracer (io.grpc.ServerStreamTracer)2 MetricsRecord (io.grpc.internal.testing.StatsTestUtils.MetricsRecord)2 TagValueWithMetadata (io.opencensus.implcore.tags.TagValueWithMetadata)2 ViewData (io.opencensus.stats.ViewData)2 List (java.util.List)2 Map (java.util.Map)2