Skip to content

Commit

Permalink
Implement ArrayAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
thisthat committed Mar 16, 2020
1 parent cecf833 commit a9ff82c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
18 changes: 9 additions & 9 deletions api/src/main/java/io/opentelemetry/common/AttributeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static AttributeValue arrayAttributeValue(String... stringValues) {
* @return an {@code AttributeValue} with a boolean array value.
* @since 0.3.0
*/
public static AttributeValue arrayAttributeValue(boolean... booleanValues) {
public static AttributeValue arrayAttributeValue(Boolean... booleanValues) {
return AttributeValueBooleanArray.create(booleanValues);
}

Expand All @@ -123,7 +123,7 @@ public static AttributeValue arrayAttributeValue(boolean... booleanValues) {
* @return an {@code AttributeValue} with a long array value.
* @since 0.3.0
*/
public static AttributeValue arrayAttributeValue(long... longValues) {
public static AttributeValue arrayAttributeValue(Long... longValues) {
return AttributeValueLongArray.create(longValues);
}

Expand All @@ -134,7 +134,7 @@ public static AttributeValue arrayAttributeValue(long... longValues) {
* @return an {@code AttributeValue} with a double array value.
* @since 0.3.0
*/
public static AttributeValue arrayAttributeValue(double... doubleValues) {
public static AttributeValue arrayAttributeValue(Double... doubleValues) {
return AttributeValueDoubleArray.create(doubleValues);
}

Expand Down Expand Up @@ -351,13 +351,13 @@ abstract static class AttributeValueBooleanArray extends AttributeValue {

AttributeValueBooleanArray() {}

static AttributeValue create(boolean... booleanValues) {
static AttributeValue create(Boolean... booleanValues) {
if (booleanValues == null) {
return new AutoValue_AttributeValue_AttributeValueBooleanArray(
Collections.<Boolean>emptyList());
}
List<Boolean> values = new ArrayList<>(booleanValues.length);
for (boolean value : booleanValues) {
for (Boolean value : booleanValues) {
values.add(value);
}
return new AutoValue_AttributeValue_AttributeValueBooleanArray(
Expand All @@ -379,12 +379,12 @@ abstract static class AttributeValueLongArray extends AttributeValue {

AttributeValueLongArray() {}

static AttributeValue create(long... longValues) {
static AttributeValue create(Long... longValues) {
if (longValues == null) {
return new AutoValue_AttributeValue_AttributeValueLongArray(Collections.<Long>emptyList());
}
List<Long> values = new ArrayList<>(longValues.length);
for (long value : longValues) {
for (Long value : longValues) {
values.add(value);
}
return new AutoValue_AttributeValue_AttributeValueLongArray(
Expand All @@ -406,13 +406,13 @@ abstract static class AttributeValueDoubleArray extends AttributeValue {

AttributeValueDoubleArray() {}

static AttributeValue create(double... doubleValues) {
static AttributeValue create(Double... doubleValues) {
if (doubleValues == null) {
return new AutoValue_AttributeValue_AttributeValueDoubleArray(
Collections.<Double>emptyList());
}
List<Double> values = new ArrayList<>(doubleValues.length);
for (double value : doubleValues) {
for (Double value : doubleValues) {
values.add(value);
}
return new AutoValue_AttributeValue_AttributeValueDoubleArray(
Expand Down
23 changes: 20 additions & 3 deletions api/src/test/java/io/opentelemetry/common/AttributeValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public void attributeValue_EqualsAndHashCode() {
public void doNotCrashOnNull() {
AttributeValue.stringAttributeValue(null);
AttributeValue.arrayAttributeValue((String[]) null);
AttributeValue.arrayAttributeValue((boolean[]) null);
AttributeValue.arrayAttributeValue((long[]) null);
AttributeValue.arrayAttributeValue((double[]) null);
AttributeValue.arrayAttributeValue((Boolean[]) null);
AttributeValue.arrayAttributeValue((Long[]) null);
AttributeValue.arrayAttributeValue((Double[]) null);
}

@Test
Expand Down Expand Up @@ -98,4 +98,21 @@ public void attributeValue_ToString() {
assertThat(attribute.toString()).contains("1.2345");
assertThat(attribute.toString()).contains("6.789");
}

@Test
public void arrayAttributeValue_nullValuesWithinArray() {
AttributeValue attribute;

attribute = AttributeValue.arrayAttributeValue("string", null, "", "string");
assertThat(attribute.getStringArrayValue().size()).isEqualTo(4);

attribute = AttributeValue.arrayAttributeValue(10L, null, 20L);
assertThat(attribute.getLongArrayValue().size()).isEqualTo(3);

attribute = AttributeValue.arrayAttributeValue(true, null, false);
assertThat(attribute.getBooleanArrayValue().size()).isEqualTo(3);

attribute = AttributeValue.arrayAttributeValue(1.2, null, 3.4);
assertThat(attribute.getDoubleArrayValue().size()).isEqualTo(3);
}
}
6 changes: 3 additions & 3 deletions api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public void doNotCrash() {
span.setAttribute("NullString", (String) null);
span.setAttribute("EmptyString", "");
span.setAttribute("NullArrayString", AttributeValue.arrayAttributeValue((String[]) null));
span.setAttribute("NullArrayBoolean", AttributeValue.arrayAttributeValue((boolean[]) null));
span.setAttribute("NullArrayLong", AttributeValue.arrayAttributeValue((long[]) null));
span.setAttribute("NullArrayDouble", AttributeValue.arrayAttributeValue((double[]) null));
span.setAttribute("NullArrayBoolean", AttributeValue.arrayAttributeValue((Boolean[]) null));
span.setAttribute("NullArrayLong", AttributeValue.arrayAttributeValue((Long[]) null));
span.setAttribute("NullArrayDouble", AttributeValue.arrayAttributeValue((Double[]) null));
span.addEvent("event");
span.addEvent("event", 0);
span.addEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ static AttributeKeyValue toProtoAttribute(String key, AttributeValue attributeVa
.setType(ValueType.DOUBLE)
.setDoubleValue(attributeValue.getDoubleValue())
.build();
case BOOLEAN_ARRAY:
case LONG_ARRAY:
case DOUBLE_ARRAY:
case STRING_ARRAY:
return builder.setType(ValueType.UNRECOGNIZED).build();
}
return builder.setType(ValueType.UNRECOGNIZED).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.EvictingQueue;
import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.common.AttributeValue.Type;
import io.opentelemetry.internal.StringUtils;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ public void setAttribute() {
span.setAttribute(
"ArrayBooleanKey", AttributeValue.arrayAttributeValue(true, false, false, true));
span.setAttribute("NullArrayStringKey", AttributeValue.arrayAttributeValue((String[]) null));
span.setAttribute("NullArrayLongKey", AttributeValue.arrayAttributeValue((long[]) null));
span.setAttribute("NullArrayDoubleKey", AttributeValue.arrayAttributeValue((double[]) null));
span.setAttribute("NullArrayLongKey", AttributeValue.arrayAttributeValue((Long[]) null));
span.setAttribute("NullArrayDoubleKey", AttributeValue.arrayAttributeValue((Double[]) null));
span.setAttribute(
"NullArrayBooleanKey", AttributeValue.arrayAttributeValue((boolean[]) null));
"NullArrayBooleanKey", AttributeValue.arrayAttributeValue((Boolean[]) null));
} finally {
span.end();
}
Expand Down

0 comments on commit a9ff82c

Please sign in to comment.