resource.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package telemetry // import "go.opentelemetry.io/otel/trace/internal/telemetry"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. )
  11. // Resource information.
  12. type Resource struct {
  13. // Attrs are the set of attributes that describe the resource. Attribute
  14. // keys MUST be unique (it is not allowed to have more than one attribute
  15. // with the same key).
  16. Attrs []Attr `json:"attributes,omitempty"`
  17. // DroppedAttrs is the number of dropped attributes. If the value
  18. // is 0, then no attributes were dropped.
  19. DroppedAttrs uint32 `json:"droppedAttributesCount,omitempty"`
  20. }
  21. // UnmarshalJSON decodes the OTLP formatted JSON contained in data into r.
  22. func (r *Resource) UnmarshalJSON(data []byte) error {
  23. decoder := json.NewDecoder(bytes.NewReader(data))
  24. t, err := decoder.Token()
  25. if err != nil {
  26. return err
  27. }
  28. if t != json.Delim('{') {
  29. return errors.New("invalid Resource type")
  30. }
  31. for decoder.More() {
  32. keyIface, err := decoder.Token()
  33. if err != nil {
  34. if errors.Is(err, io.EOF) {
  35. // Empty.
  36. return nil
  37. }
  38. return err
  39. }
  40. key, ok := keyIface.(string)
  41. if !ok {
  42. return fmt.Errorf("invalid Resource field: %#v", keyIface)
  43. }
  44. switch key {
  45. case "attributes":
  46. err = decoder.Decode(&r.Attrs)
  47. case "droppedAttributesCount", "dropped_attributes_count":
  48. err = decoder.Decode(&r.DroppedAttrs)
  49. default:
  50. // Skip unknown.
  51. }
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. return nil
  57. }