-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add post-query custom attributes hook for spans
- Loading branch information
Showing
5 changed files
with
156 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -546,5 +546,111 @@ describe('[email protected]', () => { | |
client.query('SELECT NOW()').then(queryHandler); | ||
}); | ||
}); | ||
|
||
it('should call postQueryHook with query text if set', async () => { | ||
instrumentation.disable(); | ||
let called = false; | ||
const query = 'SELECT NOW()'; | ||
const config: PgInstrumentationConfig = { | ||
postQueryHook: ctx => { | ||
called = true; | ||
assert.strictEqual(ctx.query, query); | ||
assert.strictEqual(ctx.params, undefined); | ||
}, | ||
}; | ||
instrumentation.setConfig(config); | ||
instrumentation.enable(); | ||
|
||
const attributes = { | ||
...DEFAULT_ATTRIBUTES, | ||
[SemanticAttributes.DB_STATEMENT]: query, | ||
}; | ||
const events: TimedEvent[] = []; | ||
const span = tracer.startSpan('test span'); | ||
await context.with(trace.setSpan(context.active(), span), async () => { | ||
try { | ||
const resPromise = await client.query(query); | ||
assert.ok(resPromise); | ||
runCallbackTest(span, attributes, events); | ||
} catch (e) { | ||
assert.ok(false, e.message); | ||
} | ||
}); | ||
assert.strictEqual(called, true); | ||
}); | ||
it('should call postQueryHook with query text and params if set', async () => { | ||
instrumentation.disable(); | ||
let called = false; | ||
const values = ['0']; | ||
const query = 'SELECT $1::text'; | ||
const config: PgInstrumentationConfig = { | ||
postQueryHook: ctx => { | ||
called = true; | ||
assert.strictEqual(ctx.query, query); | ||
assert.strictEqual(ctx.params, values); | ||
}, | ||
}; | ||
instrumentation.setConfig(config); | ||
instrumentation.enable(); | ||
|
||
const attributes = { | ||
...DEFAULT_ATTRIBUTES, | ||
[SemanticAttributes.DB_STATEMENT]: query, | ||
}; | ||
const events: TimedEvent[] = []; | ||
const span = tracer.startSpan('test span'); | ||
await context.with(trace.setSpan(context.active(), span), async () => { | ||
const resPromise = await client.query(query, values); | ||
try { | ||
assert.ok(resPromise); | ||
runCallbackTest(span, attributes, events); | ||
} catch (e) { | ||
assert.ok(false, e.message); | ||
} | ||
}); | ||
assert.strictEqual(called, true); | ||
}); | ||
it('should call postQueryHook with query config if set', async () => { | ||
instrumentation.disable(); | ||
const name = 'fetch-text'; | ||
const query = 'SELECT $1::text'; | ||
const values = ['0']; | ||
let called = false; | ||
const config: PgInstrumentationConfig = { | ||
postQueryHook: ctx => { | ||
called = true; | ||
if (!ctx.config) { | ||
assert.ok(false, 'ctx.config was undefined'); | ||
} | ||
assert.strictEqual(ctx.config.text, query); | ||
assert.strictEqual(ctx.config.values, values); | ||
}, | ||
}; | ||
instrumentation.setConfig(config); | ||
instrumentation.enable(); | ||
|
||
const attributes = { | ||
...DEFAULT_ATTRIBUTES, | ||
[AttributeNames.PG_PLAN]: name, | ||
[SemanticAttributes.DB_STATEMENT]: query, | ||
}; | ||
const events: TimedEvent[] = []; | ||
const span = tracer.startSpan('test span'); | ||
|
||
await context.with(trace.setSpan(context.active(), span), async () => { | ||
try { | ||
const resPromise = await client.query({ | ||
name: name, | ||
text: query, | ||
values: values, | ||
}); | ||
assert.strictEqual(resPromise.command, 'SELECT'); | ||
runCallbackTest(span, attributes, events); | ||
} catch (e) { | ||
assert.ok(false, e.message); | ||
} | ||
}); | ||
assert.strictEqual(called, true); | ||
}); | ||
}); | ||
}); |