Skip to content

Commit

Permalink
[mysql] fix: ensure span name is a string to avoid [object Object] as…
Browse files Browse the repository at this point in the history
… span name (#208)

* fix: ensure span name is a string to avoid [object Object] as span name

Signed-off-by: Naseem <[email protected]>

* fix: make test read better

Signed-off-by: Naseem <[email protected]>
  • Loading branch information
Naseem authored Oct 9, 2020
1 parent 807346b commit cd24cdf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
4 changes: 2 additions & 2 deletions plugins/node/opentelemetry-plugin-mysql/src/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { BasePlugin, isWrapped } from '@opentelemetry/core';
import { CanonicalCode, Span, SpanKind } from '@opentelemetry/api';
import type * as mysqlTypes from 'mysql';
import * as shimmer from 'shimmer';
import { getConnectionAttributes, getDbStatement } from './utils';
import { getConnectionAttributes, getDbStatement, getSpanName } from './utils';
import { VERSION } from './version';
import { DatabaseAttribute } from '@opentelemetry/semantic-conventions';

Expand Down Expand Up @@ -207,7 +207,7 @@ export class MysqlPlugin extends BasePlugin<typeof mysqlTypes> {
return originalQuery.apply(connection, arguments);
}

const span = thisPlugin._tracer.startSpan(`${query}`, {
const span = thisPlugin._tracer.startSpan(getSpanName(query), {
kind: SpanKind.CLIENT,
attributes: {
...MysqlPlugin.COMMON_ATTRIBUTES,
Expand Down
13 changes: 13 additions & 0 deletions plugins/node/opentelemetry-plugin-mysql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ export function getDbStatement(
: query.sql;
}
}

/**
* The span name SHOULD be set to a low cardinality value
* representing the statement executed on the database.
*
* @returns SQL statement without variable arguments or SQL verb
*/
export function getSpanName(query: string | Query | QueryOptions): string {
if (typeof query === 'object') {
return query.sql;
}
return query.split(' ')[0];
}
46 changes: 39 additions & 7 deletions plugins/node/opentelemetry-plugin-mysql/test/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ describe('[email protected]', () => {
assert.strictEqual(plugin.moduleName, 'mysql');
});

describe('when the query is a string', () => {
it('should name the span accordingly ', done => {
const span = provider.getTracer('default').startSpan('test span');
provider.getTracer('default').withSpan(span, () => {
const sql = 'SELECT 1+1 as solution';
const query = connection.query(sql);

query.on('end', () => {
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans[0].name, 'SELECT');
done();
});
});
});
});

describe('when the query is an object', () => {
it('should name the span accordingly ', done => {
const span = provider.getTracer('default').startSpan('test span');
provider.getTracer('default').withSpan(span, () => {
const sql = 'SELECT 1+? as solution';
const query = connection.query({ sql, values: [1] });

query.on('end', () => {
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans[0].name, sql);
done();
});
});
});
});

describe('#Connection', () => {
it('should intercept connection.query(text: string)', done => {
const span = provider.getTracer('default').startSpan('test span');
Expand Down Expand Up @@ -585,17 +617,17 @@ function assertSpan(
values?: any,
errorMessage?: string
) {
assert.equal(span.attributes[DatabaseAttribute.DB_SYSTEM], 'mysql');
assert.equal(span.attributes[DatabaseAttribute.DB_NAME], database);
assert.equal(span.attributes[GeneralAttribute.NET_PEER_PORT], port);
assert.equal(span.attributes[GeneralAttribute.NET_PEER_HOSTNAME], host);
assert.equal(span.attributes[DatabaseAttribute.DB_USER], user);
assert.strictEqual(span.attributes[DatabaseAttribute.DB_SYSTEM], 'mysql');
assert.strictEqual(span.attributes[DatabaseAttribute.DB_NAME], database);
assert.strictEqual(span.attributes[GeneralAttribute.NET_PEER_PORT], port);
assert.strictEqual(span.attributes[GeneralAttribute.NET_PEER_HOSTNAME], host);
assert.strictEqual(span.attributes[DatabaseAttribute.DB_USER], user);
assert.strictEqual(
span.attributes[DatabaseAttribute.DB_STATEMENT],
mysql.format(sql, values)
);
if (errorMessage) {
assert.equal(span.status.message, errorMessage);
assert.equal(span.status.code, CanonicalCode.UNKNOWN);
assert.strictEqual(span.status.message, errorMessage);
assert.strictEqual(span.status.code, CanonicalCode.UNKNOWN);
}
}

0 comments on commit cd24cdf

Please sign in to comment.