Search in sources :

Example 11 with TagKey

use of io.opencensus.tags.TagKey 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 12 with TagKey

use of io.opencensus.tags.TagKey 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)

Example 13 with TagKey

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

the class RecordUtils method getTagValues.

@VisibleForTesting
static List</*@Nullable*/
TagValue> getTagValues(Map<? extends TagKey, TagValueWithMetadata> tags, List<? extends TagKey> columns) {
    List<TagValue> /*@Nullable*/
    tagValues = new ArrayList</*@Nullable*/
    TagValue>(columns.size());
    // Every view aggregates every measure. This is similar to doing a GROUPBY view’s keys.
    for (int i = 0; i < columns.size(); ++i) {
        TagKey tagKey = columns.get(i);
        if (!tags.containsKey(tagKey)) {
            @javax.annotation.Nullable TagValue tagValue = UNKNOWN_TAG_VALUE;
            TagKey[] newKeys = RPC_TAG_MAPPINGS.get(tagKey);
            if (newKeys != null) {
                tagValue = getTagValueForDeprecatedRpcTag(tags, newKeys);
            }
            tagValues.add(tagValue);
        } else {
            tagValues.add(tags.get(tagKey).getTagValue());
        }
    }
    return tagValues;
}
Also used : ArrayList(java.util.ArrayList) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 14 with TagKey

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

the class ViewManagerImplTest method testViewDataWithMultipleTagKeys.

@Test
public void testViewDataWithMultipleTagKeys() {
    TagKey key1 = TagKey.create("Key-1");
    TagKey key2 = TagKey.create("Key-2");
    viewManager.registerView(createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(key1, key2)));
    statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.1).record(tagger.emptyBuilder().put(key1, TagValue.create("v1")).put(key2, TagValue.create("v10")).build());
    statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 2.2).record(tagger.emptyBuilder().put(key1, TagValue.create("v1")).put(key2, TagValue.create("v20")).build());
    statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 3.3).record(tagger.emptyBuilder().put(key1, TagValue.create("v2")).put(key2, TagValue.create("v10")).build());
    statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 4.4).record(tagger.emptyBuilder().put(key1, TagValue.create("v1")).put(key2, TagValue.create("v10")).build());
    ViewData viewData = viewManager.getView(VIEW_NAME);
    assertAggregationMapEquals(viewData.getAggregationMap(), ImmutableMap.of(Arrays.asList(TagValue.create("v1"), TagValue.create("v10")), StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 1.1, 4.4), Arrays.asList(TagValue.create("v1"), TagValue.create("v20")), StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 2.2), Arrays.asList(TagValue.create("v2"), TagValue.create("v10")), StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 3.3)), EPSILON);
}
Also used : StatsTestUtil.createEmptyViewData(io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData) ViewData(io.opencensus.stats.ViewData) TagKey(io.opencensus.tags.TagKey) Test(org.junit.Test)

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