Skip to content

fix(knex): connection attrs missing if connectionString used #1932

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

Conversation

edosrecki
Copy link

Which problem is this PR solving?

  • Connection related attributes (hostname, port, user, database) are missing if users use connectionString property when initializing knex (instead of specifying individual fields):
const pg = require('knex')({
  client: 'pg',
  connection: {
    connectionString: config.DATABASE_URL,
  },
});
  • Span name is also affected, for example here database = undefined:
Screenshot 2024-02-10 at 14 28 28

Short description of the changes

  • If connectionString is provided in knex options, extract attributes from it (hostname, port, user, database).
  • If both connectionString and individual fields are used, connectionString takes precedence:

Screenshot 2024-02-10 at 15 43 55

Copy link

codecov bot commented Feb 12, 2024

Codecov Report

Attention: Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.

Project coverage is 91.02%. Comparing base (7895306) to head (d6e6090).
Report is 514 commits behind head on main.

Files with missing lines Patch % Lines
...emetry-instrumentation-knex/src/instrumentation.ts 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1932      +/-   ##
==========================================
- Coverage   91.02%   91.02%   -0.01%     
==========================================
  Files         146      146              
  Lines        7478     7488      +10     
  Branches     1497     1502       +5     
==========================================
+ Hits         6807     6816       +9     
- Misses        671      672       +1     
Files with missing lines Coverage Δ
...de/opentelemetry-instrumentation-knex/src/utils.ts 91.48% <100.00%> (+1.24%) ⬆️
...emetry-instrumentation-knex/src/instrumentation.ts 97.67% <87.50%> (-1.11%) ⬇️
🚀 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.

const url = new URL(connectionString);
return {
host: url.hostname,
port: parseInt(url.port || '5432', 10),

Choose a reason for hiding this comment

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

I'm interested in this pull request getting merged, so I thought I'd add some notes.

It looks like connectionString is for both PostgreSQL and RedShift. It looks like the default port for RedShift is 5439.

I think you can check the URL protocol which should be postgres: or redshift: and set it to 5432/5439?

> (new URL("postgres://user:password@myhost/mydb")).protocol
'postgres:'

Copy link
Author

@edosrecki edosrecki Mar 27, 2024

Choose a reason for hiding this comment

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

Thanks for the comment. Yes I can do that. However we cannot then cover the case of CockroachDB which uses postgres protocol, but has a different default port of 26257.

I will see how knex handles redshift connection string when port is not provided and try to match that functionality. In the end that is the right thing to do.

@pichlermarc pichlermarc added pkg-status:unmaintained This package is unmaintained. Only bugfixes may be acceped until a new owner has been found. bug Something isn't working labels May 21, 2025
@pichlermarc
Copy link
Member

It looks like there's another in-flight PR that will also affect the same part of the code, we'll wait for this to land first (#2671).

@pichlermarc pichlermarc added the priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect label May 21, 2025
@@ -131,13 +131,19 @@ export class KnexInstrumentation extends InstrumentationBase<any> {
return function wrapQuery(original: () => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
const connection = utils.parseConnectionString(
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could pass the entire config.connection here and return the resulting values instead of having undefined checks in the attribute assignment further down?

@trentm
Copy link
Contributor

trentm commented May 21, 2025

There is also #2286
I haven't looked closely yet, but it is patching for a similar thing.
@deejay1 and others that use Knex: preferences for other PR?

@deejay1
Copy link
Contributor

deejay1 commented May 22, 2025

I think this settles having a custom function returning the connection attributes, when we now have three different ways to configure them, what do you think @edosrecki ?

I think it would be nice rolling this into this PR, I'd wait for @edosrecki to respond, if I'll make both changes it into a separate PR.

@maryliag
Copy link
Contributor

maryliag commented Jun 18, 2025

Hi @edosrecki , thank you for your contribution. Recently the Database Semantic Conventions was marked as stabled, and with this a few changes came, for example, there are no longer attributes for Connection String, DB User, DB name, etc. And new attributes were added, such as DB namespace (that should include the removed attributes).
Here you can find a details doc about migrating to the stable convention.

Because of those changes, we don't want to add or update attributes that are no longer stable and should be removed instead. For this reason, I'll close this PR.

You can still open a new PR focusing only on fixes related to the stable convention, and you can also help out with the migration (which would be very much appreciate it and I would be happy to review), but I can see that a lot has already been done here, so you can check if any attributes are missing. Here is an example of how I'm doing this migration for the PG package.

Feel free to reach out to me if you have any questions!

@maryliag maryliag closed this Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:instrumentation-knex pkg-status:unmaintained This package is unmaintained. Only bugfixes may be acceped until a new owner has been found. priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants