This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtracing.ts
More file actions
82 lines (79 loc) · 2.78 KB
/
tracing.ts
File metadata and controls
82 lines (79 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Span } from 'opentracing'
import { Observable } from 'rxjs'
/**
* Traces a synchronous function by passing it a new child span.
* The span is finished when the function returns.
* If the function throws an Error, it is logged and the `error` tag set.
*
* @param operationName The operation name for the new span
* @param childOf The parent span
* @param operation The function to call
*/
export function traceSync<T>(operationName: string, childOf: Span, operation: (span: Span) => T): T {
const span = childOf.tracer().startSpan(operationName, { childOf })
try {
return operation(span)
} catch (err) {
span.setTag('error', true)
span.log({ event: 'error', 'error.object': err, stack: err.stack, message: err.message })
throw err
} finally {
span.finish()
}
}
/**
* Traces a Promise-returning (or async) function by passing it a new child span.
* The span is finished when the Promise is resolved.
* If the Promise is rejected, the Error is logged and the `error` tag set.
*
* @param operationName The operation name for the new span
* @param childOf The parent span
* @param operation The function to call
*/
export async function tracePromise<T>(
operationName: string,
childOf: Span,
operation: (span: Span) => Promise<T>
): Promise<T> {
const span = childOf.tracer().startSpan(operationName, { childOf })
try {
return await operation(span)
} catch (err) {
span.setTag('error', true)
span.log({ event: 'error', 'error.object': err, stack: err.stack, message: err.message })
throw err
} finally {
span.finish()
}
}
/**
* Traces an Observable-returning function by passing it a new child span.
* The span is finished when the Observable completes or is unsubscribed from.
* If the Observable errors or the function throws an Error, the Error is logged and the `error` tag set.
*
* @param operationName The operation name for the new span
* @param childOf The parent span
* @param operation The function to call
*/
export function traceObservable<T>(
operationName: string,
childOf: Span,
operation: (span: Span) => Observable<T>
): Observable<T> {
const span = childOf.tracer().startSpan(operationName, { childOf })
try {
return operation(span)
.do(undefined as any, err => {
span.setTag('error', true)
span.log({ event: 'error', 'error.object': err, stack: err.stack, message: err.message })
})
.finally(() => {
span.finish()
})
} catch (err) {
span.setTag('error', true)
span.log({ event: 'error', 'error.object': err, stack: err.stack, message: err.message })
span.finish()
return Observable.throw(err)
}
}