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

Prevent NPE when deleting OpenTracing baggage #7637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Xyaren
Copy link
Contributor

@Xyaren Xyaren commented Sep 18, 2024

What Does This Do

Prevent NPE when deleting baggage value by setBaggageItem("key", null)

Motivation

NPE when trying to remove a baggage item.

java.lang.NullPointerException: null
	at java.base/java.util.concurrent.ConcurrentHashMap.putVal(Unknown Source)
	at java.base/java.util.concurrent.ConcurrentHashMap.put(Unknown Source)
	at datadog.trace.agent.core.DDSpanContext.setBaggageItem(DDSpanContext.java:651)
	at datadog.trace.agent.core.DDSpan.setBaggageItem(DDSpan.java:514)
	at datadog.trace.agent.core.DDSpan.setBaggageItem(DDSpan.java:50)
	at datadog.trace.instrumentation.opentracing32.OTSpan.setBaggageItem(OTSpan.java:122)
	at datadog.trace.instrumentation.opentracing32.OTSpan.setBaggageItem(OTSpan.java:17)
	at com.mycomp.tracing.OpenTracingBusinessContextStore.clearBaggageItem(OpenTracingBusinessContextStore.java:55)

Additional Notes

Contributor Checklist

@Xyaren Xyaren changed the title Prevent NPE when deleting baggage Prevent NPE when deleting baggage comp:core Sep 18, 2024
@Xyaren Xyaren changed the title Prevent NPE when deleting baggage comp:core Prevent NPE when deleting baggage Sep 18, 2024
@Xyaren Xyaren marked this pull request as ready for review September 18, 2024 09:25
@Xyaren Xyaren requested a review from a team as a code owner September 18, 2024 09:25
@smola smola added comp: opentracing OpenTracing comp: core Tracer core labels Sep 18, 2024
if (baggageItems == EMPTY_BAGGAGE) {
baggageItems = new ConcurrentHashMap<>(4);
if (value == null) {
baggageItems.remove(key);

Choose a reason for hiding this comment

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

Should we protect the call when the baggageItems has not yet been initialized (in that case this will throw an unsupported operation exception)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this would be unexpected. An outside actor is not aware if something is initialized or not.

Choose a reason for hiding this comment

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

Yep, you can then just check if the baggageItems != EMPTY_BAGGAGE before calling remove

Many thanks for the contribution 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

EMPTY_BAGGAGE is just an empty map, so .remove is a noop.
Would that make a difference ?

Copy link
Contributor

@PerfectSlayer PerfectSlayer left a comment

Choose a reason for hiding this comment

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

Dump question but I could not find from OpenTracing spec nor code base that null value must be supported?
Not the null should throw a NPE, it should not but I am not sure that setting a null value should remove the key-value pair... 🤔
Can you help me where you found this behavior?

Moreover, this change would required additional test for core and OpenTracing instrumentation (we can discuss this point later once we find out how the API should behave).

EDIT: I find the the OpenTracing shim from OpenTelemetry (so the implementation of OpenTracing from the OpenTelemetry standard) does skip null keys and values:
https://github.com/open-telemetry/opentelemetry-java/blob/0132d5d98b21cd4cbc2a0354a4d5bcc2867a2969/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java#L167-L178

So I would not be in favor of changing the baggage behavior, only fixing the NPE.

@PerfectSlayer PerfectSlayer changed the title Prevent NPE when deleting baggage Prevent NPE when deleting OpenTracing baggage Sep 18, 2024
@Xyaren
Copy link
Contributor Author

Xyaren commented Sep 18, 2024

Right, fortunately with opentelemetry you have the option to get the Baggage using getBaggage() turn it into a builder, update it and store it again.

https://github.com/open-telemetry/opentelemetry-java/blob/main/api/all/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java#L75

open-telemetry/opentelemetry-java#2553

I feel this might be an unintended restriction due to the ConcurrentHashMap.

What would my alternative be to remove a baggage Item ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp: core Tracer core comp: opentracing OpenTracing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants