Skip to content

Commit e140374

Browse files
authored
[exporter/syslog] Add support for octet counting in the syslog exporter (#31014)
**Description:** <Describe what has changed.> This introduces a new configuration `enable_octet_counting` which matches the same configuration on the syslog receiver side, and if enabled will append the length of the message to the start of the message. **Link to tracking Issue:** #31013 **Testing:** Added tests alongside the existing syslog exporter tests **Documentation:** Updated the README with the new config option Signed-off-by: sinkingpoint <[email protected]>
1 parent a95f8d4 commit e140374

File tree

7 files changed

+57
-9
lines changed

7 files changed

+57
-9
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: syslogexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Adding support for sending rfc6587 octet counts in syslog messages
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31013]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

exporter/syslogexporter/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This means that syslog messages received via the Syslog receiver and exported vi
2727
- `protocol` - (default = `rfc5424`) rfc5424/rfc3164
2828
- `rfc5424` - Expects the syslog messages to be rfc5424 compliant
2929
- `rfc3164` - Expects the syslog messages to be rfc3164 compliant
30+
- `enable_octet_counting` (default = `false`) - Whether or not to enable rfc6587 octet counting
3031
- `tls` - configuration for TLS/mTLS
3132
- `insecure` (default = `false`) whether to enable client transport security, by default, TLS is enabled.
3233
- `cert_file` - Path to the TLS cert to use for TLS required connections. Should only be used if `insecure` is set to `false`.

exporter/syslogexporter/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
errInvalidEndpoint = errors.New("invalid endpoint: endpoint is required but it is not configured")
1919
errUnsupportedNetwork = errors.New("unsupported network: network is required, only tcp/udp supported")
2020
errUnsupportedProtocol = errors.New("unsupported protocol: Only rfc5424 and rfc3164 supported")
21+
errOctetCounting = errors.New("octet counting is only supported for rfc5424 protocol")
2122
)
2223

2324
// Config defines configuration for Syslog exporter.
@@ -33,6 +34,9 @@ type Config struct {
3334
// options: rfc5424, rfc3164
3435
Protocol string `mapstructure:"protocol"`
3536

37+
// Wether or not to enable RFC 6587 Octet Counting.
38+
EnableOctetCounting bool `mapstructure:"enable_octet_counting"`
39+
3640
// TLSSetting struct exposes TLS client configuration.
3741
TLSSetting configtls.TLSClientSetting `mapstructure:"tls"`
3842

@@ -63,6 +67,10 @@ func (cfg *Config) Validate() error {
6367
invalidFields = append(invalidFields, errUnsupportedProtocol)
6468
}
6569

70+
if cfg.EnableOctetCounting && cfg.Protocol != protocolRFC5424Str {
71+
invalidFields = append(invalidFields, errOctetCounting)
72+
}
73+
6674
if len(invalidFields) > 0 {
6775
return multierr.Combine(invalidFields...)
6876
}

exporter/syslogexporter/exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func initExporter(cfg *Config, createSettings exporter.CreateSettings) (*sysloge
3636
config: cfg,
3737
logger: createSettings.Logger,
3838
tlsConfig: tlsConfig,
39-
formatter: createFormatter(cfg.Protocol),
39+
formatter: createFormatter(cfg.Protocol, cfg.EnableOctetCounting),
4040
}
4141

4242
s.logger.Info("Syslog Exporter configured",

exporter/syslogexporter/formatter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"go.opentelemetry.io/collector/pdata/plog"
88
)
99

10-
func createFormatter(protocol string) formatter {
10+
func createFormatter(protocol string, octetCounting bool) formatter {
1111
if protocol == protocolRFC5424Str {
12-
return newRFC5424Formatter()
12+
return newRFC5424Formatter(octetCounting)
1313
}
1414
return newRFC3164Formatter()
1515
}

exporter/syslogexporter/rfc5424_formatter.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import (
1313
)
1414

1515
type rfc5424Formatter struct {
16+
octetCounting bool
1617
}
1718

18-
func newRFC5424Formatter() *rfc5424Formatter {
19-
return &rfc5424Formatter{}
19+
func newRFC5424Formatter(octetCounting bool) *rfc5424Formatter {
20+
return &rfc5424Formatter{
21+
octetCounting: octetCounting,
22+
}
2023
}
2124

2225
func (f *rfc5424Formatter) format(logRecord plog.LogRecord) string {
@@ -30,6 +33,11 @@ func (f *rfc5424Formatter) format(logRecord plog.LogRecord) string {
3033
structuredData := f.formatStructuredData(logRecord)
3134
messageString := f.formatMessage(logRecord)
3235
formatted := fmt.Sprintf("<%s>%s %s %s %s %s %s %s%s\n", priorityString, versionString, timestampString, hostnameString, appnameString, pidString, messageIDString, structuredData, messageString)
36+
37+
if f.octetCounting {
38+
formatted = fmt.Sprintf("%d %s", len(formatted), formatted)
39+
}
40+
3341
return formatted
3442
}
3543

exporter/syslogexporter/rfc5424_formatter_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ func TestRFC5424Formatter(t *testing.T) {
2929
require.NoError(t, err)
3030
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
3131

32-
actual := newRFC5424Formatter().format(logRecord)
32+
actual := newRFC5424Formatter(false).format(logRecord)
3333
assert.Equal(t, expected, actual)
34+
octetCounting := newRFC5424Formatter(true).format(logRecord)
35+
assert.Equal(t, fmt.Sprintf("%d %s", len(expected), expected), octetCounting)
3436

3537
expected = "<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog 111 ID47 - BOMAn application event log entry...\n"
3638
logRecord = plog.NewLogRecord()
@@ -45,8 +47,10 @@ func TestRFC5424Formatter(t *testing.T) {
4547
require.NoError(t, err)
4648
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
4749

48-
actual = newRFC5424Formatter().format(logRecord)
50+
actual = newRFC5424Formatter(false).format(logRecord)
4951
assert.Equal(t, expected, actual)
52+
octetCounting = newRFC5424Formatter(true).format(logRecord)
53+
assert.Equal(t, fmt.Sprintf("%d %s", len(expected), expected), octetCounting)
5054

5155
// Test structured data
5256
expectedRegex := "\\<165\\>1 2003-08-24T12:14:15.000003Z 192\\.0\\.2\\.1 myproc 8710 - " +
@@ -72,7 +76,7 @@ func TestRFC5424Formatter(t *testing.T) {
7276
require.NoError(t, err)
7377
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
7478

75-
actual = newRFC5424Formatter().format(logRecord)
79+
actual = newRFC5424Formatter(false).format(logRecord)
7680
assert.NoError(t, err)
7781
matched, err := regexp.MatchString(expectedRegex, actual)
7882
assert.NoError(t, err)
@@ -89,6 +93,6 @@ func TestRFC5424Formatter(t *testing.T) {
8993
require.NoError(t, err)
9094
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
9195

92-
actual = newRFC5424Formatter().format(logRecord)
96+
actual = newRFC5424Formatter(false).format(logRecord)
9397
assert.Equal(t, expected, actual)
9498
}

0 commit comments

Comments
 (0)