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

Commit beba028

Browse files
authored
Tags: Add TagMetadata. (#1768)
1 parent 2bcac95 commit beba028

File tree

23 files changed

+482
-59
lines changed

23 files changed

+482
-59
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Add `metrics.data` package to hold common classes shared between stats and metrics.
1010
- Refactor `Exemplar` and `AttachmentValue` to be under `metrics.data`. Note that this is a breaking change
1111
if you're using the `Exemplar` classes or APIs in the previous releases.
12+
- Add `TagMetadata` that defines the properties associated with a `Tag`.
1213

1314
# 0.19.0 - 2019-01-28
1415
- Add an artifact `opencensus-contrib-http-jetty-client` for instrumenting jetty http client. Add extractor for Jetty Client.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,21 @@ private static final class NoopTagContextBuilder extends TagContextBuilder {
150150
static final TagContextBuilder INSTANCE = new NoopTagContextBuilder();
151151

152152
@Override
153+
@SuppressWarnings("deprecation")
153154
public TagContextBuilder put(TagKey key, TagValue value) {
154155
Utils.checkNotNull(key, "key");
155156
Utils.checkNotNull(value, "value");
156157
return this;
157158
}
158159

160+
@Override
161+
public TagContextBuilder put(TagKey key, TagValue value, TagMetadata tagMetadata) {
162+
Utils.checkNotNull(key, "key");
163+
Utils.checkNotNull(value, "value");
164+
Utils.checkNotNull(tagMetadata, "tagMetadata");
165+
return this;
166+
}
167+
159168
@Override
160169
public TagContextBuilder remove(TagKey key) {
161170
Utils.checkNotNull(key, "key");

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

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

1919
import com.google.auto.value.AutoValue;
20+
import io.opencensus.tags.TagMetadata.TagTtl;
2021
import javax.annotation.concurrent.Immutable;
2122

2223
/**
@@ -28,18 +29,41 @@
2829
@AutoValue
2930
public abstract class Tag {
3031

32+
private static final TagMetadata METADATA_UNLIMITED_PROPAGATION =
33+
TagMetadata.create(TagTtl.UNLIMITED_PROPAGATION);
34+
3135
Tag() {}
3236

3337
/**
3438
* Creates a {@code Tag} from the given key and value.
3539
*
40+
* <p>For backwards-compatibility this method still produces propagating {@link Tag}s.
41+
*
42+
* <p>This is equivalent to calling {@code create(key, value,
43+
* TagMetadata.create(TagTtl.UNLIMITED_PROPAGATION))}.
44+
*
3645
* @param key the tag key.
3746
* @param value the tag value.
3847
* @return a {@code Tag} with the given key and value.
3948
* @since 0.8
49+
* @deprecated in favor of {@link #create(TagKey, TagValue, TagMetadata)}.
4050
*/
51+
@Deprecated
4152
public static Tag create(TagKey key, TagValue value) {
42-
return new AutoValue_Tag(key, value);
53+
return create(key, value, METADATA_UNLIMITED_PROPAGATION);
54+
}
55+
56+
/**
57+
* Creates a {@code Tag} from the given key, value and metadata.
58+
*
59+
* @param key the tag key.
60+
* @param value the tag value.
61+
* @param tagMetadata the tag metadata.
62+
* @return a {@code Tag}.
63+
* @since 0.20
64+
*/
65+
public static Tag create(TagKey key, TagValue value, TagMetadata tagMetadata) {
66+
return new AutoValue_Tag(key, value, tagMetadata);
4367
}
4468

4569
/**
@@ -57,4 +81,12 @@ public static Tag create(TagKey key, TagValue value) {
5781
* @since 0.8
5882
*/
5983
public abstract TagValue getValue();
84+
85+
/**
86+
* Returns the {@link TagMetadata} associated with this {@link Tag}.
87+
*
88+
* @return the {@code TagMetadata}.
89+
* @since 0.20
90+
*/
91+
public abstract TagMetadata getTagMetadata();
6092
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,31 @@ public abstract class TagContextBuilder {
2828
/**
2929
* Adds the key/value pair regardless of whether the key is present.
3030
*
31+
* <p>For backwards-compatibility this method still produces propagating {@link Tag}s.
32+
*
33+
* <p>Equivalent to calling {@code put(key, value,
34+
* TagMetadata.create(TagTtl.UNLIMITED_PROPAGATION))}.
35+
*
3136
* @param key the {@code TagKey} which will be set.
3237
* @param value the {@code TagValue} to set for the given key.
3338
* @return this
3439
* @since 0.8
40+
* @deprecated in favor of {@link #put(TagKey, TagValue, TagMetadata)}.
3541
*/
42+
@Deprecated
3643
public abstract TagContextBuilder put(TagKey key, TagValue value);
3744

45+
/**
46+
* Adds the key/value pair and metadata regardless of whether the key is present.
47+
*
48+
* @param key the {@code TagKey} which will be set.
49+
* @param value the {@code TagValue} to set for the given key.
50+
* @param tagMetadata the {@code TagMetadata} associated with this {@link Tag}.
51+
* @return this
52+
* @since 0.20
53+
*/
54+
public abstract TagContextBuilder put(TagKey key, TagValue value, TagMetadata tagMetadata);
55+
3856
/**
3957
* Removes the key if it exists.
4058
*
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.tags;
18+
19+
import com.google.auto.value.AutoValue;
20+
import javax.annotation.concurrent.Immutable;
21+
22+
/**
23+
* {@link TagMetadata} contains properties associated with a {@link Tag}.
24+
*
25+
* <p>For now only the property {@link TagTtl} is defined. In future, additional properties may be
26+
* added to address specific situations.
27+
*
28+
* @since 0.20
29+
*/
30+
@Immutable
31+
@AutoValue
32+
public abstract class TagMetadata {
33+
34+
TagMetadata() {}
35+
36+
/**
37+
* Creates a {@link TagMetadata} with the given {@link TagTtl}.
38+
*
39+
* @param tagTtl TTL of a {@code Tag}.
40+
* @return a {@code TagMetadata}.
41+
* @since 0.20
42+
*/
43+
public static TagMetadata create(TagTtl tagTtl) {
44+
return new AutoValue_TagMetadata(tagTtl);
45+
}
46+
47+
/**
48+
* Returns the {@link TagTtl} of this {@link TagMetadata}.
49+
*
50+
* @return the {@code TagTtl}.
51+
* @since 0.20
52+
*/
53+
public abstract TagTtl getTagTtl();
54+
55+
/**
56+
* {@link TagTtl} is an integer that represents number of hops a tag can propagate.
57+
*
58+
* <p>Anytime a sender serializes a tag, sends it over the wire and receiver deserializes the tag
59+
* then the tag is considered to have travelled one hop.
60+
*
61+
* <p>There could be one or more proxy(ies) between sender and receiver. Proxies are treated as
62+
* transparent entities and they are not counted as hops.
63+
*
64+
* <p>For now, only special values of {@link TagTtl} are supported.
65+
*
66+
* @since 0.20
67+
*/
68+
public enum TagTtl {
69+
70+
/**
71+
* A {@link Tag} with {@link TagTtl#NO_PROPAGATION} is considered to have local scope and is
72+
* used within the process where it's created.
73+
*
74+
* @since 0.20
75+
*/
76+
NO_PROPAGATION(0),
77+
78+
/**
79+
* A {@link Tag} with {@link TagTtl#UNLIMITED_PROPAGATION} can propagate unlimited hops.
80+
*
81+
* <p>However, it is still subject to outgoing and incoming (on remote side) filter criteria.
82+
*
83+
* <p>{@link TagTtl#UNLIMITED_PROPAGATION} is typical used to track a request, which may be
84+
* processed across multiple entities.
85+
*
86+
* @since 0.20
87+
*/
88+
UNLIMITED_PROPAGATION(-1);
89+
90+
private final int hops;
91+
92+
private TagTtl(int hops) {
93+
this.hops = hops;
94+
}
95+
}
96+
}

api/src/main/java/io/opencensus/tags/propagation/TagContextBinarySerializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package io.opencensus.tags.propagation;
1818

19+
import io.opencensus.tags.Tag;
1920
import io.opencensus.tags.TagContext;
21+
import io.opencensus.tags.TagMetadata;
22+
import io.opencensus.tags.TagMetadata.TagTtl;
2023

2124
/**
2225
* Object for serializing and deserializing {@link TagContext}s with the binary format.
@@ -34,6 +37,9 @@ public abstract class TagContextBinarySerializer {
3437
*
3538
* <p>This method should be the inverse of {@link #fromByteArray}.
3639
*
40+
* <p>{@link Tag}s that have a {@link TagMetadata} with {@link TagTtl#NO_PROPAGATION} will not be
41+
* serialized.
42+
*
3743
* @param tags the {@code TagContext} to serialize.
3844
* @return the on-the-wire representation of a {@code TagContext}.
3945
* @throws TagContextSerializationException if the result would be larger than the maximum allowed

api/src/test/java/io/opencensus/tags/NoopTagsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ public void noopTagContextBuilder_Put_DisallowsNullValue() {
134134
noopBuilder.put(KEY, null);
135135
}
136136

137+
@Test
138+
public void noopTagContextBuilder_Put_DisallowsNullTagMetadata() {
139+
TagContextBuilder noopBuilder = NoopTags.getNoopTagContextBuilder();
140+
thrown.expect(NullPointerException.class);
141+
noopBuilder.put(KEY, VALUE, null);
142+
}
143+
137144
@Test
138145
public void noopTagContextBuilder_Remove_DisallowsNullKey() {
139146
TagContextBuilder noopBuilder = NoopTags.getNoopTagContextBuilder();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.tags;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.common.testing.EqualsTester;
22+
import io.opencensus.tags.TagMetadata.TagTtl;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
/** Tests for {@link TagMetadata}. */
28+
@RunWith(JUnit4.class)
29+
public class TagMetadataTest {
30+
31+
@Test
32+
public void testGetTagTtl() {
33+
TagMetadata tagMetadata = TagMetadata.create(TagTtl.NO_PROPAGATION);
34+
assertThat(tagMetadata.getTagTtl()).isEqualTo(TagTtl.NO_PROPAGATION);
35+
}
36+
37+
@Test
38+
public void testEquals() {
39+
new EqualsTester()
40+
.addEqualityGroup(
41+
TagMetadata.create(TagTtl.NO_PROPAGATION), TagMetadata.create(TagTtl.NO_PROPAGATION))
42+
.addEqualityGroup(TagMetadata.create(TagTtl.UNLIMITED_PROPAGATION))
43+
.testEquals();
44+
}
45+
}

api/src/test/java/io/opencensus/tags/TagTest.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020

2121
import com.google.common.testing.EqualsTester;
22+
import io.opencensus.tags.TagMetadata.TagTtl;
2223
import org.junit.Test;
2324
import org.junit.runner.RunWith;
2425
import org.junit.runners.JUnit4;
@@ -27,20 +28,41 @@
2728
@RunWith(JUnit4.class)
2829
public final class TagTest {
2930

31+
private static final TagKey KEY = TagKey.create("KEY");
32+
private static final TagKey KEY_2 = TagKey.create("KEY2");
33+
private static final TagValue VALUE = TagValue.create("VALUE");
34+
private static final TagValue VALUE_2 = TagValue.create("VALUE2");
35+
private static final TagMetadata METADATA_UNLIMITED_PROPAGATION =
36+
TagMetadata.create(TagTtl.UNLIMITED_PROPAGATION);
37+
private static final TagMetadata METADATA_NO_PROPAGATION =
38+
TagMetadata.create(TagTtl.NO_PROPAGATION);
39+
3040
@Test
3141
public void testGetKey() {
32-
assertThat(Tag.create(TagKey.create("k"), TagValue.create("v")).getKey())
33-
.isEqualTo(TagKey.create("k"));
42+
assertThat(Tag.create(KEY, VALUE).getKey()).isEqualTo(KEY);
43+
}
44+
45+
@Test
46+
public void testGetTagMetadata() {
47+
assertThat(Tag.create(KEY, VALUE, METADATA_NO_PROPAGATION).getTagMetadata())
48+
.isEqualTo(METADATA_NO_PROPAGATION);
49+
}
50+
51+
@Test
52+
public void testGetTagMetadata_default() {
53+
assertThat(Tag.create(KEY, VALUE).getTagMetadata()).isEqualTo(METADATA_UNLIMITED_PROPAGATION);
3454
}
3555

3656
@Test
3757
public void testTagEquals() {
3858
new EqualsTester()
3959
.addEqualityGroup(
40-
Tag.create(TagKey.create("Key"), TagValue.create("foo")),
41-
Tag.create(TagKey.create("Key"), TagValue.create("foo")))
42-
.addEqualityGroup(Tag.create(TagKey.create("Key"), TagValue.create("bar")))
43-
.addEqualityGroup(Tag.create(TagKey.create("Key2"), TagValue.create("foo")))
60+
Tag.create(KEY, VALUE),
61+
Tag.create(KEY, VALUE),
62+
Tag.create(KEY, VALUE, METADATA_UNLIMITED_PROPAGATION))
63+
.addEqualityGroup(Tag.create(KEY, VALUE_2))
64+
.addEqualityGroup(Tag.create(KEY_2, VALUE))
65+
.addEqualityGroup(Tag.create(KEY, VALUE, METADATA_NO_PROPAGATION))
4466
.testEquals();
4567
}
4668
}

contrib/http_util/src/main/java/io/opencensus/contrib/http/HttpClientHandler.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.opencensus.contrib.http;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static io.opencensus.contrib.http.HttpRequestContext.METADATA_NO_PROPAGATION;
2021
import static io.opencensus.contrib.http.util.HttpMeasureConstants.HTTP_CLIENT_METHOD;
2122
import static io.opencensus.contrib.http.util.HttpMeasureConstants.HTTP_CLIENT_RECEIVED_BYTES;
2223
import static io.opencensus.contrib.http.util.HttpMeasureConstants.HTTP_CLIENT_ROUNDTRIP_LATENCY;
@@ -162,10 +163,14 @@ private void recordStats(
162163
TagContext startCtx =
163164
tagger
164165
.toBuilder(context.tagContext)
165-
.put(HTTP_CLIENT_METHOD, TagValue.create(methodStr == null ? "" : methodStr))
166+
.put(
167+
HTTP_CLIENT_METHOD,
168+
TagValue.create(methodStr == null ? "" : methodStr),
169+
METADATA_NO_PROPAGATION)
166170
.put(
167171
HTTP_CLIENT_STATUS,
168-
TagValue.create(status == 0 ? "error" : Integer.toString(status)))
172+
TagValue.create(status == 0 ? "error" : Integer.toString(status)),
173+
METADATA_NO_PROPAGATION)
169174
.build();
170175

171176
statsRecorder

0 commit comments

Comments
 (0)