io.opencensus.tags.Tag Java Examples

The following examples show how to use io.opencensus.tags.Tag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScopedTagMapTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void addToCurrentTagsWithBuilder() {
  TagContext scopedTags = tagger.emptyBuilder().put(KEY_1, VALUE_1).build();
  Scope scope1 = tagger.withTagContext(scopedTags);
  try {
    Scope scope2 = tagger.currentBuilder().put(KEY_2, VALUE_2).buildScoped();
    try {
      assertThat(tagContextToList(tagger.getCurrentTagContext()))
          .containsExactly(Tag.create(KEY_1, VALUE_1), Tag.create(KEY_2, VALUE_2));
    } finally {
      scope2.close();
    }
    assertThat(tagger.getCurrentTagContext()).isSameInstanceAs(scopedTags);
  } finally {
    scope1.close();
  }
}
 
Example #2
Source File: TaggerImpl.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private static TagMapImpl toTagMapImpl(TagContext tags) {
  if (tags instanceof TagMapImpl) {
    return (TagMapImpl) tags;
  } else {
    Iterator<Tag> i = InternalUtils.getTags(tags);
    if (!i.hasNext()) {
      return TagMapImpl.EMPTY;
    }
    TagMapBuilderImpl builder = new TagMapBuilderImpl();
    while (i.hasNext()) {
      Tag tag = i.next();
      if (tag != null) {
        TagContextUtils.addTagToBuilder(tag, builder);
      }
    }
    return builder.build();
  }
}
 
Example #3
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void roundTrip()
    throws TagContextSerializationException, TagContextDeserializationException {
  Tag[] tags = new Tag[40];
  for (int i = 0; i < tags.length; i++) {
    tags[i] =
        Tag.create(
            TagKey.create(generateRandom(10)),
            TagValue.create(generateRandom(10)),
            METADATA_UNLIMITED_PROPAGATION);
  }
  TagContext tagContext = makeTagContext(tags);
  Map<String, String> carrier = new HashMap<String, String>();
  textFormat.inject(tagContext, carrier, setter);
  TagContext actual = textFormat.extract(carrier, getter);
  assertThat(TagsTestUtil.tagContextToList(actual))
      .containsExactlyElementsIn(TagsTestUtil.tagContextToList(tagContext));
}
 
Example #4
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void record_StatsDisabled() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(KEY),
          Cumulative.create());

  viewManager.registerView(view);
  statsComponent.setState(StatsCollectionState.DISABLED);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.0)
      .record(new SimpleTagContext(Tag.create(KEY, VALUE)));
  assertThat(viewManager.getView(VIEW_NAME)).isEqualTo(createEmptyViewData(view));
}
 
Example #5
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void record_MapDeprecatedRpcConstants() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(RecordUtils.RPC_METHOD));

  viewManager.registerView(view);
  MeasureMap statsRecord = statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0);
  statsRecord.record(new SimpleTagContext(Tag.create(RecordUtils.GRPC_CLIENT_METHOD, VALUE)));
  ViewData viewData = viewManager.getView(VIEW_NAME);

  // There should be two entries.
  StatsTestUtil.assertAggregationMapEquals(
      viewData.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0)),
      1e-6);
}
 
Example #6
Source File: BinarySerializationUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
static byte[] serializeBinary(TagContext tags) throws TagContextSerializationException {
  // Use a ByteArrayDataOutput to avoid needing to handle IOExceptions.
  final ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
  byteArrayDataOutput.write(VERSION_ID);
  int totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars.
  for (Iterator<Tag> i = InternalUtils.getTags(tags); i.hasNext(); ) {
    Tag tag = i.next();
    if (TagTtl.NO_PROPAGATION.equals(tag.getTagMetadata().getTagTtl())) {
      continue;
    }
    totalChars += tag.getKey().getName().length();
    totalChars += tag.getValue().asString().length();
    encodeTag(tag, byteArrayDataOutput);
  }
  if (totalChars > TAGCONTEXT_SERIALIZED_SIZE_LIMIT) {
    throw new TagContextSerializationException(
        "Size of TagContext exceeds the maximum serialized size "
            + TAGCONTEXT_SERIALIZED_SIZE_LIMIT);
  }
  return byteArrayDataOutput.toByteArray();
}
 
Example #7
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void recordTwice() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(KEY),
          Cumulative.create());
  viewManager.registerView(view);
  MeasureMap statsRecord = statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0);
  statsRecord.record(new SimpleTagContext(Tag.create(KEY, VALUE)));
  statsRecord.record(new SimpleTagContext(Tag.create(KEY, VALUE_2)));
  ViewData viewData = viewManager.getView(VIEW_NAME);

  // There should be two entries.
  StatsTestUtil.assertAggregationMapEquals(
      viewData.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0),
          Arrays.asList(VALUE_2),
          StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0)),
      1e-6);
}
 
Example #8
Source File: TagMapImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testEquals() {
  new EqualsTester()
      .addEqualityGroup(
          tagger.emptyBuilder().put(K1, V1).put(K2, V2).build(),
          tagger.emptyBuilder().put(K1, V1).put(K2, V2).build(),
          tagger.emptyBuilder().put(K2, V2).put(K1, V1).build(),
          new TagContext() {
            @Override
            protected Iterator<Tag> getIterator() {
              return Lists.<Tag>newArrayList(Tag.create(K1, V1), Tag.create(K2, V2)).iterator();
            }
          })
      .addEqualityGroup(tagger.emptyBuilder().put(K1, V1).put(K2, V1).build())
      .addEqualityGroup(tagger.emptyBuilder().put(K1, V2).put(K2, V1).build())
      .testEquals();
}
 
Example #9
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void record_CurrentContextSet() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(KEY),
          Cumulative.create());
  viewManager.registerView(view);
  TagContext tags = new SimpleTagContext(Tag.create(KEY, VALUE));
  Context orig = ContextUtils.withValue(Context.current(), tags).attach();
  try {
    statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0).record();
  } finally {
    Context.current().detach(orig);
  }
  ViewData viewData = viewManager.getView(VIEW_NAME);

  // record() should have used the given TagContext.
  assertThat(viewData.getAggregationMap().keySet()).containsExactly(Arrays.asList(VALUE));
}
 
Example #10
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void inject_SizeTooLarge() throws TagContextSerializationException {
  Tag[] tags = new Tag[40];
  for (int i = 0; i < tags.length; i++) {
    tags[i] =
        Tag.create(
            TagKey.create(generateRandom(240)),
            TagValue.create(generateRandom(240)),
            METADATA_UNLIMITED_PROPAGATION);
  }
  TagContext tagContext = makeTagContext(tags);
  Map<String, String> carrier = new HashMap<String, String>();
  thrown.expect(TagContextSerializationException.class);
  textFormat.inject(tagContext, carrier, setter);
}
 
Example #11
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void inject_MixedPropagatingAndNonPropagatingTags()
    throws TagContextSerializationException {
  Map<String, String> carrier = new HashMap<String, String>();
  Tag tag = Tag.create(K1, V1, METADATA_NO_PROPAGATION);
  textFormat.inject(makeTagContext(T1, tag, T2), carrier, setter);
  assertThat(carrier).containsExactly(CORRELATION_CONTEXT, "k1=v1,k2=v2");
}
 
Example #12
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void extract_WithUnknownProperties() throws TagContextDeserializationException {
  Map<String, String> carrier =
      Collections.singletonMap(CORRELATION_CONTEXT, "k1=v1;property1=p1;property2=p2,k2=v2");
  Tag expected = Tag.create(K1, TagValue.create("v1"), METADATA_UNLIMITED_PROPAGATION);
  TagContext tagContext = textFormat.extract(carrier, getter);
  assertThat(TagsTestUtil.tagContextToList(tagContext)).containsExactly(expected, T2);
}
 
Example #13
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void extract_TrimSpaces() throws TagContextDeserializationException {
  Map<String, String> carrier = Collections.singletonMap(CORRELATION_CONTEXT, "k1= v1, k2=v2 ");
  Tag expected1 = Tag.create(K1, V1, METADATA_UNLIMITED_PROPAGATION);
  Tag expected2 = Tag.create(K2, V2, METADATA_UNLIMITED_PROPAGATION);
  TagContext tagContext = textFormat.extract(carrier, getter);
  assertThat(TagsTestUtil.tagContextToList(tagContext)).containsExactly(expected1, expected2);
}
 
Example #14
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void extract_OverrideTagWithSpaces() throws TagContextDeserializationException {
  Map<String, String> carrier = Collections.singletonMap(CORRELATION_CONTEXT, "k1= v1, k1=v2 ");
  Tag expected = Tag.create(K1, V2, METADATA_UNLIMITED_PROPAGATION);
  TagContext tagContext = textFormat.extract(carrier, getter);
  assertThat(TagsTestUtil.tagContextToList(tagContext)).containsExactly(expected);
}
 
Example #15
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static TagContext makeTagContext(final Tag... tags) {
  return new TagContext() {
    @Override
    public Iterator<Tag> getIterator() {
      return Arrays.<Tag>asList(tags).iterator();
    }
  };
}
 
Example #16
Source File: TagContextBinarySerializerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Tag> getIterator() {
  return ImmutableSet.<Tag>of(
          Tag.create(
              TagKey.create("key"), TagValue.create("value"), METADATA_NO_PROPAGATION))
      .iterator();
}
 
Example #17
Source File: TagMapImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testIterator() {
  TagMapImpl tags = new TagMapImpl(ImmutableMap.of(K1, VM1, K2, VM2));
  Iterator<Tag> i = tags.getIterator();
  assertTrue(i.hasNext());
  Tag tag1 = i.next();
  assertTrue(i.hasNext());
  Tag tag2 = i.next();
  assertFalse(i.hasNext());
  assertThat(Arrays.asList(tag1, tag2)).containsExactly(Tag.create(K1, V1), Tag.create(K2, V2));
  thrown.expect(NoSuchElementException.class);
  i.next();
}
 
Example #18
Source File: TagMapImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void disallowCallingRemoveOnIterator() {
  TagMapImpl tags = new TagMapImpl(ImmutableMap.of(K1, VM1, K2, VM2));
  Iterator<Tag> i = tags.getIterator();
  i.next();
  thrown.expect(UnsupportedOperationException.class);
  i.remove();
}
 
Example #19
Source File: TaggerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyBuilder_TaggingReenabled() {
  tagsComponent.setState(TaggingState.DISABLED);
  assertThat(tagger.emptyBuilder()).isSameInstanceAs(NoopTagMapBuilder.INSTANCE);
  tagsComponent.setState(TaggingState.ENABLED);
  TagContextBuilder builder = tagger.emptyBuilder();
  assertThat(builder).isInstanceOf(TagMapBuilderImpl.class);
  assertThat(tagContextToList(builder.put(K1, V1).build())).containsExactly(Tag.create(K1, V1));
}
 
Example #20
Source File: TaggerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void currentBuilder_RemoveDuplicateTags() {
  Tag tag1 = Tag.create(K1, V1);
  Tag tag2 = Tag.create(K1, V2);
  TagContext tagContextWithDuplicateTags = new SimpleTagContext(tag1, tag2);
  TagContextBuilder result = getResultOfCurrentBuilder(tagContextWithDuplicateTags);
  assertThat(tagContextToList(result.build())).containsExactly(tag2);
}
 
Example #21
Source File: TaggerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void toBuilder_RemoveDuplicatesFromUnknownTagContext() {
  Tag tag1 = Tag.create(K1, V1);
  Tag tag2 = Tag.create(K1, V2);
  TagContext tagContextWithDuplicateTags = new SimpleTagContext(tag1, tag2);
  TagContext newTagContext = tagger.toBuilder(tagContextWithDuplicateTags).build();
  assertThat(tagContextToList(newTagContext)).containsExactly(tag2);
}
 
Example #22
Source File: TaggerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #23
Source File: TaggerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void withTagContext_RemoveDuplicatesFromUnknownTagContext() {
  Tag tag1 = Tag.create(K1, V1);
  Tag tag2 = Tag.create(K1, V2);
  TagContext tagContextWithDuplicateTags = new SimpleTagContext(tag1, tag2);
  TagContext result = getResultOfWithTagContext(tagContextWithDuplicateTags);
  assertThat(tagContextToList(result)).containsExactly(tag2);
}
 
Example #24
Source File: ScopedTagMapTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createBuilderFromCurrentTags() {
  TagContext scopedTags = tagger.emptyBuilder().put(KEY_1, VALUE_1).build();
  Scope scope = tagger.withTagContext(scopedTags);
  try {
    TagContext newTags = tagger.currentBuilder().put(KEY_2, VALUE_2).build();
    assertThat(tagContextToList(newTags))
        .containsExactly(Tag.create(KEY_1, VALUE_1), Tag.create(KEY_2, VALUE_2));
    assertThat(tagger.getCurrentTagContext()).isSameInstanceAs(scopedTags);
  } finally {
    scope.close();
  }
}
 
Example #25
Source File: ScopedTagMapTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void setCurrentTagsWithBuilder() {
  assertThat(tagContextToList(tagger.getCurrentTagContext())).isEmpty();
  Scope scope = tagger.emptyBuilder().put(KEY_1, VALUE_1).buildScoped();
  try {
    assertThat(tagContextToList(tagger.getCurrentTagContext()))
        .containsExactly(Tag.create(KEY_1, VALUE_1));
  } finally {
    scope.close();
  }
  assertThat(tagContextToList(tagger.getCurrentTagContext())).isEmpty();
}
 
Example #26
Source File: ScopedTagMapTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void multiScopeTagMapWithMetadata() {
  TagContext scopedTags =
      tagger
          .emptyBuilder()
          .put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION)
          .put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION)
          .build();
  Scope scope1 = tagger.withTagContext(scopedTags);
  try { // Scope 1
    Scope scope2 =
        tagger
            .currentBuilder()
            .put(KEY_3, VALUE_3, METADATA_NO_PROPAGATION)
            .put(KEY_2, VALUE_4, METADATA_NO_PROPAGATION)
            .buildScoped();
    try { // Scope 2
      assertThat(tagContextToList(tagger.getCurrentTagContext()))
          .containsExactly(
              Tag.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION),
              Tag.create(KEY_2, VALUE_4, METADATA_NO_PROPAGATION),
              Tag.create(KEY_3, VALUE_3, METADATA_NO_PROPAGATION));
    } finally {
      scope2.close(); // Close Scope 2
    }
    assertThat(tagger.getCurrentTagContext()).isSameInstanceAs(scopedTags);
  } finally {
    scope1.close();
  }
}
 
Example #27
Source File: StatsTestUtils.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
protected Iterator<Tag> getIterator() {
  return Iterators.transform(
      tags.entrySet().iterator(),
      new Function<Map.Entry<TagKey, TagValue>, Tag>() {
        @Override
        public Tag apply(@Nullable Map.Entry<TagKey, TagValue> entry) {
          return Tag.create(entry.getKey(), entry.getValue(), METADATA_PROPAGATING);
        }
      });
}
 
Example #28
Source File: StatsTestUtils.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
protected Iterator<Tag> getIterator() {
  return Iterators.transform(
      tags.entrySet().iterator(),
      new Function<Map.Entry<TagKey, TagValue>, Tag>() {
        @Override
        public Tag apply(@Nullable Map.Entry<TagKey, TagValue> entry) {
          return Tag.create(entry.getKey(), entry.getValue());
        }
      });
}
 
Example #29
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void inject_SkipNonPropagatingTag() throws TagContextSerializationException {
  Map<String, String> carrier = new HashMap<String, String>();
  Tag tag = Tag.create(K1, V1, METADATA_NO_PROPAGATION);
  textFormat.inject(makeTagContext(tag), carrier, setter);
  assertThat(carrier).containsExactly(CORRELATION_CONTEXT, "");
}
 
Example #30
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void inject_TooManyTags() throws TagContextSerializationException {
  Tag[] tags = new Tag[CorrelationContextFormat.MAX_NUMBER_OF_TAGS + 1];
  for (int i = 0; i < tags.length; i++) {
    tags[i] =
        Tag.create(
            TagKey.create("k" + i), TagValue.create("v" + i), METADATA_UNLIMITED_PROPAGATION);
  }
  TagContext tagContext = makeTagContext(tags);
  Map<String, String> carrier = new HashMap<String, String>();
  thrown.expect(TagContextSerializationException.class);
  textFormat.inject(tagContext, carrier, setter);
}