scope.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package telemetry
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. )
  11. // Scope is the identifying values of the instrumentation scope.
  12. type Scope struct {
  13. Name string `json:"name,omitempty"`
  14. Version string `json:"version,omitempty"`
  15. Attrs []Attr `json:"attributes,omitempty"`
  16. DroppedAttrs uint32 `json:"droppedAttributesCount,omitempty"`
  17. }
  18. // UnmarshalJSON decodes the OTLP formatted JSON contained in data into r.
  19. func (s *Scope) UnmarshalJSON(data []byte) error {
  20. decoder := json.NewDecoder(bytes.NewReader(data))
  21. t, err := decoder.Token()
  22. if err != nil {
  23. return err
  24. }
  25. if t != json.Delim('{') {
  26. return errors.New("invalid Scope type")
  27. }
  28. for decoder.More() {
  29. keyIface, err := decoder.Token()
  30. if err != nil {
  31. if errors.Is(err, io.EOF) {
  32. // Empty.
  33. return nil
  34. }
  35. return err
  36. }
  37. key, ok := keyIface.(string)
  38. if !ok {
  39. return fmt.Errorf("invalid Scope field: %#v", keyIface)
  40. }
  41. switch key {
  42. case "name":
  43. err = decoder.Decode(&s.Name)
  44. case "version":
  45. err = decoder.Decode(&s.Version)
  46. case "attributes":
  47. err = decoder.Decode(&s.Attrs)
  48. case "droppedAttributesCount", "dropped_attributes_count":
  49. err = decoder.Decode(&s.DroppedAttrs)
  50. default:
  51. // Skip unknown.
  52. }
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. return nil
  58. }