diff --git a/.gitignore b/.gitignore index 6dd7dc74f2f..4a0cebed070 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ bin # Vim .swp + +.fake \ No newline at end of file diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java index 1580b05c465..4e39e410b70 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java @@ -418,22 +418,37 @@ private void addTimedEvent(EventData timedEvent) { @Override public ReadWriteSpan setStatus(StatusCode statusCode, @Nullable String description) { - if (statusCode == null) { - return this; - } - synchronized (lock) { - if (!isModifiableByCurrentThread()) { - logger.log(Level.FINE, "Calling setStatus() on an ended Span."); - return this; - } else if (this.status.getStatusCode() == StatusCode.OK) { - logger.log(Level.FINE, "Calling setStatus() on a Span that is already set to OK."); - return this; + if (statusCode == null) { + return this; // No action if statusCode is null } - this.status = StatusData.create(statusCode, description); - } - return this; + synchronized (lock) { + if (!isModifiableByCurrentThread()) { + logger.log(Level.FINE, "Calling setStatus() on an ended Span."); + return this; // Prevent modification if the span has ended + } + + // Check the current status and enforce priority rules + StatusCode currentStatusCode = this.status.getStatusCode(); + + // Prevent setting a lower priority status + if (currentStatusCode == StatusCode.OK) { + logger.log(Level.FINE, "Calling setStatus() on a Span that is already set to OK."); + return this; // Do not allow lower priority status to override OK + } else if (currentStatusCode == StatusCode.ERROR && statusCode == StatusCode.UNSET) { + logger.log(Level.FINE, "Cannot set status to UNSET when current status is ERROR."); + return this; // Do not allow UNSET to override ERROR + } + + // Set the status, ignoring description if status is not ERROR + if (statusCode == StatusCode.ERROR) { + this.status = StatusData.create(statusCode, description); // Allow description for ERROR + } else { + this.status = StatusData.create(statusCode, null); // Ignore description for non-ERROR statuses + } + } + return this; // Return the current span for method chaining } - + @Override public ReadWriteSpan recordException(Throwable exception) { recordException(exception, Attributes.empty());