Skip to content

Commit

Permalink
Adapt RecordEventsReadableSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
thisthat committed Mar 20, 2020
1 parent a7c61ae commit cb82343
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package io.opentelemetry.sdk.trace;

import static io.opentelemetry.common.AttributeValue.Type.STRING;

import com.google.common.base.Preconditions;
import com.google.common.collect.EvictingQueue;
import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.internal.StringUtils;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.resources.Resource;
Expand Down Expand Up @@ -331,35 +332,11 @@ public void setAttribute(String key, boolean value) {
@Override
public void setAttribute(String key, AttributeValue value) {
Preconditions.checkNotNull(key, "key");
Preconditions.checkNotNull(value, "value");
switch (value.getType()) {
case STRING:
if (StringUtils.isNullOrEmpty(value.getStringValue())) {
return;
}
break;
case STRING_ARRAY:
if (value.getStringArrayValue().size() == 0) {
return;
}
break;
case BOOLEAN_ARRAY:
if (value.getBooleanArrayValue().size() == 0) {
return;
}
break;
case LONG_ARRAY:
if (value.getLongArrayValue().size() == 0) {
return;
}
break;
case DOUBLE_ARRAY:
if (value.getDoubleArrayValue().size() == 0) {
return;
}
break;
default:
break;
if (value == null || (value.getType().equals(STRING) && value.getStringValue() == null)) {
synchronized (lock) {
attributes.remove(key);
}
return;
}
synchronized (lock) {
if (hasEnded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void setAttribute() {
span.end();
}
SpanData spanData = span.toSpanData();
assertThat(spanData.getAttributes().size()).isEqualTo(8);
assertThat(spanData.getAttributes().size()).isEqualTo(14);
assertThat(spanData.getAttributes().get("ArrayStringKey").getStringArrayValue().size())
.isEqualTo(4);
assertThat(spanData.getAttributes().get("ArrayLongKey").getLongArrayValue().size())
Expand All @@ -322,6 +322,56 @@ public void setAttribute() {
.isEqualTo(4);
}

@Test
public void setAttribute_emptyArrayAttributeValue() throws Exception {
RecordEventsReadableSpan span = createTestRootSpan();
span.setAttribute("stringArrayAttribute", AttributeValue.arrayAttributeValue(new String[0]));
span.setAttribute("boolArrayAttribute", AttributeValue.arrayAttributeValue(new Boolean[0]));
span.setAttribute("longArrayAttribute", AttributeValue.arrayAttributeValue(new Long[0]));
span.setAttribute("doubleArrayAttribute", AttributeValue.arrayAttributeValue(new Double[0]));
assertThat(span.toSpanData().getAttributes().size()).isEqualTo(4);
}

@Test
public void setAttribute_nullStringValue() throws Exception {
RecordEventsReadableSpan span = createTestRootSpan();
span.setAttribute("emptyString", "");
span.setAttribute("nullString", (String) null);
span.setAttribute("nullStringAttributeValue", AttributeValue.stringAttributeValue(null));
span.setAttribute("emptyStringAttributeValue", AttributeValue.stringAttributeValue(""));
assertThat(span.toSpanData().getAttributes().size()).isEqualTo(2);
span.setAttribute("emptyString", (String) null);
span.setAttribute("emptyStringAttributeValue", (String) null);
assertThat(span.toSpanData().getAttributes()).isEmpty();
}

@Test
public void setAttribute_nullAttributeValue() throws Exception {
RecordEventsReadableSpan span = createTestRootSpan();
span.setAttribute("emptyString", "");
span.setAttribute("nullString", (AttributeValue) null);
span.setAttribute("nullStringAttributeValue", AttributeValue.stringAttributeValue(null));
span.setAttribute("emptyStringAttributeValue", AttributeValue.stringAttributeValue(""));
span.setAttribute("longAttribute", 0L);
span.setAttribute("boolAttribute", false);
span.setAttribute("doubleAttribute", 0.12345f);
span.setAttribute("stringArrayAttribute", AttributeValue.arrayAttributeValue("", null));
span.setAttribute("boolArrayAttribute", AttributeValue.arrayAttributeValue(true, null));
span.setAttribute("longArrayAttribute", AttributeValue.arrayAttributeValue(12345L, null));
span.setAttribute("doubleArrayAttribute", AttributeValue.arrayAttributeValue(1.2345, null));
assertThat(span.toSpanData().getAttributes().size()).isEqualTo(9);
span.setAttribute("emptyString", (AttributeValue) null);
span.setAttribute("emptyStringAttributeValue", (AttributeValue) null);
span.setAttribute("longAttribute", (AttributeValue) null);
span.setAttribute("boolAttribute", (AttributeValue) null);
span.setAttribute("doubleAttribute", (AttributeValue) null);
span.setAttribute("stringArrayAttribute", (AttributeValue) null);
span.setAttribute("boolArrayAttribute", (AttributeValue) null);
span.setAttribute("longArrayAttribute", (AttributeValue) null);
span.setAttribute("doubleArrayAttribute", (AttributeValue) null);
assertThat(span.toSpanData().getAttributes()).isEmpty();
}

@Test
public void addEvent() {
RecordEventsReadableSpan span = createTestRootSpan();
Expand Down

0 comments on commit cb82343

Please sign in to comment.