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

fix(pg): avoid disjoint spans from pg instrumentation #1122

Merged
merged 17 commits into from
Sep 13, 2022

Conversation

seemk
Copy link
Contributor

@seemk seemk commented Aug 22, 2022

Which problem is this PR solving?

  • pg.Client.connect was uninstrumented. With net and dns instrumentations in use it meant that when client.connect() was called with no active context, it produced 2 disjoint traces, one for net and one for dns. This now produces a new span called pg.connect.
  • pg.Pool.connect had the same problem where it did not activate the newly created context, causing disjoint spans for net and dns.

Downstream spans from pg are now linked to pg spans correctly.

Misc: When instrumenting pg.Client.connect the connection string contained jdbc prefix (which I removed). How did this end up in a Node.js instrumentation? 🤔

Checklist

  • Ran npm run test-all-versions for the edited package(s) on the latest commit if applicable.

@seemk seemk requested a review from a team August 22, 2022 17:51
@github-actions github-actions bot requested a review from rauno56 August 22, 2022 17:51
@codecov
Copy link

codecov bot commented Aug 22, 2022

Codecov Report

Merging #1122 (23e6025) into main (457be50) will decrease coverage by 0.40%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1122      +/-   ##
==========================================
- Coverage   96.07%   95.67%   -0.41%     
==========================================
  Files          14       18       +4     
  Lines         892     1156     +264     
  Branches      191      233      +42     
==========================================
+ Hits          857     1106     +249     
- Misses         35       50      +15     
Impacted Files Coverage Δ
...elemetry-instrumentation-pg/src/instrumentation.ts 89.74% <100.00%> (ø)
...node/opentelemetry-instrumentation-pg/src/utils.ts 97.82% <100.00%> (ø)
...try-instrumentation-pg/src/enums/AttributeNames.ts 100.00% <0.00%> (ø)
...tapackages/auto-instrumentations-node/src/utils.ts 98.00% <0.00%> (ø)

Copy link
Member

@blumamir blumamir left a comment

Choose a reason for hiding this comment

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

LGTM. left few suggestions

@@ -50,16 +50,20 @@ function getCommandFromText(text?: string): string {
return words[0].length > 0 ? words[0] : 'unknown';
}

export function getJDBCString(params: PgClientConnectionParams) {
Copy link
Member

Choose a reason for hiding this comment

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

What is the motivation to replace PgClientConnectionParams?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this change and reused the same type albeit with changing the fields to possibly be undefined. In client.connect case the client itself is passed to getConnectionString and the client has all of these fields as optional. Perhaps the PgClientConnectionParams fields should've been optional since the beginning.

this: pgTypes.Client,
err: Error
) {
if (err) {
Copy link
Member

Choose a reason for hiding this comment

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

maybe also record exception?

span.recordExcpetion(err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Member

Choose a reason for hiding this comment

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

Did you forget to push?

@@ -236,3 +240,22 @@ export function patchCallbackPGPool(
cb.call(this, err, res, done);
};
}

export function patchConnectErrorCallback(
Copy link
Member

Choose a reason for hiding this comment

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

Is this callback only for errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed it to patchClientConnectCallback

Comment on lines 161 to 188
if (!(connectResult instanceof Promise)) {
return connectResult;
}

const connectResultPromise = connectResult as Promise<unknown>;
return context.bind(
context.active(),
connectResultPromise
.then(
result =>
new Promise(resolve => {
span.end();
resolve(result);
})
)
.catch((error: Error) => {
return new Promise((_, reject) => {
span.setStatus({
code: SpanStatusCode.ERROR,
message: error.message,
});
span.end();
reject(error);
});
})
);
};
};
Copy link
Member

Choose a reason for hiding this comment

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

This block is a duplicate of the code in _getPoolConnectPatch.
Could be nice to extract into a function and invoke from both places

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored this into a single function named handleConnectResult

@@ -236,3 +240,22 @@ export function patchCallbackPGPool(
cb.call(this, err, res, done);
};
}

export function patchConnectErrorCallback(
Copy link
Member

Choose a reason for hiding this comment

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

There is already a function patchCallbackPGPool which does almost the same thing. The only difference is the arguments list to the callback which we don't use.
Can we reuse one function for both cases?
Maybe write it with arguments array parameter instead of listing the different arguments for each case

Copy link
Contributor Author

@seemk seemk Aug 30, 2022

Choose a reason for hiding this comment

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

I am not sure about this, think there is some value in keeping descriptive function names (i.e. the created callback). I did rename the function though

@seemk
Copy link
Contributor Author

seemk commented Aug 30, 2022

Seem to be unrelated unit tests failing atm

Copy link
Member

@rauno56 rauno56 left a comment

Choose a reason for hiding this comment

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

Awesome! Thanks for taking this on!

@rauno56 rauno56 merged commit 82b8a84 into open-telemetry:main Sep 13, 2022
@dyladan dyladan mentioned this pull request Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants