rfc5424_formatter.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
  4. import (
  5. "fmt"
  6. "strconv"
  7. "time"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. "go.opentelemetry.io/collector/pdata/plog"
  10. )
  11. type rfc5424Formatter struct {
  12. }
  13. func newRFC5424Formatter() *rfc5424Formatter {
  14. return &rfc5424Formatter{}
  15. }
  16. func (f *rfc5424Formatter) format(logRecord plog.LogRecord) string {
  17. priorityString := f.formatPriority(logRecord)
  18. versionString := f.formatVersion(logRecord)
  19. timestampString := f.formatTimestamp(logRecord)
  20. hostnameString := f.formatHostname(logRecord)
  21. appnameString := f.formatAppname(logRecord)
  22. pidString := f.formatPid(logRecord)
  23. messageIDString := f.formatMessageID(logRecord)
  24. structuredData := f.formatStructuredData(logRecord)
  25. messageString := f.formatMessage(logRecord)
  26. formatted := fmt.Sprintf("<%s>%s %s %s %s %s %s %s%s\n", priorityString, versionString, timestampString, hostnameString, appnameString, pidString, messageIDString, structuredData, messageString)
  27. return formatted
  28. }
  29. func (f *rfc5424Formatter) formatPriority(logRecord plog.LogRecord) string {
  30. return getAttributeValueOrDefault(logRecord, priority, strconv.Itoa(defaultPriority))
  31. }
  32. func (f *rfc5424Formatter) formatVersion(logRecord plog.LogRecord) string {
  33. return getAttributeValueOrDefault(logRecord, version, strconv.Itoa(versionRFC5424))
  34. }
  35. func (f *rfc5424Formatter) formatTimestamp(logRecord plog.LogRecord) string {
  36. return logRecord.Timestamp().AsTime().Format(time.RFC3339Nano)
  37. }
  38. func (f *rfc5424Formatter) formatHostname(logRecord plog.LogRecord) string {
  39. return getAttributeValueOrDefault(logRecord, hostname, emptyValue)
  40. }
  41. func (f *rfc5424Formatter) formatAppname(logRecord plog.LogRecord) string {
  42. return getAttributeValueOrDefault(logRecord, app, emptyValue)
  43. }
  44. func (f *rfc5424Formatter) formatPid(logRecord plog.LogRecord) string {
  45. return getAttributeValueOrDefault(logRecord, pid, emptyValue)
  46. }
  47. func (f *rfc5424Formatter) formatMessageID(logRecord plog.LogRecord) string {
  48. return getAttributeValueOrDefault(logRecord, msgID, emptyValue)
  49. }
  50. func (f *rfc5424Formatter) formatStructuredData(logRecord plog.LogRecord) string {
  51. structuredDataAttributeValue, found := logRecord.Attributes().Get(structuredData)
  52. if !found {
  53. return emptyValue
  54. }
  55. if structuredDataAttributeValue.Type() != pcommon.ValueTypeMap {
  56. return emptyValue
  57. }
  58. sdElements := []string{}
  59. for key, val := range structuredDataAttributeValue.Map().AsRaw() {
  60. sdElements = append(sdElements, key)
  61. vval, ok := val.(map[string]any)
  62. if !ok {
  63. continue
  64. }
  65. for k, v := range vval {
  66. vv, ok := v.(string)
  67. if !ok {
  68. continue
  69. }
  70. sdElements = append(sdElements, fmt.Sprintf("%s=\"%s\"", k, vv))
  71. }
  72. }
  73. return fmt.Sprint(sdElements)
  74. }
  75. func (f *rfc5424Formatter) formatMessage(logRecord plog.LogRecord) string {
  76. formatted := getAttributeValueOrDefault(logRecord, message, emptyMessage)
  77. if len(formatted) > 0 {
  78. formatted = " " + formatted
  79. }
  80. return formatted
  81. }