Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setStatus method conforms to the specified behavior regarding status … #6808

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7f9ef5f
setStatus method conforms to the specified behavior regarding status …
Victorsesan Oct 21, 2024
ef46f8f
fix
Victorsesan Oct 23, 2024
9590742
Merge branch 'open-telemetry:main' into main
Victorsesan Oct 23, 2024
a9fb915
Fixed and adjusted the following.
Victorsesan Oct 24, 2024
500d261
Merge branch 'main' of github.com:Victorsesan/opentelemetry-java
Victorsesan Oct 24, 2024
aa731c7
fix comments
Victorsesan Oct 25, 2024
bed6535
fix comment
Victorsesan Oct 25, 2024
c21608c
Created a test in the sdkspantest to see if codecov will have line 4…
Victorsesan Oct 26, 2024
ec33889
test 2
Victorsesan Oct 26, 2024
ab67793
.
Victorsesan Oct 26, 2024
189c416
.
Victorsesan Oct 26, 2024
7991abf
.
Victorsesan Oct 26, 2024
448ece8
ensured span's status does not change after it has ended in the nothi…
Victorsesan Nov 3, 2024
5a62252
Merge branch 'open-telemetry:main' into main
Victorsesan Nov 3, 2024
2fdd372
#
Victorsesan Nov 4, 2024
d452c3e
#
Victorsesan Nov 4, 2024
f04bb8d
Update sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java
Victorsesan Nov 8, 2024
5aa430e
Update sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java
Victorsesan Nov 8, 2024
db6a054
Update sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java
Victorsesan Nov 8, 2024
b1776cf
Update sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java
Victorsesan Nov 8, 2024
d9e11e8
Update sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java
Victorsesan Nov 8, 2024
8fc9659
Added a new test to help ensure that my setStatus method behaves cor…
Victorsesan Nov 8, 2024
cb9283e
Merge branch 'main' of github.com:Victorsesan/opentelemetry-java
Victorsesan Nov 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
}
29 changes: 25 additions & 4 deletions sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,38 @@ private void addTimedEvent(EventData timedEvent) {
@Override
public ReadWriteSpan setStatus(StatusCode statusCode, @Nullable String description) {
if (statusCode == null) {
return this;
return this; // No action if statusCode is null
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this?

synchronized (lock) {
if (!isModifiableByCurrentThread()) {
logger.log(Level.FINE, "Calling setStatus() on an ended Span.");
return this;
} else if (this.status.getStatusCode() == StatusCode.OK) {
}
<<<<<<< HEAD

=======

>>>>>>> d9e11e8e4aebfe44bc79436b8e7ee42344e4a5aa
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;
return this; // Do not allow lower priority status to override OK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove the logging that was here before?

} 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);
} else {
if (currentStatusCode != statusCode) {
this.status =
StatusData.create(statusCode, null);
}
}
this.status = StatusData.create(statusCode, description);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,25 @@ void setUp() {
@Test
void nothingChangedAfterEnd() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change you made doesn't only apply after the span has ended, does it? This test is testing the case of things not changing after span ending.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take a look at the test called setStatusCannotOverrideStatusOK. Write a new test that looks much like that one that tests the case(s) that you've introduced.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added a new test for that ,though not sure if that is the required approach, still open for corrections. Also pardon some of my mistakes or modification , I'm still learning and improving on myself, and i always try to use different approach whenever needed , which if i knew how to perfectly run a test on my changes to know if they are okay it would have been much easier, so i have to check and get corrections here. Thanks for your time , let me know if the test i wrote is that which you need to be added.

SdkSpan span = createTestSpan(SpanKind.INTERNAL);

// Set an initial status before ending the span
span.setStatus(StatusCode.OK, "Initial status");
assertThat(span.toSpanData().getStatus().getStatusCode()).isEqualTo(StatusCode.OK);

// End the span
span.end();
// Check that adding trace events or update fields after Span#end() does not throw any thrown

// Store the current status for verification
StatusData currentStatus = span.toSpanData().getStatus();

// Check that adding trace events or update fields after Span#end() does not throw any exception
// and are ignored.
spanDoWork(span, StatusCode.ERROR, "CANCELLED");

// Verify that the status has not changed after the span has ended
SpanData spanData = span.toSpanData();
assertThat(spanData.getStatus()).isEqualTo(currentStatus);

verifySpanData(
spanData,
Attributes.empty(),
Expand Down Expand Up @@ -1432,6 +1446,29 @@ private void verifySpanData(
spanDataAttributes.forEach((key, value) -> assertThat(attributes.get(key)).isEqualTo(value));
}


@Test
void setStatusCannotOverrideStatusError() {
SdkSpan testSpan = createTestRootSpan();

// Set an initial status to ERROR
testSpan.setStatus(StatusCode.ERROR, "Initial error status");
assertThat(testSpan.toSpanData().getStatus().getStatusCode()).isEqualTo(StatusCode.ERROR);

// Attempt to set status to OK (should not change)
testSpan.setStatus(StatusCode.OK);
assertThat(testSpan.toSpanData().getStatus().getStatusCode()).isEqualTo(StatusCode.ERROR);

// Attempt to set status to UNSET (should not change)
testSpan.setStatus(StatusCode.UNSET);
assertThat(testSpan.toSpanData().getStatus().getStatusCode()).isEqualTo(StatusCode.ERROR);

// Attempt to set status to ERROR again (should remain ERROR)
testSpan.setStatus(StatusCode.ERROR, "Another error status");
assertThat(testSpan.toSpanData().getStatus().getStatusCode()).isEqualTo(StatusCode.ERROR);
}


@Test
void testAsSpanData() {
String name = "GreatSpan";
Expand Down
Loading