Skip to content

Commit

Permalink
feat: add integration test for syslog exporter (and receiver) (open-t…
Browse files Browse the repository at this point in the history
…elemetry#27464)

**Description:**

Adding integration tests for syslog exporter (and syslog receiver) and
fixing bugs which has been found during the process

**Link to tracking Issue:** open-telemetry#21245 

**Testing:**

Integration tests and more unit tests

**Documentation:**

N/A

---------

Signed-off-by: Dominik Rosiek <drosiek@sumologic.com>
Co-authored-by: Daniel Jaglowski <jaglows3@gmail.com>
  • Loading branch information
2 people authored and JaredTan95 committed Oct 18, 2023
1 parent c38a394 commit fc2a3b3
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 17 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]))
appname := ""
if msg[app] != emptyValue {
appname = msg[app].(string) + ":"
}
if appname != "" && message != emptyMessage {
appname += " "
}
return fmt.Sprintf("<%d>%s %s %s%s", msg[priority], timestampString, msg[hostname], appname, 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)
}
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 DataReceiver
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.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sapmexporter v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/zipkinexporter v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver v0.87.0
Expand All @@ -22,6 +24,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkhecreceiver v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/testbed/mockdatareceivers/mockawsxrayreceiver v0.87.0
github.com/open-telemetry/opentelemetry-collector-contrib/testbed/mockdatasenders/mockdatadogagentexporter v0.87.0
Expand Down Expand Up @@ -79,6 +82,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.24 // indirect
Expand Down Expand Up @@ -146,6 +150,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 @@ -157,6 +162,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.

4 changes: 4 additions & 0 deletions testbed/testbed/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/opencensusexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/zipkinexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/opencensusreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver"
)

Expand All @@ -43,6 +45,7 @@ func Components() (
jaegerreceiver.NewFactory(),
opencensusreceiver.NewFactory(),
otlpreceiver.NewFactory(),
syslogreceiver.NewFactory(),
zipkinreceiver.NewFactory(),
)
errs = multierr.Append(errs, err)
Expand All @@ -52,6 +55,7 @@ func Components() (
opencensusexporter.NewFactory(),
otlpexporter.NewFactory(),
otlphttpexporter.NewFactory(),
syslogexporter.NewFactory(),
zipkinexporter.NewFactory(),
)
errs = multierr.Append(errs, err)
Expand Down
Loading

0 comments on commit fc2a3b3

Please sign in to comment.