-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporter/syslog] send syslog messages in batches (#27799)
**Description:** This changes the behavior of the Syslog exporter to send each batch of Syslog messages in a single request (with messages separated by newlines), instead of sending each message in a separate request and closing the connection after each message. The batching only happens when using TCP. For UDP, each syslog message is still sent in a separate request, as defined by [the spec](https://datatracker.ietf.org/doc/html/rfc5426#section-3.1). This also significantly refactors (and hopefully simplifies) the exporter's code, extracting the code that formats the syslog messages from the `sender` type into separate `formatter` types. Hopefully this will make the development of this component easier. **Link to tracking Issue:** - #21244 **Testing:** The unit tests have been updated to reflect the refactored codebase. The integration tests introduced in #27464 are unchanged, as the format of the output messages hasn't changed. **Documentation:** No documentation updates.
- Loading branch information
1 parent
3d94380
commit f2ec166
Showing
11 changed files
with
410 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: exporter/syslog | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: send syslog messages in batches | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [21244] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: This changes the behavior of the Syslog exporter to send each batch of Syslog messages in a single request (with messages separated by newlines), instead of sending each message in a separate request and closing the connection after each message. | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter" | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
func createFormatter(protocol string) formatter { | ||
if protocol == protocolRFC5424Str { | ||
return newRFC5424Formatter() | ||
} | ||
return newRFC3164Formatter() | ||
} | ||
|
||
type formatter interface { | ||
format(plog.LogRecord) string | ||
} | ||
|
||
// getAttributeValueOrDefault returns the value of the requested log record's attribute as a string. | ||
// If the attribute was not found, it returns the provided default value. | ||
func getAttributeValueOrDefault(logRecord plog.LogRecord, attributeName string, defaultValue string) string { | ||
value := defaultValue | ||
if attributeValue, found := logRecord.Attributes().Get(attributeName); found { | ||
value = attributeValue.AsString() | ||
} | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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" | ||
|
||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
type rfc3164Formatter struct { | ||
} | ||
|
||
func newRFC3164Formatter() *rfc3164Formatter { | ||
return &rfc3164Formatter{} | ||
} | ||
|
||
func (f *rfc3164Formatter) format(logRecord plog.LogRecord) string { | ||
priorityString := f.formatPriority(logRecord) | ||
timestampString := f.formatTimestamp(logRecord) | ||
hostnameString := f.formatHostname(logRecord) | ||
appnameString := f.formatAppname(logRecord) | ||
messageString := f.formatMessage(logRecord) | ||
appnameMessageDelimiter := "" | ||
if len(appnameString) > 0 && messageString != emptyMessage { | ||
appnameMessageDelimiter = " " | ||
} | ||
formatted := fmt.Sprintf("<%s>%s %s %s%s%s\n", priorityString, timestampString, hostnameString, appnameString, appnameMessageDelimiter, messageString) | ||
return formatted | ||
} | ||
|
||
func (f *rfc3164Formatter) formatPriority(logRecord plog.LogRecord) string { | ||
return getAttributeValueOrDefault(logRecord, priority, strconv.Itoa(defaultPriority)) | ||
} | ||
|
||
func (f *rfc3164Formatter) formatTimestamp(logRecord plog.LogRecord) string { | ||
return logRecord.Timestamp().AsTime().Format("Jan 02 15:04:05") | ||
} | ||
|
||
func (f *rfc3164Formatter) formatHostname(logRecord plog.LogRecord) string { | ||
return getAttributeValueOrDefault(logRecord, hostname, emptyValue) | ||
} | ||
|
||
func (f *rfc3164Formatter) formatAppname(logRecord plog.LogRecord) string { | ||
value := getAttributeValueOrDefault(logRecord, app, "") | ||
if value != "" { | ||
value += ":" | ||
} | ||
return value | ||
} | ||
|
||
func (f *rfc3164Formatter) formatMessage(logRecord plog.LogRecord) string { | ||
return getAttributeValueOrDefault(logRecord, message, emptyMessage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package syslogexporter | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
func TestRFC3164Formatter(t *testing.T) { | ||
expected := "<34>Aug 24 05:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8\n" | ||
logRecord := plog.NewLogRecord() | ||
logRecord.Attributes().PutStr("appname", "su") | ||
logRecord.Attributes().PutStr("hostname", "mymachine") | ||
logRecord.Attributes().PutStr("message", "'su root' failed for lonvick on /dev/pts/8") | ||
logRecord.Attributes().PutInt("priority", 34) | ||
timestamp, err := time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003Z") | ||
require.NoError(t, err) | ||
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
|
||
actual := newRFC3164Formatter().format(logRecord) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
|
||
expected = "<165>Aug 24 05:14:15 - -\n" | ||
logRecord = plog.NewLogRecord() | ||
logRecord.Attributes().PutStr("message", "-") | ||
timestamp, err = time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003Z") | ||
require.NoError(t, err) | ||
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
|
||
actual = newRFC3164Formatter().format(logRecord) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
} |
Oops, something went wrong.