codes.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package codes // import "go.opentelemetry.io/otel/codes"
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "strconv"
  19. )
  20. const (
  21. // Unset is the default status code.
  22. Unset Code = 0
  23. // Error indicates the operation contains an error.
  24. //
  25. // NOTE: The error code in OTLP is 2.
  26. // The value of this enum is only relevant to the internals
  27. // of the Go SDK.
  28. Error Code = 1
  29. // Ok indicates operation has been validated by an Application developers
  30. // or Operator to have completed successfully, or contain no error.
  31. //
  32. // NOTE: The Ok code in OTLP is 1.
  33. // The value of this enum is only relevant to the internals
  34. // of the Go SDK.
  35. Ok Code = 2
  36. maxCode = 3
  37. )
  38. // Code is an 32-bit representation of a status state.
  39. type Code uint32
  40. var codeToStr = map[Code]string{
  41. Unset: "Unset",
  42. Error: "Error",
  43. Ok: "Ok",
  44. }
  45. var strToCode = map[string]Code{
  46. `"Unset"`: Unset,
  47. `"Error"`: Error,
  48. `"Ok"`: Ok,
  49. }
  50. // String returns the Code as a string.
  51. func (c Code) String() string {
  52. return codeToStr[c]
  53. }
  54. // UnmarshalJSON unmarshals b into the Code.
  55. //
  56. // This is based on the functionality in the gRPC codes package:
  57. // https://github.com/grpc/grpc-go/blob/bb64fee312b46ebee26be43364a7a966033521b1/codes/codes.go#L218-L244
  58. func (c *Code) UnmarshalJSON(b []byte) error {
  59. // From json.Unmarshaler: By convention, to approximate the behavior of
  60. // Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as
  61. // a no-op.
  62. if string(b) == "null" {
  63. return nil
  64. }
  65. if c == nil {
  66. return fmt.Errorf("nil receiver passed to UnmarshalJSON")
  67. }
  68. var x interface{}
  69. if err := json.Unmarshal(b, &x); err != nil {
  70. return err
  71. }
  72. switch x.(type) {
  73. case string:
  74. if jc, ok := strToCode[string(b)]; ok {
  75. *c = jc
  76. return nil
  77. }
  78. return fmt.Errorf("invalid code: %q", string(b))
  79. case float64:
  80. if ci, err := strconv.ParseUint(string(b), 10, 32); err == nil {
  81. if ci >= maxCode {
  82. return fmt.Errorf("invalid code: %q", ci)
  83. }
  84. *c = Code(ci)
  85. return nil
  86. }
  87. return fmt.Errorf("invalid code: %q", string(b))
  88. default:
  89. return fmt.Errorf("invalid code: %q", string(b))
  90. }
  91. }
  92. // MarshalJSON returns c as the JSON encoding of c.
  93. func (c *Code) MarshalJSON() ([]byte, error) {
  94. if c == nil {
  95. return []byte("null"), nil
  96. }
  97. str, ok := codeToStr[*c]
  98. if !ok {
  99. return nil, fmt.Errorf("invalid code: %d", *c)
  100. }
  101. return []byte(fmt.Sprintf("%q", str)), nil
  102. }