Skip to content

Commit 5072efc

Browse files
refactor(telemetry): @withTelemetryContext can emit with source
Now the @withTelemetryContext decorator can have a method emit when called and will also add the `source` field automatically. By default it does not emit. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent f4e550a commit 5072efc

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

packages/core/src/shared/telemetry/util.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { isValidationExemptMetric } from './exemptMetrics'
2626
import { isAmazonQ, isCloud9, isSageMaker } from '../../shared/extensionUtilities'
2727
import { randomUUID } from '../crypto'
2828
import { ClassToInterfaceType } from '../utilities/tsUtils'
29-
import { FunctionEntry } from './spans'
29+
import { asStringifiedStack, FunctionEntry } from './spans'
3030
import { telemetry } from './telemetry'
3131
import { v5 as uuidV5 } from 'uuid'
3232
import { ToolkitError } from '../errors'
@@ -383,9 +383,15 @@ export function withTelemetryContext(opts: TelemetryContextArgs) {
383383
) {
384384
function decoratedMethod(this: This, ...args: Args): Return {
385385
return telemetry.function_call.run(
386-
() => {
386+
(span) => {
387387
try {
388-
// DEVELOPERS: Set a breakpoint here and step in to it to debug the original function
388+
span.record({
389+
functionName: opts.name,
390+
className: opts.class,
391+
source: asStringifiedStack(telemetry.getFunctionStack()),
392+
})
393+
394+
// DEVELOPERS: Set a breakpoint here and step in and debug the original function
389395
const result = originalMethod.call(this, ...args)
390396

391397
if (result instanceof Promise) {
@@ -405,7 +411,7 @@ export function withTelemetryContext(opts: TelemetryContextArgs) {
405411
}
406412
},
407413
{
408-
emit: false,
414+
emit: opts.emit !== undefined ? opts.emit : false,
409415
functionId: opts,
410416
}
411417
)

packages/core/src/shared/telemetry/vscodeTelemetry.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,10 @@
11161116
{
11171117
"type": "className",
11181118
"required": false
1119+
},
1120+
{
1121+
"type": "source",
1122+
"required": false
11191123
}
11201124
],
11211125
"passive": true

packages/core/src/test/shared/telemetry/spans.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,41 @@ describe('TelemetryTracer', function () {
579579
})
580580

581581
describe('withTelemetryContext', async function () {
582+
class TestEmit {
583+
@withTelemetryContext({ name: 'doesNotEmit', class: 'TestEmit' })
584+
doesNotEmit() {
585+
return
586+
}
587+
588+
@withTelemetryContext({ name: 'doesEmit', class: 'TestEmit', emit: false })
589+
doesEmit() {
590+
return this.doesEmitNested()
591+
}
592+
593+
@withTelemetryContext({ name: 'doesEmitNested', class: 'TestEmit', emit: true })
594+
doesEmitNested() {
595+
return
596+
}
597+
}
598+
599+
it(`does NOT emit an event if not explicitly set`, function () {
600+
const inst = new TestEmit()
601+
inst.doesNotEmit()
602+
assertTelemetry('function_call', [])
603+
})
604+
605+
it(`does emit an event on its own when explicitly set`, function () {
606+
const inst = new TestEmit()
607+
inst.doesEmit()
608+
assertTelemetry('function_call', [
609+
{
610+
functionName: 'doesEmitNested',
611+
className: 'TestEmit',
612+
source: 'TestEmit#doesEmit,doesEmitNested',
613+
},
614+
])
615+
})
616+
582617
class TestThrows {
583618
@withTelemetryContext({ name: 'throwsError', class: 'TestThrows', errorCtx: true })
584619
throwsError() {

0 commit comments

Comments
 (0)