Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 7dc2112

Browse files
authored
Tags: Add TagContextBuilder.putLocal(). (#1851)
1 parent 8a86284 commit 7dc2112

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Add an option to allow users to override the default "opencensus_task" metric label in Stackdriver Stats Exporter.
55
- Allow setting custom namespace in Prometheus exporter.
66
- Add Cumulative (`DoubleCumulative`, `LongCumulative`, `DerivedDoubleCumulative`, `DerivedLongCumulative`) APIs.
7+
- Add a convenient API `TagContextBuilder.putLocal()` that adds non-propagating tags.
78

89
## 0.20.0 - 2019-03-28
910
- Add OpenCensus Java OC-Agent Trace Exporter.

api/src/main/java/io/opencensus/tags/TagContextBuilder.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
package io.opencensus.tags;
1818

1919
import io.opencensus.common.Scope;
20+
import io.opencensus.tags.TagMetadata.TagTtl;
2021

2122
/**
2223
* Builder for the {@link TagContext} class.
2324
*
2425
* @since 0.8
2526
*/
2627
public abstract class TagContextBuilder {
28+
29+
private static final TagMetadata METADATA_NO_PROPAGATION =
30+
TagMetadata.create(TagTtl.NO_PROPAGATION);
31+
2732
/**
2833
* Adds the key/value pair regardless of whether the key is present.
2934
*
@@ -36,7 +41,8 @@ public abstract class TagContextBuilder {
3641
* @param value the {@code TagValue} to set for the given key.
3742
* @return this
3843
* @since 0.8
39-
* @deprecated in favor of {@link #put(TagKey, TagValue, TagMetadata)}.
44+
* @deprecated in favor of {@link #put(TagKey, TagValue, TagMetadata)}, or {@link
45+
* #putLocal(TagKey, TagValue)} if you only want in-process tags.
4046
*/
4147
@Deprecated
4248
public abstract TagContextBuilder put(TagKey key, TagValue value);
@@ -56,6 +62,21 @@ public TagContextBuilder put(TagKey key, TagValue value, TagMetadata tagMetadata
5662
return builder;
5763
}
5864

65+
/**
66+
* Adds a non-propagating tag to this {@code TagContextBuilder}.
67+
*
68+
* <p>This is equivalent to calling {@code put(key, value,
69+
* TagMetadata.create(TagTtl.NO_PROPAGATION))}.
70+
*
71+
* @param key the {@code TagKey} which will be set.
72+
* @param value the {@code TagValue} to set for the given key.
73+
* @return this
74+
* @since 0.21
75+
*/
76+
public final TagContextBuilder putLocal(TagKey key, TagValue value) {
77+
return put(key, value, METADATA_NO_PROPAGATION);
78+
}
79+
5980
/**
6081
* Removes the key if it exists.
6182
*

impl_core/src/test/java/io/opencensus/implcore/tags/TagMapImplTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class TagMapImplTest {
5353

5454
private static final TagMetadata METADATA_UNLIMITED_PROPAGATION =
5555
TagMetadata.create(TagTtl.UNLIMITED_PROPAGATION);
56+
private static final TagMetadata METADATA_NO_PROPAGATION =
57+
TagMetadata.create(TagTtl.NO_PROPAGATION);
5658

5759
private static final TagKey K1 = TagKey.create("k1");
5860
private static final TagKey K2 = TagKey.create("k2");
@@ -111,6 +113,13 @@ public void put_nullValue() {
111113
builder.put(K2, null);
112114
}
113115

116+
@Test
117+
public void putLocal() {
118+
TagContext tags1 = tagger.emptyBuilder().put(K1, V1, METADATA_NO_PROPAGATION).build();
119+
TagContext tags2 = tagger.emptyBuilder().putLocal(K1, V1).build();
120+
assertThat(tags1).isEqualTo(tags2);
121+
}
122+
114123
@Test
115124
public void remove_existingKey() {
116125
TagContext tags = new TagMapImpl(ImmutableMap.of(K1, VM1, K2, VM2));

0 commit comments

Comments
 (0)