-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathspanstatus.ts
67 lines (60 loc) · 2.23 KB
/
spanstatus.ts
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
import type { Span, SpanStatus } from '../types-hoist';
export const SPAN_STATUS_UNSET = 0;
export const SPAN_STATUS_OK = 1;
export const SPAN_STATUS_ERROR = 2;
/**
* Converts a HTTP status code into a sentry status with a message.
*
* @param httpStatus The HTTP response status code.
* @returns The span status or unknown_error.
*/
// https://develop.sentry.dev/sdk/event-payloads/span/
export function getSpanStatusFromHttpCode(httpStatus: number): SpanStatus {
if (httpStatus < 400 && httpStatus >= 100) {
return { code: SPAN_STATUS_OK };
}
if (httpStatus >= 400 && httpStatus < 500) {
switch (httpStatus) {
case 401:
return { code: SPAN_STATUS_ERROR, message: 'unauthenticated' };
case 403:
return { code: SPAN_STATUS_ERROR, message: 'permission_denied' };
case 404:
return { code: SPAN_STATUS_ERROR, message: 'not_found' };
case 409:
return { code: SPAN_STATUS_ERROR, message: 'already_exists' };
case 413:
return { code: SPAN_STATUS_ERROR, message: 'failed_precondition' };
case 429:
return { code: SPAN_STATUS_ERROR, message: 'resource_exhausted' };
case 499:
return { code: SPAN_STATUS_ERROR, message: 'cancelled' };
default:
return { code: SPAN_STATUS_ERROR, message: 'invalid_argument' };
}
}
if (httpStatus >= 500 && httpStatus < 600) {
switch (httpStatus) {
case 501:
return { code: SPAN_STATUS_ERROR, message: 'unimplemented' };
case 503:
return { code: SPAN_STATUS_ERROR, message: 'unavailable' };
case 504:
return { code: SPAN_STATUS_ERROR, message: 'deadline_exceeded' };
default:
return { code: SPAN_STATUS_ERROR, message: 'internal_error' };
}
}
return { code: SPAN_STATUS_ERROR, message: 'unknown_error' };
}
/**
* Sets the Http status attributes on the current span based on the http code.
* Additionally, the span's status is updated, depending on the http code.
*/
export function setHttpStatus(span: Span, httpStatus: number): void {
span.setAttribute('http.response.status_code', httpStatus);
const spanStatus = getSpanStatusFromHttpCode(httpStatus);
if (spanStatus.message !== 'unknown_error') {
span.setStatus(spanStatus);
}
}