Skip to content

Commit

Permalink
fix(instrumentation-pg): prevent net.peer.port from being NaN (#1982)
Browse files Browse the repository at this point in the history
* fix(instrumentation-pg): prevent net.peer.port from being NaN

* fix: lint

* fix: test name

* Update plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
  • Loading branch information
pichlermarc authored Mar 8, 2024
1 parent 0574168 commit 3b2090b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
13 changes: 12 additions & 1 deletion plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,25 @@ export function getConnectionString(params: PgParsedConnectionParams) {
return `postgresql://${host}:${port}/${database}`;
}

function getPort(port: number | undefined): number | undefined {
// Port may be NaN as parseInt() is used on the value, passing null will result in NaN being parsed.
// https://github.com/brianc/node-postgres/blob/2a8efbee09a284be12748ed3962bc9b816965e36/packages/pg/lib/connection-parameters.js#L66
if (Number.isInteger(port)) {
return port;
}

// Unable to find the default used in pg code, so falling back to 'undefined'.
return undefined;
}

export function getSemanticAttributesFromConnection(
params: PgParsedConnectionParams
) {
return {
[SemanticAttributes.DB_NAME]: params.database, // required
[SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required
[SemanticAttributes.NET_PEER_NAME]: params.host, // required
[SemanticAttributes.NET_PEER_PORT]: params.port,
[SemanticAttributes.NET_PEER_PORT]: getPort(params.port),
[SemanticAttributes.DB_USER]: params.user,
};
}
Expand Down
45 changes: 45 additions & 0 deletions plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { PgInstrumentationConfig } from '../src';
import { AttributeNames } from '../src/enums/AttributeNames';
import { PgClientExtended } from '../src/internal-types';
import * as utils from '../src/utils';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

const memoryExporter = new InMemorySpanExporter();

Expand Down Expand Up @@ -193,4 +194,48 @@ describe('utils.ts', () => {
assert.deepStrictEqual(pgValues, ['0']);
});
});

describe('.getSemanticAttributesFromConnection()', () => {
it('should set port attribute to undefined when port is not an integer', () => {
assert.strictEqual(
utils.getSemanticAttributesFromConnection({
port: Infinity,
})[SemanticAttributes.NET_PEER_PORT],
undefined
);
assert.strictEqual(
utils.getSemanticAttributesFromConnection({
port: -Infinity,
})[SemanticAttributes.NET_PEER_PORT],
undefined
);
assert.strictEqual(
utils.getSemanticAttributesFromConnection({
port: NaN,
})[SemanticAttributes.NET_PEER_PORT],
undefined
);
assert.strictEqual(
utils.getSemanticAttributesFromConnection({
port: 1.234,
})[SemanticAttributes.NET_PEER_PORT],
undefined
);
});

it('should set port attribute when port is an integer', () => {
assert.strictEqual(
utils.getSemanticAttributesFromConnection({
port: 1234,
})[SemanticAttributes.NET_PEER_PORT],
1234
);
assert.strictEqual(
utils.getSemanticAttributesFromConnection({
port: Number.MAX_VALUE,
})[SemanticAttributes.NET_PEER_PORT],
Number.MAX_VALUE
);
});
});
});

0 comments on commit 3b2090b

Please sign in to comment.