opentelemetry-js
opentelemetry-js copied to clipboard
fix(instr-http): respect requireParent flag when INVALID_SPAN_CONTEXT is used
Which problem is this PR solving?
The http instrumentation (and many other instrumentation libraries) create spans with INVALID_SPAN_CONTEXT to create NonRecordingSpans when we enable requireParent and no parent is present: https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/http.ts#L769
That works fine; those spans are not exported. However, when we create child spans, those end up becoming real spans that are exported. E.g. if we have an http server, when a request comes in, we create a span with INVALID_SPAN_CONTEXT, and then if the http request handler makes an outbound request, the instrumentation sees that it has a parent span (even though it is invalid) and creates a real child span, initiating a new trace.
The opentelemetry sdk creates new traces for descendants of INVALID_SPAN_CONTEXT: https://github.com/open-telemetry/opentelemetry-js/blob/ecc88a38d85d5d6adf847306d96e7be77b7df8d6/packages/opentelemetry-sdk-trace-base/src/Tracer.ts#L91
Short description of the changes
This checks whether the parent span is recording, and does not create a real child span if the parent isn't recording.
Type of change
Please delete options that are not relevant.
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update
How Has This Been Tested?
Added unit test to verify fix
Checklist:
- [x] Followed the style guidelines of this project
- [x] Unit tests have been added
- [ ] Documentation has been updated