fields.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sumologicexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sumologicexporter"
  4. import (
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. )
  10. // fields represents metadata
  11. type fields struct {
  12. orig pcommon.Map
  13. replacer *strings.Replacer
  14. }
  15. func newFields(attrMap pcommon.Map) fields {
  16. return fields{
  17. orig: attrMap,
  18. replacer: strings.NewReplacer(",", "_", "=", ":", "\n", "_"),
  19. }
  20. }
  21. // string returns fields as ordered key=value string with `, ` as separator
  22. func (f fields) string() string {
  23. returnValue := make([]string, 0, f.orig.Len())
  24. f.orig.Range(func(k string, v pcommon.Value) bool {
  25. returnValue = append(
  26. returnValue,
  27. fmt.Sprintf(
  28. "%s=%s",
  29. f.sanitizeField(k),
  30. f.sanitizeField(v.AsString()),
  31. ),
  32. )
  33. return true
  34. })
  35. sort.Strings(returnValue)
  36. return strings.Join(returnValue, ", ")
  37. }
  38. // sanitizeFields sanitize field (key or value) to be correctly parsed by sumologic receiver
  39. func (f fields) sanitizeField(fld string) string {
  40. return f.replacer.Replace(fld)
  41. }