|
| 1 | +import type { Span } from '@sentry/types'; |
| 2 | + |
1 | 3 | /** The status of an Span. |
2 | 4 | * |
3 | 5 | * @deprecated Use string literals - if you require type casting, cast to SpanStatusType type |
@@ -38,3 +40,108 @@ export enum SpanStatus { |
38 | 40 | /** Unrecoverable data loss or corruption */ |
39 | 41 | DataLoss = 'data_loss', |
40 | 42 | } |
| 43 | + |
| 44 | +export type SpanStatusType = |
| 45 | + /** The operation completed successfully. */ |
| 46 | + | 'ok' |
| 47 | + /** Deadline expired before operation could complete. */ |
| 48 | + | 'deadline_exceeded' |
| 49 | + /** 401 Unauthorized (actually does mean unauthenticated according to RFC 7235) */ |
| 50 | + | 'unauthenticated' |
| 51 | + /** 403 Forbidden */ |
| 52 | + | 'permission_denied' |
| 53 | + /** 404 Not Found. Some requested entity (file or directory) was not found. */ |
| 54 | + | 'not_found' |
| 55 | + /** 429 Too Many Requests */ |
| 56 | + | 'resource_exhausted' |
| 57 | + /** Client specified an invalid argument. 4xx. */ |
| 58 | + | 'invalid_argument' |
| 59 | + /** 501 Not Implemented */ |
| 60 | + | 'unimplemented' |
| 61 | + /** 503 Service Unavailable */ |
| 62 | + | 'unavailable' |
| 63 | + /** Other/generic 5xx. */ |
| 64 | + | 'internal_error' |
| 65 | + /** Unknown. Any non-standard HTTP status code. */ |
| 66 | + | 'unknown_error' |
| 67 | + /** The operation was cancelled (typically by the user). */ |
| 68 | + | 'cancelled' |
| 69 | + /** Already exists (409) */ |
| 70 | + | 'already_exists' |
| 71 | + /** Operation was rejected because the system is not in a state required for the operation's */ |
| 72 | + | 'failed_precondition' |
| 73 | + /** The operation was aborted, typically due to a concurrency issue. */ |
| 74 | + | 'aborted' |
| 75 | + /** Operation was attempted past the valid range. */ |
| 76 | + | 'out_of_range' |
| 77 | + /** Unrecoverable data loss or corruption */ |
| 78 | + | 'data_loss'; |
| 79 | + |
| 80 | +/** |
| 81 | + * Converts a HTTP status code into a {@link SpanStatusType}. |
| 82 | + * |
| 83 | + * @param httpStatus The HTTP response status code. |
| 84 | + * @returns The span status or unknown_error. |
| 85 | + */ |
| 86 | +export function spanStatusfromHttpCode(httpStatus: number): SpanStatusType { |
| 87 | + if (httpStatus < 400 && httpStatus >= 100) { |
| 88 | + return 'ok'; |
| 89 | + } |
| 90 | + |
| 91 | + if (httpStatus >= 400 && httpStatus < 500) { |
| 92 | + switch (httpStatus) { |
| 93 | + case 401: |
| 94 | + return 'unauthenticated'; |
| 95 | + case 403: |
| 96 | + return 'permission_denied'; |
| 97 | + case 404: |
| 98 | + return 'not_found'; |
| 99 | + case 409: |
| 100 | + return 'already_exists'; |
| 101 | + case 413: |
| 102 | + return 'failed_precondition'; |
| 103 | + case 429: |
| 104 | + return 'resource_exhausted'; |
| 105 | + default: |
| 106 | + return 'invalid_argument'; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (httpStatus >= 500 && httpStatus < 600) { |
| 111 | + switch (httpStatus) { |
| 112 | + case 501: |
| 113 | + return 'unimplemented'; |
| 114 | + case 503: |
| 115 | + return 'unavailable'; |
| 116 | + case 504: |
| 117 | + return 'deadline_exceeded'; |
| 118 | + default: |
| 119 | + return 'internal_error'; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return 'unknown_error'; |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Sets the Http status attributes on the current span based on the http code. |
| 128 | + * Additionally, the span's status is updated, depending on the http code. |
| 129 | + */ |
| 130 | +export function setHttpStatus(span: Span, httpStatus: number): void { |
| 131 | + // TODO (v8): Remove these calls |
| 132 | + // Relay does not require us to send the status code as a tag |
| 133 | + // For now, just because users might expect it to land as a tag we keep sending it. |
| 134 | + // Same with data. |
| 135 | + // In v8, we replace both, simply with |
| 136 | + // span.setAttribute('http.response.status_code', httpStatus); |
| 137 | + |
| 138 | + // eslint-disable-next-line deprecation/deprecation |
| 139 | + span.setTag('http.status_code', String(httpStatus)); |
| 140 | + // eslint-disable-next-line deprecation/deprecation |
| 141 | + span.setData('http.response.status_code', httpStatus); |
| 142 | + |
| 143 | + const spanStatus = spanStatusfromHttpCode(httpStatus); |
| 144 | + if (spanStatus !== 'unknown_error') { |
| 145 | + span.setStatus(spanStatus); |
| 146 | + } |
| 147 | +} |
0 commit comments