Skip to content

Commit

Permalink
Do not allow null description in the StatusData (#2896)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Feb 22, 2021
1 parent 013643c commit 9e1c76b
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion api/all/src/main/java/io/opentelemetry/api/trace/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ default Span addEvent(String name, Attributes attributes, Instant timestamp) {
* @return this.
*/
default Span setStatus(StatusCode statusCode) {
return setStatus(statusCode, null);
return setStatus(statusCode, "");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static Span toJaeger(SpanData span) {
.setVStr(span.getKind().name().toLowerCase(Locale.ROOT)));
}

if (span.getStatus().getDescription() != null) {
if (!span.getStatus().getDescription().isEmpty()) {
tags.add(
new Tag(KEY_SPAN_STATUS_MESSAGE, TagType.STRING)
.setVStr(span.getStatus().getDescription()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static Model.Span toJaeger(SpanData span) {
.build());
}

if (span.getStatus().getDescription() != null) {
if (!span.getStatus().getDescription().isEmpty()) {
target.addTags(
Model.KeyValue.newBuilder()
.setKey(KEY_SPAN_STATUS_MESSAGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static Status toStatusProto(StatusData status) {
@SuppressWarnings("deprecation") // setDeprecatedCode is deprecated.
Status.Builder builder =
Status.newBuilder().setCode(protoStatusCode).setDeprecatedCode(deprecatedStatusCode);
if (status.getDescription() != null) {
if (!status.getDescription().isEmpty()) {
builder.setMessage(status.getDescription());
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ void generateSpan_WithRpcTimeoutErrorStatus_WithTimeoutErrorDescription() {
}

@Test
void generateSpan_WithRpcErrorStatus_WithNullErrorDescription() {
void generateSpan_WithRpcErrorStatus_WithEmptyErrorDescription() {
Attributes attributeMap = Attributes.of(SemanticAttributes.RPC_SERVICE, "my service name");

SpanData data =
buildStandardSpan()
.setStatus(StatusData.create(StatusCode.ERROR, null))
.setStatus(StatusData.create(StatusCode.ERROR, ""))
.setAttributes(attributeMap)
.build();

Expand All @@ -302,7 +302,7 @@ void generateSpan_WithRpcUnsetStatus() {

SpanData data =
buildStandardSpan()
.setStatus(StatusData.create(StatusCode.UNSET, null))
.setStatus(StatusData.create(StatusCode.UNSET, ""))
.setAttributes(attributeMap)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import java.util.EnumMap;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
Expand All @@ -25,13 +24,13 @@ abstract class ImmutableStatusData implements StatusData {
* The operation has been validated by an Application developers or Operator to have completed
* successfully.
*/
static final StatusData OK = createInternal(StatusCode.OK, null);
static final StatusData OK = createInternal(StatusCode.OK, "");

/** The default status. */
static final StatusData UNSET = createInternal(StatusCode.UNSET, null);
static final StatusData UNSET = createInternal(StatusCode.UNSET, "");

/** The operation contains an error. */
static final StatusData ERROR = createInternal(StatusCode.ERROR, null);
static final StatusData ERROR = createInternal(StatusCode.ERROR, "");

// Visible for test
static final EnumMap<StatusCode, StatusData> codeToStatus = new EnumMap<>(StatusCode.class);
Expand All @@ -47,7 +46,7 @@ abstract class ImmutableStatusData implements StatusData {
for (StatusCode code : codes) {
StatusData status = codeToStatus.get(code);
if (status == null) {
codeToStatus.put(code, createInternal(code, null));
codeToStatus.put(code, createInternal(code, ""));
}
}
}
Expand All @@ -58,14 +57,14 @@ abstract class ImmutableStatusData implements StatusData {
* @param description the new description of the {@code Status}.
* @return The newly created {@code Status} with the given description.
*/
static StatusData create(StatusCode statusCode, @Nullable String description) {
if (description == null) {
static StatusData create(StatusCode statusCode, String description) {
if (description == null || description.isEmpty()) {
return codeToStatus.get(statusCode);
}
return createInternal(statusCode, description);
}

private static StatusData createInternal(StatusCode statusCode, @Nullable String description) {
private static StatusData createInternal(StatusCode statusCode, String description) {
return new AutoValue_ImmutableStatusData(statusCode, description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
Expand Down Expand Up @@ -39,7 +38,7 @@ static StatusData error() {
* Returns a {@link StatusData} with the given {@code code} and {@code description}. If {@code
* description} is {@code null}, the returned {@link StatusData} does not have a description.
*/
static StatusData create(StatusCode code, @Nullable String description) {
static StatusData create(StatusCode code, String description) {
return ImmutableStatusData.create(code, description);
}

Expand All @@ -51,6 +50,5 @@ static StatusData create(StatusCode code, @Nullable String description) {
*
* @return the description of this {@code Status}.
*/
@Nullable
String getDescription();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ void defaultConstants() {
StatusCode[] codes = StatusCode.values();
assertThat(codes).hasSize(3);
assertThat(StatusData.unset().getStatusCode()).isEqualTo(StatusCode.UNSET);
assertThat(StatusData.unset().getDescription()).isNull();
assertThat(StatusData.unset().getDescription()).isEmpty();
assertThat(StatusData.ok().getStatusCode()).isEqualTo(StatusCode.OK);
assertThat(StatusData.ok().getDescription()).isNull();
assertThat(StatusData.ok().getDescription()).isEmpty();
assertThat(StatusData.error().getStatusCode()).isEqualTo(StatusCode.ERROR);
assertThat(StatusData.error().getDescription()).isNull();
assertThat(StatusData.error().getDescription()).isEmpty();
}

@Test
Expand All @@ -31,7 +31,7 @@ void generateCodeToStatus() {
for (StatusCode code : codes) {
assertThat(ImmutableStatusData.codeToStatus.get(code)).isNotNull();
assertThat(ImmutableStatusData.codeToStatus.get(code).getStatusCode()).isEqualTo(code);
assertThat(ImmutableStatusData.codeToStatus.get(code).getDescription()).isNull();
assertThat(ImmutableStatusData.codeToStatus.get(code).getDescription()).isEmpty();
}
}
}

0 comments on commit 9e1c76b

Please sign in to comment.