Skip to content

fix(redis): use new tracer after setTracerProvider #2865

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

Merged
merged 4 commits into from
Jun 4, 2025

Conversation

blumamir
Copy link
Member

@blumamir blumamir commented May 29, 2025

Which problem is this PR solving?

This PR fixes a bug in "redis" instrumentation, where the tracer used to create spans is brought into closure scope once at instrumentation "create client" patch time and then used regardless of any new tracer provider being set via the setTracerProvider on the instrumentation instance.

  private _getPatchCreateClient() {
    const tracer = this.tracer;
    return function createClient(original: Function) {
      return getTracedCreateClient(tracer, original);
    };
  }

The function taking care of doing the patch and creating the span is now found in utils.ts which I think less makes sense logically and also requires passing tracer, config (and we should also use diag and meter) to a util function which is the cause for the current bug. This pattern is uncommon in the repo since having the function on instrumentation class guarantee simple access to providers instead of passing them as arguments.

I am working on adding support for redis v5, and wanted to first consolidate v4 into this package, and run into this bug will hooking up the multi-version support.

Short description of the changes

I moved the function from utils.ts to a member function on the instrumentation class. the patched function now calls instrumentation.tracer which is a getter function that returns _tracer which is updated on the instrumentation every time setTracerProvider is called.

This guarantee that startSpan is called on a tracer from the most up-to-date tracerProvider that was set via instrumentation public api function.

Added a new test to assert this change (failing before the refactor and passing after). The PR only moved the function from utils.ts and instrumentation.ts and changed the tracer being used. the function content itself is not modified


const dbStatementSerializer =
config?.dbStatementSerializer || defaultDbStatementSerializer;
const span = instrumentation.tracer.startSpan(
Copy link
Member Author

Choose a reason for hiding this comment

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

reviewer note: this is the fix. here we use instrumentation.tracer which gets updated in after setTracerProvider so we always use the latest one

},
err => {
if (err) {
instrumentation._diag.error(
Copy link
Member Author

Choose a reason for hiding this comment

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

reviewer note: bug fixed here as well. using instrumentation._diag scoped logger instead of diag directly which aligns with best practices and other instrumentations

@@ -49,17 +28,14 @@ const endSpan = (span: Span, err?: Error | null) => {
span.end();
};

export const getTracedCreateClient = (tracer: Tracer, original: Function) => {
export const getTracedCreateClient = (original: Function) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

reviewer note: removing unused parameter (which is also locked to it's initial value and does not update on setTracerProvider

Copy link

codecov bot commented May 29, 2025

Codecov Report

Attention: Patch coverage is 93.02326% with 3 lines in your changes missing coverage. Please review.

Project coverage is 89.69%. Comparing base (2317e2f) to head (ac4987f).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...metry-instrumentation-redis/src/instrumentation.ts 92.30% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2865      +/-   ##
==========================================
- Coverage   89.69%   89.69%   -0.01%     
==========================================
  Files         186      186              
  Lines        9048     9041       -7     
  Branches     1855     1855              
==========================================
- Hits         8116     8109       -7     
  Misses        932      932              
Files with missing lines Coverage Δ
...e/opentelemetry-instrumentation-redis/src/utils.ts 94.73% <100.00%> (+1.63%) ⬆️
...metry-instrumentation-redis/src/instrumentation.ts 91.78% <92.30%> (-0.91%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@david-luna
Copy link
Contributor

@blumamir,

thanks for the changes. I think we should merge this one before any work related to #2867

Also, as a component owner, your feedback on the issue would be valuable :)

@pichlermarc pichlermarc merged commit 5861dfa into open-telemetry:main Jun 4, 2025
23 checks passed
@dyladan dyladan mentioned this pull request Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants