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

Commit 825fdd5

Browse files
committed
Skip null tag value for exporter (#843)
(cherry picked from commit 7adb4f2)
1 parent 34b28e8 commit 825fdd5

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ static Metric createMetric(View view, List<? extends TagValue> tagValues) {
177177
for (int i = 0; i < tagValues.size(); i++) {
178178
TagKey key = columns.get(i);
179179
TagValue value = tagValues.get(i);
180+
if (value == null) {
181+
continue;
182+
}
180183
stringTagMap.put(key.getName(), value.asString());
181184
}
182185
builder.putAllLabels(stringTagMap);

exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtilsTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class StackdriverExportUtilsTest {
7070
@Rule public final ExpectedException thrown = ExpectedException.none();
7171

7272
private static final TagKey KEY = TagKey.create("KEY");
73+
private static final TagKey KEY_2 = TagKey.create("KEY2");
74+
private static final TagKey KEY_3 = TagKey.create("KEY3");
7375
private static final TagValue VALUE_1 = TagValue.create("VALUE1");
7476
private static final TagValue VALUE_2 = TagValue.create("VALUE2");
7577
private static final String MEASURE_UNIT = "us";
@@ -153,6 +155,41 @@ public void createMetric() {
153155
.build());
154156
}
155157

158+
@Test
159+
public void createMetric_skipNullTagValue() {
160+
View view =
161+
View.create(
162+
Name.create(VIEW_NAME),
163+
VIEW_DESCRIPTION,
164+
MEASURE_DOUBLE,
165+
DISTRIBUTION,
166+
Arrays.asList(KEY, KEY_2, KEY_3),
167+
CUMULATIVE);
168+
assertThat(StackdriverExportUtils.createMetric(view, Arrays.asList(VALUE_1, null, VALUE_2)))
169+
.isEqualTo(
170+
Metric.newBuilder()
171+
.setType("custom.googleapis.com/opencensus/" + VIEW_NAME)
172+
.putLabels("KEY", "VALUE1")
173+
.putLabels("KEY3", "VALUE2")
174+
.build());
175+
}
176+
177+
@Test
178+
public void createMetric_throwWhenTagKeysAndValuesHaveDifferentSize() {
179+
View view =
180+
View.create(
181+
Name.create(VIEW_NAME),
182+
VIEW_DESCRIPTION,
183+
MEASURE_DOUBLE,
184+
DISTRIBUTION,
185+
Arrays.asList(KEY, KEY_2, KEY_3),
186+
CUMULATIVE);
187+
List<TagValue> tagValues = Arrays.asList(VALUE_1, null);
188+
thrown.expect(IllegalArgumentException.class);
189+
thrown.expectMessage("TagKeys and TagValues don't have same size.");
190+
StackdriverExportUtils.createMetric(view, tagValues);
191+
}
192+
156193
@Test
157194
public void convertTimestamp() {
158195
Timestamp censusTimestamp1 = Timestamp.create(100, 3000);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static List<TagValue> getTagValues(
139139
for (int i = 0; i < columns.size(); ++i) {
140140
TagKey tagKey = columns.get(i);
141141
if (!tags.containsKey(tagKey)) {
142-
// replace not found key values by “unknown/not set”.
142+
// replace not found key values by null.
143143
tagValues.add(UNKNOWN_TAG_VALUE);
144144
} else {
145145
tagValues.add(tags.get(tagKey));

0 commit comments

Comments
 (0)