Skip to content

Commit b2cbc3f

Browse files
withTelemetryContextFactory
Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 5072efc commit b2cbc3f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,11 @@ export function withTelemetryContext(opts: TelemetryContextArgs) {
426426
})
427427
}
428428
}
429+
430+
/**
431+
* Returns a custom {@link withTelemetryContext} decorator, but with some values predefined
432+
* to deduplicate boilerplate for a class that will use this decorator multiple times.
433+
*/
434+
export function withTelemetryContextFactory(predefinedOpts: Omit<TelemetryContextArgs, 'name'>) {
435+
return (opts: { name: string }) => withTelemetryContext({ ...opts, ...predefinedOpts })
436+
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { assertTelemetry, getMetrics, installFakeClock } from '../../testUtil'
1111
import { selectFrom } from '../../../shared/utilities/tsUtils'
1212
import { getAwsServiceError } from '../errors.test'
1313
import { sleep } from '../../../shared'
14-
import { withTelemetryContext } from '../../../shared/telemetry/util'
14+
import { withTelemetryContext, withTelemetryContextFactory } from '../../../shared/telemetry/util'
1515
import { SinonSandbox } from 'sinon'
1616
import sinon from 'sinon'
1717
import * as crypto from '../../../shared/crypto'
@@ -685,6 +685,27 @@ describe('TelemetryTracer', function () {
685685
(e) => e === arbitraryError
686686
)
687687
})
688+
689+
it('Uses a custom telemetry context', function () {
690+
const customWithTelemetryContext = withTelemetryContextFactory({ class: 'TestCustomContext', emit: true })
691+
692+
class TestCustomContext {
693+
@customWithTelemetryContext({ name: 'testMethod' })
694+
testMethod() {
695+
return
696+
}
697+
}
698+
699+
const inst = new TestCustomContext()
700+
inst.testMethod()
701+
assertTelemetry('function_call', [
702+
{
703+
functionName: 'testMethod',
704+
className: 'TestCustomContext',
705+
source: 'TestCustomContext#testMethod',
706+
},
707+
])
708+
})
688709
})
689710

690711
describe('withTraceId()', function () {

0 commit comments

Comments
 (0)