spanstatus.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. /** The status of an Span.
  3. *
  4. * @deprecated Use string literals - if you require type casting, cast to SpanStatusType type
  5. */
  6. exports.SpanStatus = void 0; (function (SpanStatus) {
  7. /** The operation completed successfully. */
  8. const Ok = 'ok'; SpanStatus["Ok"] = Ok;
  9. /** Deadline expired before operation could complete. */
  10. const DeadlineExceeded = 'deadline_exceeded'; SpanStatus["DeadlineExceeded"] = DeadlineExceeded;
  11. /** 401 Unauthorized (actually does mean unauthenticated according to RFC 7235) */
  12. const Unauthenticated = 'unauthenticated'; SpanStatus["Unauthenticated"] = Unauthenticated;
  13. /** 403 Forbidden */
  14. const PermissionDenied = 'permission_denied'; SpanStatus["PermissionDenied"] = PermissionDenied;
  15. /** 404 Not Found. Some requested entity (file or directory) was not found. */
  16. const NotFound = 'not_found'; SpanStatus["NotFound"] = NotFound;
  17. /** 429 Too Many Requests */
  18. const ResourceExhausted = 'resource_exhausted'; SpanStatus["ResourceExhausted"] = ResourceExhausted;
  19. /** Client specified an invalid argument. 4xx. */
  20. const InvalidArgument = 'invalid_argument'; SpanStatus["InvalidArgument"] = InvalidArgument;
  21. /** 501 Not Implemented */
  22. const Unimplemented = 'unimplemented'; SpanStatus["Unimplemented"] = Unimplemented;
  23. /** 503 Service Unavailable */
  24. const Unavailable = 'unavailable'; SpanStatus["Unavailable"] = Unavailable;
  25. /** Other/generic 5xx. */
  26. const InternalError = 'internal_error'; SpanStatus["InternalError"] = InternalError;
  27. /** Unknown. Any non-standard HTTP status code. */
  28. const UnknownError = 'unknown_error'; SpanStatus["UnknownError"] = UnknownError;
  29. /** The operation was cancelled (typically by the user). */
  30. const Cancelled = 'cancelled'; SpanStatus["Cancelled"] = Cancelled;
  31. /** Already exists (409) */
  32. const AlreadyExists = 'already_exists'; SpanStatus["AlreadyExists"] = AlreadyExists;
  33. /** Operation was rejected because the system is not in a state required for the operation's */
  34. const FailedPrecondition = 'failed_precondition'; SpanStatus["FailedPrecondition"] = FailedPrecondition;
  35. /** The operation was aborted, typically due to a concurrency issue. */
  36. const Aborted = 'aborted'; SpanStatus["Aborted"] = Aborted;
  37. /** Operation was attempted past the valid range. */
  38. const OutOfRange = 'out_of_range'; SpanStatus["OutOfRange"] = OutOfRange;
  39. /** Unrecoverable data loss or corruption */
  40. const DataLoss = 'data_loss'; SpanStatus["DataLoss"] = DataLoss;
  41. })(exports.SpanStatus || (exports.SpanStatus = {}));
  42. /**
  43. * Converts a HTTP status code into a {@link SpanStatusType}.
  44. *
  45. * @param httpStatus The HTTP response status code.
  46. * @returns The span status or unknown_error.
  47. */
  48. function getSpanStatusFromHttpCode(httpStatus) {
  49. if (httpStatus < 400 && httpStatus >= 100) {
  50. return 'ok';
  51. }
  52. if (httpStatus >= 400 && httpStatus < 500) {
  53. switch (httpStatus) {
  54. case 401:
  55. return 'unauthenticated';
  56. case 403:
  57. return 'permission_denied';
  58. case 404:
  59. return 'not_found';
  60. case 409:
  61. return 'already_exists';
  62. case 413:
  63. return 'failed_precondition';
  64. case 429:
  65. return 'resource_exhausted';
  66. default:
  67. return 'invalid_argument';
  68. }
  69. }
  70. if (httpStatus >= 500 && httpStatus < 600) {
  71. switch (httpStatus) {
  72. case 501:
  73. return 'unimplemented';
  74. case 503:
  75. return 'unavailable';
  76. case 504:
  77. return 'deadline_exceeded';
  78. default:
  79. return 'internal_error';
  80. }
  81. }
  82. return 'unknown_error';
  83. }
  84. /**
  85. * Converts a HTTP status code into a {@link SpanStatusType}.
  86. *
  87. * @deprecated Use {@link spanStatusFromHttpCode} instead.
  88. * This export will be removed in v8 as the signature contains a typo.
  89. *
  90. * @param httpStatus The HTTP response status code.
  91. * @returns The span status or unknown_error.
  92. */
  93. const spanStatusfromHttpCode = getSpanStatusFromHttpCode;
  94. /**
  95. * Sets the Http status attributes on the current span based on the http code.
  96. * Additionally, the span's status is updated, depending on the http code.
  97. */
  98. function setHttpStatus(span, httpStatus) {
  99. // TODO (v8): Remove these calls
  100. // Relay does not require us to send the status code as a tag
  101. // For now, just because users might expect it to land as a tag we keep sending it.
  102. // Same with data.
  103. // In v8, we replace both, simply with
  104. // span.setAttribute('http.response.status_code', httpStatus);
  105. // eslint-disable-next-line deprecation/deprecation
  106. span.setTag('http.status_code', String(httpStatus));
  107. // eslint-disable-next-line deprecation/deprecation
  108. span.setData('http.response.status_code', httpStatus);
  109. const spanStatus = getSpanStatusFromHttpCode(httpStatus);
  110. if (spanStatus !== 'unknown_error') {
  111. span.setStatus(spanStatus);
  112. }
  113. }
  114. exports.getSpanStatusFromHttpCode = getSpanStatusFromHttpCode;
  115. exports.setHttpStatus = setHttpStatus;
  116. exports.spanStatusfromHttpCode = spanStatusfromHttpCode;
  117. //# sourceMappingURL=spanstatus.js.map