Skip to content

Commit b3f2ad9

Browse files
update documentation slightly
Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 654dc3f commit b3f2ad9

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

docs/telemetry.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ not the path it took to get there. We sometimes need this stack trace to debug,
150150
Common example: _"I have a function, `thisFailsSometimes()` that is called in multiple places. The function sometimes fails, I know from telemetry, but I do not know if it is failing when it is a specific caller. If I knew the call stack/trace that it took to call my function that would help me debug."_
151151

152152
```typescript
153-
function outerA() {
153+
function runsSuccessfully() {
154154
thisFailsSometimes(1) // this succeeds
155155
}
156156

157-
function outerB() {
157+
function thisThrows() {
158158
thisFailsSometimes(0) // this fails
159159
}
160160

161-
function thisFailsSometimes(num: number) {
161+
function failsDependingOnInput(num: number) {
162162
return telemetry.my_Metric.run(() => {
163163
if (number === 0) {
164164
throw Error('Cannot be 0')
@@ -170,7 +170,37 @@ function thisFailsSometimes(num: number) {
170170

171171
### Solution
172172

173-
Add a value to `function` in the options of a `run()`. This will result in a stack of functions identifiers that were previously called
173+
On class methods use the `@withTelemetryContext()` decorator add context to the execution. Depending on the args set, it provides features like emitting the result, or adding it's context to errors.
174+
175+
> NOTE: Decorators are currently only supported for methods and not functions
176+
177+
```typescript
178+
class MyClass {
179+
@withTelemetryContext({ name: 'runsSuccessfully', class: 'MyClass' })
180+
public runsSuccessfully() {
181+
failsDependingOnInput(1)
182+
}
183+
184+
@withTelemetryContext({ name: 'thisThrows', class: 'MyClass', errorCtx: true })
185+
public thisThrows() {
186+
failsDependingOnInput(0)
187+
}
188+
189+
@withTelemetryContext({ name: 'failsDependingOnInput' class: 'MyClass', emit: true, errorCtx: true})
190+
private failsDependingOnInput(num: number) {
191+
if (number === 0) {
192+
throw Error('Cannot be 0')
193+
}
194+
...
195+
}
196+
}
197+
198+
// Results in a metric: { source: 'MyClass#thisThrows,failsDependingOnInput', result: 'Failed' }
199+
// Results in an error that has context about the methods that lead up to it.
200+
new MyClass().thisThrows()
201+
```
202+
203+
Separately if you must use a function, add a value to `function` in the options of a `run()`. This will result in a stack of functions identifiers that were previously called
174204
before `failsDependingOnInput()` was run. You can then retrieve the stack in the `run()` of your final metric using `getFunctionStack()`.
175205

176206
```typescript
@@ -197,40 +227,9 @@ function failsDependingOnInput(num: number) {
197227
thisThrows()
198228
```
199229

200-
Additionally the `@withTelemetryContext()` decorator can be added to methods to do the same as above, but with a cleaner syntax.
201-
202-
```typescript
203-
class MyClass {
204-
@withTelemetryContext({ name: 'runsSuccessfully', class: 'MyClass' })
205-
public runsSuccessfully() {
206-
failsDependingOnInput(1)
207-
}
208-
209-
@withTelemetryContext({ name: 'thisThrows', class: 'MyClass' })
210-
public thisThrows() {
211-
failsDependingOnInput(0)
212-
}
213-
214-
private failsDependingOnInput(num: number) {
215-
return telemetry.my_Metric.run(() => {
216-
telemetry.record({ theCallStack: asStringifiedStack(telemetry.getFunctionStack())})
217-
if (number === 0) {
218-
throw Error('Cannot be 0')
219-
}
220-
...
221-
}, { functionId: { name: 'failsDependingOnInput' }})
222-
}
223-
224-
}
225-
226-
227-
// Results in a metric: { theCallStack: 'MyClass#thisThrows,failsDependingOnInput', result: 'Failed' }
228-
new MyClass().thisThrows()
229-
```
230-
231230
### Important Notes
232231

233-
- Using `@withTelemetryContext` + setting `errorCtx` will wrap errors with the functionId properties
232+
- You can avoid redundancy when repeating fields like `class` in `@withTelemetryContext` by using `withTelemetryContextFactory()`. It builds a new decorator with pre-defined values that you choose.
234233

235234
- If a nested function does not use a `run()` then it will not be part of the call stack.
236235

0 commit comments

Comments
 (0)