spanstatus.js 4.8 KB

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