Java Code Examples for io.opencensus.tags.Tag#create()

The following examples show how to use io.opencensus.tags.Tag#create() . 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: 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 2
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 3
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 4
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 5
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);
}
 
Example 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
Source File: TagsBenchmark.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Tag tagCreation(Data data) {
  return Tag.create(data.tagKey, data.tagValue, TagsBenchmarksUtil.UNLIMITED_PROPAGATION);
}
 
Example 15
Source File: StatsRecorderImplTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private void recordWithAttachments() {
  TagContext context = new SimpleTagContext(Tag.create(KEY, VALUE));

  // The test Distribution has bucket boundaries [-10.0, 0.0, 10.0].

  testClock.advanceTime(ONE_SECOND); // 1st second.
  // -1.0 is in the 2nd bucket [-10.0, 0.0).
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, -1.0)
      .putAttachment("k1", ATTACHMENT_VALUE_1)
      .record(context);

  testClock.advanceTime(ONE_SECOND); // 2nd second.
  // 1.0 is in the 3rd bucket [0.0, 10.0).
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.0)
      .putAttachment("k2", ATTACHMENT_VALUE_2)
      .record(context);

  testClock.advanceTime(ONE_SECOND); // 3rd second.
  // 12.0 is in the 4th bucket [10.0, +Inf).
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 12.0)
      .putAttachment("k1", ATTACHMENT_VALUE_3)
      .record(context);

  testClock.advanceTime(ONE_SECOND); // 4th second.
  // -20.0 is in the 1st bucket [-Inf, -10.0).
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, -20.0)
      .putAttachment("k3", ATTACHMENT_VALUE_1)
      .record(context);

  testClock.advanceTime(ONE_SECOND); // 5th second.
  // -5.0 is in the 2nd bucket [-10.0, 0), should overwrite the previous exemplar -1.0.
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, -5.0)
      .putAttachment("k3", ATTACHMENT_VALUE_3)
      .record(context);

  testClock.advanceTime(ONE_SECOND); // 6th second.
  // -3.0 is in the 2nd bucket [-10.0, 0), but this value doesn't come with attachments, so it
  // shouldn't overwrite the previous exemplar (-5.0).
  statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, -3.0).record(context);
}