Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(mysql): bind get connection callback to active context
Signed-off-by: Simon Stone <simonstone1@gmail.com>
  • Loading branch information
sstone1 authored and Simon Stone committed Aug 10, 2021
commit 25b93b1585ef7d1540814f379fb4d1296a4f60a0
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* limitations under the License.
*/

import { diag, Span, SpanKind, SpanStatusCode } from '@opentelemetry/api';
import {
context,
diag,
Span,
SpanKind,
SpanStatusCode,
} from '@opentelemetry/api';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
Expand Down Expand Up @@ -203,6 +209,7 @@ export class MySQLInstrumentation extends InstrumentationBase<

private _getConnectionCallbackPatchFn(cb: Function, format: formatType) {
const thisPlugin = this;
const activeContext = context.active();
return function () {
if (arguments[1]) {
// this is the callback passed into a query
Expand All @@ -216,7 +223,9 @@ export class MySQLInstrumentation extends InstrumentationBase<
}
}
if (typeof cb === 'function') {
cb(...arguments);
context.with(activeContext, () => {
cb(...arguments);
});
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,30 @@ describe('mysql@2.x', () => {
});
});
});

it('should propagate active context to callback', done => {
const parentSpan = provider.getTracer('default').startSpan('test span');
context.with(trace.setSpan(context.active(), parentSpan), () => {
pool.getConnection((err, connection) => {
assert.ifError(err);
assert.ok(connection);
const sql = 'SELECT ? as solution';
connection.query(sql, 1, (err, res) => {
assert.ifError(err);
assert.ok(res);
assert.strictEqual(res[0].solution, 1);
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1);
const querySpan = spans[0];
assert.strictEqual(
querySpan.parentSpanId,
parentSpan.spanContext().spanId
);
done();
});
});
Copy link
Member

Choose a reason for hiding this comment

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

This is a very verbose way to check that the context is correctly set in the callback.
Alternative and more direct option is to extract trace.getSpan(context.active()) in the callback and compare it to parentSpan

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blumamir thanks, I've changed the test.

});
});
});

describe('#PoolCluster', () => {
Expand Down Expand Up @@ -600,6 +624,30 @@ describe('mysql@2.x', () => {
}
);
});

it('should propagate active context to callback', done => {
const parentSpan = provider.getTracer('default').startSpan('test span');
context.with(trace.setSpan(context.active(), parentSpan), () => {
poolCluster.getConnection((err, connection) => {
assert.ifError(err);
assert.ok(connection);
const sql = 'SELECT ? as solution';
connection.query(sql, 1, (err, res) => {
assert.ifError(err);
assert.ok(res);
assert.strictEqual(res[0].solution, 1);
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1);
const querySpan = spans[0];
assert.strictEqual(
querySpan.parentSpanId,
parentSpan.spanContext().spanId
);
done();
});
});
});
});
});
});

Expand Down