Skip to content

Commit

Permalink
fix(pg-values): values should be parsable when enhancedDatabaseRepoti…
Browse files Browse the repository at this point in the history
…ng:true (open-telemetry#1453)

* fix(pg-values): values should be parsable when enhancedDatabaseReporting:true

* fix(pg-values): add try catch

* fix(pg-values): lint

* fix(pg-values): fixes

* fix(pg-values): fix

* Update plugins/node/opentelemetry-instrumentation-pg/src/utils.ts

Co-authored-by: Gerhard Stöbich <deb2001-github@yahoo.de>

* fix(pg-values): support buffer, object, null and undefined

* Update plugins/node/opentelemetry-instrumentation-pg/src/utils.ts

Co-authored-by: Gerhard Stöbich <deb2001-github@yahoo.de>

* fix(pg-values): fixes

* fix(pg-values): lint

* fix(pg-values): add test for 'toPostgres'

* fix(pg-values): fix

---------

Co-authored-by: Gerhard Stöbich <deb2001-github@yahoo.de>
  • Loading branch information
haddasbronfman and Flarna authored May 4, 2023
1 parent 69d1f17 commit 49a0389
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
28 changes: 20 additions & 8 deletions plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ import type * as pgTypes from 'pg';
import { PgInstrumentation } from './';
import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';

function arrayStringifyHelper(arr: Array<unknown>): string {
return '[' + arr.toString() + ']';
}

/**
* Helper function to get a low cardinality span name from whatever info we have
* about the query.
Expand Down Expand Up @@ -156,10 +152,26 @@ export function handleConfigQuery(
instrumentationConfig.enhancedDatabaseReporting &&
Array.isArray(queryConfig.values)
) {
span.setAttribute(
AttributeNames.PG_VALUES,
arrayStringifyHelper(queryConfig.values)
);
try {
const convertedValues = queryConfig.values.map(value => {
if (value == null) {
return 'null';
} else if (value instanceof Buffer) {
return value.toString();
} else if (typeof value === 'object') {
if (typeof value.toPostgres === 'function') {
return value.toPostgres();
}
return JSON.stringify(value);
} else {
//string, number
return value.toString();
}
});
span.setAttribute(AttributeNames.PG_VALUES, convertedValues);
} catch (e) {
diag.error('failed to stringify ', queryConfig.values, e);
}
}

// Set plan name attribute, if present
Expand Down
54 changes: 54 additions & 0 deletions plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,60 @@ describe('pg', () => {
});
});

describe('Check configuration enhancedDatabaseReporting:true', () => {
const obj = { type: 'Fiat', model: '500', color: 'white' };
const buf = Buffer.from('abc');
const objWithToPostgres = {
toPostgres: () => {
return 'custom value';
},
};
const query =
'SELECT $1::text as msg1, $2::bytea as bufferParam, $3::integer as numberParam, $4::jsonb as objectParam, $5::text as objToPostgres, $6::text as msg2, $7::text as msg3';
const values = [
'Hello,World',
buf,
6,
obj,
objWithToPostgres,
null,
undefined,
];

const events: TimedEvent[] = [];

const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[AttributeNames.PG_VALUES]: [
'Hello,World',
'abc',
'6',
'{"type":"Fiat","model":"500","color":"white"}',
'custom value',
'null',
'null',
],
};
beforeEach(async () => {
create({
enhancedDatabaseReporting: true,
});
});

it('When enhancedDatabaseReporting:true, values should appear as parsable array of strings', done => {
const span = tracer.startSpan('test span');
context.with(trace.setSpan(context.active(), span), () => {
client.query(query, values, (err, res) => {
assert.strictEqual(err, null);
assert.ok(res);
runCallbackTest(span, attributes, events);
done();
});
});
});
});

describe('when specifying a requestHook configuration', () => {
const dataAttributeName = 'pg_data';
const query = 'SELECT 0::text';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('utils.ts', () => {
const readableSpan = getLatestSpan();

const pgValues = readableSpan.attributes[AttributeNames.PG_VALUES];
assert.strictEqual(pgValues, '[0]');
assert.deepStrictEqual(pgValues, ['0']);
});
});

Expand Down

0 comments on commit 49a0389

Please sign in to comment.