1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
- import (
- "fmt"
- "strconv"
- "time"
- "go.opentelemetry.io/collector/pdata/pcommon"
- "go.opentelemetry.io/collector/pdata/plog"
- )
- type rfc5424Formatter struct {
- }
- func newRFC5424Formatter() *rfc5424Formatter {
- return &rfc5424Formatter{}
- }
- func (f *rfc5424Formatter) format(logRecord plog.LogRecord) string {
- priorityString := f.formatPriority(logRecord)
- versionString := f.formatVersion(logRecord)
- timestampString := f.formatTimestamp(logRecord)
- hostnameString := f.formatHostname(logRecord)
- appnameString := f.formatAppname(logRecord)
- pidString := f.formatPid(logRecord)
- messageIDString := f.formatMessageID(logRecord)
- structuredData := f.formatStructuredData(logRecord)
- messageString := f.formatMessage(logRecord)
- formatted := fmt.Sprintf("<%s>%s %s %s %s %s %s %s%s\n", priorityString, versionString, timestampString, hostnameString, appnameString, pidString, messageIDString, structuredData, messageString)
- return formatted
- }
- func (f *rfc5424Formatter) formatPriority(logRecord plog.LogRecord) string {
- return getAttributeValueOrDefault(logRecord, priority, strconv.Itoa(defaultPriority))
- }
- func (f *rfc5424Formatter) formatVersion(logRecord plog.LogRecord) string {
- return getAttributeValueOrDefault(logRecord, version, strconv.Itoa(versionRFC5424))
- }
- func (f *rfc5424Formatter) formatTimestamp(logRecord plog.LogRecord) string {
- return logRecord.Timestamp().AsTime().Format(time.RFC3339Nano)
- }
- func (f *rfc5424Formatter) formatHostname(logRecord plog.LogRecord) string {
- return getAttributeValueOrDefault(logRecord, hostname, emptyValue)
- }
- func (f *rfc5424Formatter) formatAppname(logRecord plog.LogRecord) string {
- return getAttributeValueOrDefault(logRecord, app, emptyValue)
- }
- func (f *rfc5424Formatter) formatPid(logRecord plog.LogRecord) string {
- return getAttributeValueOrDefault(logRecord, pid, emptyValue)
- }
- func (f *rfc5424Formatter) formatMessageID(logRecord plog.LogRecord) string {
- return getAttributeValueOrDefault(logRecord, msgID, emptyValue)
- }
- func (f *rfc5424Formatter) formatStructuredData(logRecord plog.LogRecord) string {
- structuredDataAttributeValue, found := logRecord.Attributes().Get(structuredData)
- if !found {
- return emptyValue
- }
- if structuredDataAttributeValue.Type() != pcommon.ValueTypeMap {
- return emptyValue
- }
- sdElements := []string{}
- for key, val := range structuredDataAttributeValue.Map().AsRaw() {
- sdElements = append(sdElements, key)
- vval, ok := val.(map[string]any)
- if !ok {
- continue
- }
- for k, v := range vval {
- vv, ok := v.(string)
- if !ok {
- continue
- }
- sdElements = append(sdElements, fmt.Sprintf("%s=\"%s\"", k, vv))
- }
- }
- return fmt.Sprint(sdElements)
- }
- func (f *rfc5424Formatter) formatMessage(logRecord plog.LogRecord) string {
- formatted := getAttributeValueOrDefault(logRecord, message, emptyMessage)
- if len(formatted) > 0 {
- formatted = " " + formatted
- }
- return formatted
- }
|