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

Commit c35bc41

Browse files
authored
Stats: Map deprecated RPC tags when record against new tags. (#1854)
1 parent e8cbf85 commit c35bc41

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

impl_core/src/main/java/io/opencensus/implcore/stats/RecordUtils.java

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

1919
import com.google.common.annotations.VisibleForTesting;
20+
import com.google.common.collect.ImmutableMap;
2021
import com.google.common.collect.Maps;
2122
import io.opencensus.common.Function;
2223
import io.opencensus.common.Functions;
@@ -62,6 +63,19 @@ final class RecordUtils {
6263

6364
@javax.annotation.Nullable @VisibleForTesting static final TagValue UNKNOWN_TAG_VALUE = null;
6465

66+
// TODO(songy23): remove the mapping once we completely remove the deprecated RPC constants.
67+
@VisibleForTesting static final TagKey RPC_STATUS = TagKey.create("canonical_status");
68+
@VisibleForTesting static final TagKey RPC_METHOD = TagKey.create("method");
69+
@VisibleForTesting static final TagKey GRPC_CLIENT_STATUS = TagKey.create("grpc_client_status");
70+
@VisibleForTesting static final TagKey GRPC_CLIENT_METHOD = TagKey.create("grpc_client_method");
71+
@VisibleForTesting static final TagKey GRPC_SERVER_STATUS = TagKey.create("grpc_server_status");
72+
@VisibleForTesting static final TagKey GRPC_SERVER_METHOD = TagKey.create("grpc_server_method");
73+
private static final Map<TagKey, TagKey[]> RPC_TAG_MAPPINGS =
74+
ImmutableMap.<TagKey, TagKey[]>builder()
75+
.put(RPC_STATUS, new TagKey[] {GRPC_CLIENT_STATUS, GRPC_SERVER_STATUS})
76+
.put(RPC_METHOD, new TagKey[] {GRPC_CLIENT_METHOD, GRPC_SERVER_METHOD})
77+
.build();
78+
6579
static Map<TagKey, TagValueWithMetadata> getTagMap(TagContext ctx) {
6680
if (ctx instanceof TagMapImpl) {
6781
return ((TagMapImpl) ctx).getTags();
@@ -83,15 +97,32 @@ static Map<TagKey, TagValueWithMetadata> getTagMap(TagContext ctx) {
8397
for (int i = 0; i < columns.size(); ++i) {
8498
TagKey tagKey = columns.get(i);
8599
if (!tags.containsKey(tagKey)) {
86-
// replace not found key values by null.
87-
tagValues.add(UNKNOWN_TAG_VALUE);
100+
@javax.annotation.Nullable TagValue tagValue = UNKNOWN_TAG_VALUE;
101+
TagKey[] newKeys = RPC_TAG_MAPPINGS.get(tagKey);
102+
if (newKeys != null) {
103+
tagValue = getTagValueForDeprecatedRpcTag(tags, newKeys);
104+
}
105+
tagValues.add(tagValue);
88106
} else {
89107
tagValues.add(tags.get(tagKey).getTagValue());
90108
}
91109
}
92110
return tagValues;
93111
}
94112

113+
// TODO(songy23): remove the mapping once we completely remove the deprecated RPC constants.
114+
@javax.annotation.Nullable
115+
private static TagValue getTagValueForDeprecatedRpcTag(
116+
Map<? extends TagKey, TagValueWithMetadata> tags, TagKey[] newKeys) {
117+
for (TagKey newKey : newKeys) {
118+
TagValueWithMetadata valueWithMetadata = tags.get(newKey);
119+
if (valueWithMetadata != null) {
120+
return valueWithMetadata.getTagValue();
121+
}
122+
}
123+
return UNKNOWN_TAG_VALUE;
124+
}
125+
95126
/**
96127
* Create an empty {@link MutableAggregation} based on the given {@link Aggregation}.
97128
*

impl_core/src/test/java/io/opencensus/implcore/stats/RecordUtilsTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,22 @@ public class RecordUtilsTest {
6262
private static final TagKey METHOD = TagKey.create("method");
6363
private static final TagValue CALLER_V = TagValue.create("some caller");
6464
private static final TagValue METHOD_V = TagValue.create("some method");
65+
private static final TagValue METHOD_V_2 = TagValue.create("some other method");
66+
private static final TagValue METHOD_V_3 = TagValue.create("the third method");
67+
private static final TagValue STATUS_V = TagValue.create("ok");
68+
private static final TagValue STATUS_V_2 = TagValue.create("error");
6569
private static final TagValueWithMetadata CALLER_V_WITH_MD =
6670
TagValueWithMetadata.create(CALLER_V, METADATA_UNLIMITED_PROPAGATION);
6771
private static final TagValueWithMetadata METHOD_V_WITH_MD =
6872
TagValueWithMetadata.create(METHOD_V, METADATA_UNLIMITED_PROPAGATION);
73+
private static final TagValueWithMetadata METHOD_V_2_WITH_MD =
74+
TagValueWithMetadata.create(METHOD_V_2, METADATA_UNLIMITED_PROPAGATION);
75+
private static final TagValueWithMetadata METHOD_V_3_WITH_MD =
76+
TagValueWithMetadata.create(METHOD_V_3, METADATA_UNLIMITED_PROPAGATION);
77+
private static final TagValueWithMetadata STATUS_V_WITH_MD =
78+
TagValueWithMetadata.create(STATUS_V, METADATA_UNLIMITED_PROPAGATION);
79+
private static final TagValueWithMetadata STATUS_V_2_WITH_MD =
80+
TagValueWithMetadata.create(STATUS_V_2, METADATA_UNLIMITED_PROPAGATION);
6981

7082
@Test
7183
public void testConstants() {
@@ -83,6 +95,75 @@ public void testGetTagValues() {
8395
.inOrder();
8496
}
8597

98+
@Test
99+
public void testGetTagValues_MapDeprecatedRpcTag() {
100+
List<TagKey> columns = Arrays.asList(RecordUtils.RPC_STATUS, RecordUtils.RPC_METHOD);
101+
Map<TagKey, TagValueWithMetadata> tags =
102+
ImmutableMap.of(
103+
RecordUtils.GRPC_CLIENT_METHOD, METHOD_V_WITH_MD,
104+
RecordUtils.GRPC_CLIENT_STATUS, STATUS_V_WITH_MD);
105+
106+
assertThat(RecordUtils.getTagValues(tags, columns))
107+
.containsExactly(STATUS_V, METHOD_V)
108+
.inOrder();
109+
}
110+
111+
@Test
112+
public void testGetTagValues_MapDeprecatedRpcTag_WithServerTag() {
113+
List<TagKey> columns = Arrays.asList(RecordUtils.RPC_STATUS, RecordUtils.RPC_METHOD);
114+
Map<TagKey, TagValueWithMetadata> tags =
115+
ImmutableMap.of(
116+
RecordUtils.GRPC_SERVER_METHOD, METHOD_V_WITH_MD,
117+
RecordUtils.GRPC_SERVER_STATUS, STATUS_V_WITH_MD);
118+
119+
assertThat(RecordUtils.getTagValues(tags, columns))
120+
.containsExactly(STATUS_V, METHOD_V)
121+
.inOrder();
122+
}
123+
124+
@Test
125+
public void testGetTagValues_MapDeprecatedRpcTag_PreferClientTag() {
126+
List<TagKey> columns = Arrays.asList(RecordUtils.RPC_STATUS, RecordUtils.RPC_METHOD);
127+
Map<TagKey, TagValueWithMetadata> tags =
128+
ImmutableMap.of(
129+
RecordUtils.GRPC_SERVER_METHOD, METHOD_V_WITH_MD,
130+
RecordUtils.GRPC_SERVER_STATUS, STATUS_V_WITH_MD,
131+
RecordUtils.GRPC_CLIENT_METHOD, METHOD_V_2_WITH_MD,
132+
RecordUtils.GRPC_CLIENT_STATUS, STATUS_V_2_WITH_MD);
133+
134+
// When both client and server new tags are present, client values take precedence.
135+
assertThat(RecordUtils.getTagValues(tags, columns))
136+
.containsExactly(STATUS_V_2, METHOD_V_2)
137+
.inOrder();
138+
}
139+
140+
@Test
141+
public void testGetTagValues_WithOldMethodTag() {
142+
List<TagKey> columns = Arrays.asList(RecordUtils.RPC_METHOD);
143+
Map<TagKey, TagValueWithMetadata> tags =
144+
ImmutableMap.of(
145+
RecordUtils.GRPC_SERVER_METHOD, METHOD_V_WITH_MD,
146+
RecordUtils.GRPC_CLIENT_METHOD, METHOD_V_2_WITH_MD,
147+
RecordUtils.RPC_METHOD, METHOD_V_3_WITH_MD);
148+
149+
// When the old "method" tag is set, it always takes precedence.
150+
assertThat(RecordUtils.getTagValues(tags, columns)).containsExactly(METHOD_V_3).inOrder();
151+
}
152+
153+
@Test
154+
public void testGetTagValues_WithNewTags() {
155+
List<TagKey> columns =
156+
Arrays.asList(RecordUtils.GRPC_CLIENT_METHOD, RecordUtils.GRPC_SERVER_METHOD);
157+
Map<TagKey, TagValueWithMetadata> tags =
158+
ImmutableMap.of(
159+
RecordUtils.GRPC_SERVER_METHOD, METHOD_V_WITH_MD,
160+
RecordUtils.GRPC_CLIENT_METHOD, METHOD_V_2_WITH_MD);
161+
162+
assertThat(RecordUtils.getTagValues(tags, columns))
163+
.containsExactly(METHOD_V_2, METHOD_V)
164+
.inOrder();
165+
}
166+
86167
@Test
87168
public void createMutableAggregation() {
88169
BucketBoundaries bucketBoundaries = BucketBoundaries.create(Arrays.asList(-1.0, 0.0, 1.0));

impl_core/src/test/java/io/opencensus/implcore/stats/StatsRecorderImplTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,30 @@ public void recordTwice() {
295295
1e-6);
296296
}
297297

298+
@Test
299+
public void record_MapDeprecatedRpcConstants() {
300+
View view =
301+
View.create(
302+
VIEW_NAME,
303+
"description",
304+
MEASURE_DOUBLE,
305+
Sum.create(),
306+
Arrays.asList(RecordUtils.RPC_METHOD));
307+
308+
viewManager.registerView(view);
309+
MeasureMap statsRecord = statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0);
310+
statsRecord.record(new SimpleTagContext(Tag.create(RecordUtils.GRPC_CLIENT_METHOD, VALUE)));
311+
ViewData viewData = viewManager.getView(VIEW_NAME);
312+
313+
// There should be two entries.
314+
StatsTestUtil.assertAggregationMapEquals(
315+
viewData.getAggregationMap(),
316+
ImmutableMap.of(
317+
Arrays.asList(VALUE),
318+
StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0)),
319+
1e-6);
320+
}
321+
298322
@Test
299323
@SuppressWarnings("deprecation")
300324
public void record_StatsDisabled() {

0 commit comments

Comments
 (0)