attr.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package telemetry // import "go.opentelemetry.io/otel/trace/internal/telemetry"
  4. // Attr is a key-value pair.
  5. type Attr struct {
  6. Key string `json:"key,omitempty"`
  7. Value Value `json:"value,omitempty"`
  8. }
  9. // String returns an Attr for a string value.
  10. func String(key, value string) Attr {
  11. return Attr{key, StringValue(value)}
  12. }
  13. // Int64 returns an Attr for an int64 value.
  14. func Int64(key string, value int64) Attr {
  15. return Attr{key, Int64Value(value)}
  16. }
  17. // Int returns an Attr for an int value.
  18. func Int(key string, value int) Attr {
  19. return Int64(key, int64(value))
  20. }
  21. // Float64 returns an Attr for a float64 value.
  22. func Float64(key string, value float64) Attr {
  23. return Attr{key, Float64Value(value)}
  24. }
  25. // Bool returns an Attr for a bool value.
  26. func Bool(key string, value bool) Attr {
  27. return Attr{key, BoolValue(value)}
  28. }
  29. // Bytes returns an Attr for a []byte value.
  30. // The passed slice must not be changed after it is passed.
  31. func Bytes(key string, value []byte) Attr {
  32. return Attr{key, BytesValue(value)}
  33. }
  34. // Slice returns an Attr for a []Value value.
  35. // The passed slice must not be changed after it is passed.
  36. func Slice(key string, value ...Value) Attr {
  37. return Attr{key, SliceValue(value...)}
  38. }
  39. // Map returns an Attr for a map value.
  40. // The passed slice must not be changed after it is passed.
  41. func Map(key string, value ...Attr) Attr {
  42. return Attr{key, MapValue(value...)}
  43. }
  44. // Equal returns if a is equal to b.
  45. func (a Attr) Equal(b Attr) bool {
  46. return a.Key == b.Key && a.Value.Equal(b.Value)
  47. }