Skip to content

Commit

Permalink
feat: add integration test for syslog
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Rosiek <drosiek@sumologic.com>
  • Loading branch information
Dominik Rosiek committed Oct 6, 2023
1 parent 37233ae commit c6b8605
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 18 deletions.
27 changes: 27 additions & 0 deletions .chloggen/drosiek-integration-tests.yaml
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: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: syslog

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: add integration tests and fix related bugs

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [21245]

# (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:

# 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]
13 changes: 10 additions & 3 deletions exporter/syslogexporter/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,24 @@ func populateDefaults(msg map[string]any, msgProperties []string) {
}

func (s *sender) formatRFC3164(msg map[string]any, timestamp time.Time) string {
msgProperties := []string{priority, hostname, message}
msgProperties := []string{priority, hostname, message, app}
populateDefaults(msg, msgProperties)
timestampString := timestamp.Format("Jan 02 15:04:05")
return fmt.Sprintf("<%d>%s %s%s", msg[priority], timestampString, msg[hostname], formatMessagePart(msg[message]))
a := ""
if msg[app] != emptyValue {
a = msg[app].(string) + ":"
}
if a != "" && message != emptyMessage {
a += " "
}
return fmt.Sprintf("<%d>%s %s %s%s", msg[priority], timestampString, msg[hostname], a, msg[message])
}

func (s *sender) formatRFC5424(msg map[string]any, timestamp time.Time) string {
msgProperties := []string{priority, version, hostname, app, pid, msgID, message, structuredData}
populateDefaults(msg, msgProperties)
s.addStructuredData(msg)
timestampString := timestamp.Format(time.RFC3339)
timestampString := timestamp.Format(time.RFC3339Nano)

return fmt.Sprintf("<%d>%d %s %s %s %s %s %s%s", msg[priority], msg[version], timestampString, msg[hostname], msg[app], msg[pid], msg[msgID], msg[structuredData], formatMessagePart(msg[message]))
}
Expand Down
41 changes: 28 additions & 13 deletions exporter/syslogexporter/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestFormatRFC5424(t *testing.T) {
"version": 1,
}

expected := "<165>1 2003-08-24T05:14:15-07:00 192.0.2.1 myproc 8710 - - It's time to make the do-nuts."
timeObj1, err := time.Parse(time.RFC3339, "2003-08-24T05:14:15.000003-07:00")
expected := "<165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1 myproc 8710 - - It's time to make the do-nuts."
timeObj1, err := time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
assert.Equal(t, expected, s.formatMsg(msg, timeObj1))
assert.Nil(t, err)

Expand All @@ -47,8 +47,8 @@ func TestFormatRFC5424(t *testing.T) {
"version": 1,
}

expected2 := "<165>1 2003-10-11T22:14:15Z mymachine.example.com evntslog 111 ID47 - BOMAn application event log entry..."
timeObj2, err := time.Parse(time.RFC3339, "2003-10-11T22:14:15.003Z")
expected2 := "<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog 111 ID47 - BOMAn application event log entry..."
timeObj2, err := time.Parse(time.RFC3339Nano, "2003-10-11T22:14:15.003Z")
assert.Nil(t, err)
assert.Equal(t, expected2, s.formatMsg(msg2, timeObj2))

Expand All @@ -72,9 +72,9 @@ func TestFormatRFC5424(t *testing.T) {
},
}

expectedForm := "\\<165\\>1 2003-08-24T05:14:15-07:00 192\\.0\\.2\\.1 myproc 8710 - " +
expectedForm := "\\<165\\>1 2003-08-24T05:14:15.000003-07:00 192\\.0\\.2\\.1 myproc 8710 - " +
"\\[\\S+ \\S+ \\S+ \\S+ \\S+\\] It's time to make the do-nuts\\."
timeObj3, err := time.Parse(time.RFC3339, "2003-08-24T05:14:15.000003-07:00")
timeObj3, err := time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
assert.Nil(t, err)
formattedMsg := s.formatMsg(msg3, timeObj3)
matched, err := regexp.MatchString(expectedForm, formattedMsg)
Expand All @@ -87,8 +87,8 @@ func TestFormatRFC5424(t *testing.T) {

// Test defaults
msg4 := map[string]any{}
expected = "<165>1 2003-08-24T05:14:15-07:00 - - - - -"
timeObj1, err = time.Parse(time.RFC3339, "2003-08-24T05:14:15.000003-07:00")
expected = "<165>1 2003-08-24T05:14:15.000003-07:00 - - - - -"
timeObj1, err = time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
assert.Equal(t, expected, s.formatMsg(msg4, timeObj1))
assert.Nil(t, err)

Expand All @@ -112,9 +112,9 @@ func TestFormatRFC5424(t *testing.T) {
},
}

expectedForm = "\\<165\\>1 2003-08-24T05:14:15-07:00 192\\.0\\.2\\.1 myproc 8710 - " +
expectedForm = "\\<165\\>1 2003-08-24T05:14:15.000003-07:00 192\\.0\\.2\\.1 myproc 8710 - " +
"\\[\\S+ \\S+ \\S+ \\S+ \\S+\\] It's time to make the do-nuts\\."
timeObj5, err := time.Parse(time.RFC3339, "2003-08-24T05:14:15.000003-07:00")
timeObj5, err := time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
assert.Nil(t, err)
formattedMsg = s.formatMsg(msg5, timeObj5)
matched, err = regexp.MatchString(expectedForm, formattedMsg)
Expand All @@ -130,10 +130,25 @@ func TestFormatRFC3164(t *testing.T) {

s := sender{protocol: protocolRFC3164Str}

msg := map[string]interface{}{
"message": "'su root' failed for lonvick on /dev/pts/8",
"hostname": "mymachine",
"appname": "su",
"priority": int64(34),
"facility": int64(4),
}

expected := "<34>Aug 24 05:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8"
timeObj1, err := time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
assert.Equal(t, expected, s.formatMsg(msg, timeObj1))
assert.Nil(t, err)

// Test defaults
msg4 := map[string]any{}
expected := "<165>Aug 24 05:14:15 -"
timeObj1, err := time.Parse(time.RFC3339, "2003-08-24T05:14:15.000003-07:00")
msg4 := map[string]interface{}{
"message": "-",
}
expected = "<165>Aug 24 05:14:15 - -"
timeObj1, err = time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
assert.Equal(t, expected, s.formatMsg(msg4, timeObj1))
assert.Nil(t, err)
}
7 changes: 6 additions & 1 deletion pkg/stanza/operator/parser/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

sl "github.com/influxdata/go-syslog/v3"
Expand Down Expand Up @@ -77,6 +78,8 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
return nil, err
}

proto := strings.ToLower(c.Protocol)

switch {
case c.Protocol == "":
return nil, fmt.Errorf("missing field 'protocol'")
Expand All @@ -88,6 +91,8 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
if *c.NonTransparentFramingTrailer != NULTrailer && *c.NonTransparentFramingTrailer != LFTrailer {
return nil, fmt.Errorf("invalid non_transparent_framing_trailer '%s'. Must be either 'LF' or 'NUL'", *c.NonTransparentFramingTrailer)
}
case proto != RFC5424 && proto != RFC3164:
return nil, fmt.Errorf("unsupported protocol version: %s", c.Protocol)
}

if c.Location == "" {
Expand All @@ -101,7 +106,7 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {

return &Parser{
ParserOperator: parserOperator,
protocol: c.Protocol,
protocol: proto,
location: location,
enableOctetCounting: c.EnableOctetCounting,
nonTransparentFramingTrailer: c.NonTransparentFramingTrailer,
Expand Down
16 changes: 16 additions & 0 deletions pkg/stanza/operator/parser/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,19 @@ func TestSyslogParseRFC5424_SDNameTooLong(t *testing.T) {
require.FailNow(t, "Timed out waiting for entry to be processed")
}
}

func TestSyslogProtocolConfig(t *testing.T) {
for _, proto := range []string{"RFC5424", "rfc5424", "RFC3164", "rfc3164"} {
cfg := basicConfig()
cfg.Protocol = proto
_, err := cfg.Build(testutil.Logger(t))
require.NoError(t, err)
}

for _, proto := range []string{"RFC5424a", "rfc5424b", "RFC3164c", "rfc3164d"} {
cfg := basicConfig()
cfg.Protocol = proto
_, err := cfg.Build(testutil.Logger(t))
require.Error(t, err)
}
}
2 changes: 1 addition & 1 deletion testbed/datareceivers/carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type CarbonDataReceiver struct {
receiver receiver.Metrics
}

// Ensure CarbonDataReceiver implements MetricDataSender.
// Ensure CarbonDataReceiver implements MetricDataReceiver.
var _ testbed.DataReceiver = (*CarbonDataReceiver)(nil)

// NewCarbonDataReceiver creates a new CarbonDataReceiver that will listen on the
Expand Down
72 changes: 72 additions & 0 deletions testbed/datareceivers/syslog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package datareceivers // import "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/datareceivers"

import (
"context"
"fmt"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/tcp"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/testbed/testbed"
)

// SyslogDataReceiver implements Syslog format receiver.
type SyslogDataReceiver struct {
testbed.DataReceiverBase
receiver receiver.Logs
protocol string
}

// Ensure SyslogDataReceiver implements LogDataReceiver.
var _ testbed.DataReceiver = (*SyslogDataReceiver)(nil)

// NewSyslogDataReceiver creates a new SyslogDataReceiver that will listen on the
// specified port after Start is called.
func NewSyslogDataReceiver(protocol string, port int) *SyslogDataReceiver {
return &SyslogDataReceiver{DataReceiverBase: testbed.DataReceiverBase{Port: port}, protocol: protocol}
}

// Start the receiver.
func (cr *SyslogDataReceiver) Start(_ consumer.Traces, _ consumer.Metrics, lc consumer.Logs) error {
factory := syslogreceiver.NewFactory()
addr := fmt.Sprintf("127.0.0.1:%d", cr.Port)
cfg := factory.CreateDefaultConfig().(*syslogreceiver.SysLogConfig)
cfg.InputConfig.TCP = &tcp.BaseConfig{
ListenAddress: addr,
}
cfg.InputConfig.Protocol = cr.protocol

set := receivertest.NewNopCreateSettings()
var err error
cr.receiver, err = factory.CreateLogsReceiver(context.Background(), set, cfg, lc)
if err != nil {
return err
}

return cr.receiver.Start(context.Background(), componenttest.NewNopHost())
}

// Stop the receiver.
func (cr *SyslogDataReceiver) Stop() error {
return cr.receiver.Shutdown(context.Background())
}

// GenConfigYAMLStr returns receiver config for the agent.
func (cr *SyslogDataReceiver) GenConfigYAMLStr() string {
// Note that this generates an receiver config for agent.
return fmt.Sprintf(`
syslog:
endpoint: "127.0.0.1:%d"`, cr.Port)
}

// ProtocolName returns protocol name as it is specified in Collector config.
func (cr *SyslogDataReceiver) ProtocolName() string {
return "tcp"
}
14 changes: 14 additions & 0 deletions testbed/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusexporter v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sapmexporter v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/zipkinexporter v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver v0.86.1-0.20231004185026-b5635a7a90d2
Expand All @@ -22,6 +24,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkhecreceiver v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/testbed/mockdatareceivers/mockawsxrayreceiver v0.86.1-0.20231004185026-b5635a7a90d2
github.com/open-telemetry/opentelemetry-collector-contrib/testbed/mockdatasenders/mockdatadogagentexporter v0.86.1-0.20231004185026-b5635a7a90d2
Expand Down Expand Up @@ -78,6 +81,7 @@ require (
github.com/DataDog/datadog-agent/pkg/trace/exportable v0.0.0-20201016145401-4646cf596b02 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/antonmedv/expr v1.15.3 // indirect
github.com/apache/thrift v0.19.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go v1.45.20 // indirect
Expand Down Expand Up @@ -145,6 +149,7 @@ require (
github.com/hetznercloud/hcloud-go/v2 v2.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/influxdata/go-syslog/v3 v3.0.1-0.20210608084020-ac565dc76ba6 // indirect
github.com/ionos-cloud/sdk-go/v6 v6.1.8 // indirect
github.com/jaegertracing/jaeger v1.48.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand All @@ -156,6 +161,7 @@ require (
github.com/knadh/koanf/v2 v2.0.1 // indirect
github.com/kolo/xmlrpc v0.0.0-20220921171641-a4b6fa1dd06b // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/leodido/ragel-machinery v0.0.0-20181214104525-299bdde78165 // indirect
github.com/linode/linodego v1.19.0 // indirect
github.com/lufia/plan9stats v0.0.0-20220913051719-115f729f3c8c // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand Down Expand Up @@ -291,6 +297,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signa

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter => ../exporter/splunkhecexporter

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter => ../exporter/syslogexporter

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/zipkinexporter => ../exporter/zipkinexporter

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../internal/common
Expand Down Expand Up @@ -329,6 +337,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signa

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkhecreceiver => ../receiver/splunkhecreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver => ../receiver/syslogreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver => ../receiver/zipkinreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/testbed/mockdatareceivers/mockawsxrayreceiver => ../testbed/mockdatareceivers/mockawsxrayreceiver
Expand All @@ -341,6 +351,10 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/corei

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry => ../pkg/resourcetotelemetry

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza => ../pkg/stanza

replace github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage => ../extension/storage

retract (
v0.76.2
v0.76.1
Expand Down
6 changes: 6 additions & 0 deletions testbed/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c6b8605

Please sign in to comment.