Skip to content

Commit 97256f5

Browse files
paulojmdiasandrzej-stencel
authored andcommitted
[exporter/syslogexporter] Fix timestamp formatting in rfc5424 syslog messages to use microsecond precision (open-telemetry#43211)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR fixes the timestamp format used for `rfc5424` format to be with the maximum of microsecond precision instead of nanosecond as defined by the [RFC5424 spec](https://www.rfc-editor.org/rfc/rfc5424#section-6). <!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes open-telemetry#43114 <!--Describe what testing was performed and which tests were added.--> #### Testing Updated tests and added a new unit test to ensure the timestamp is always exported with the maximum of microsecond precision. --------- Signed-off-by: Paulo Dias <[email protected]> Co-authored-by: Andrzej Stencel <[email protected]>
1 parent 44d78c2 commit 97256f5

File tree

11 files changed

+77
-16
lines changed

11 files changed

+77
-16
lines changed

.chloggen/feat_43114.yaml

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: "bug_fix"
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: "exporter/syslog"
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Fix timestamp formatting in rfc5424 syslog messages to use microsecond precision"
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: [43114]
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: [user]

cmd/opampsupervisor/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ require (
8787
github.com/klauspost/compress v1.18.0 // indirect
8888
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
8989
github.com/knadh/koanf/providers/confmap v1.0.0 // indirect
90-
github.com/leodido/go-syslog/v4 v4.2.0 // indirect
90+
github.com/leodido/go-syslog/v4 v4.3.0 // indirect
9191
github.com/leodido/ragel-machinery v0.0.0-20190525184631-5f46317e436b // indirect
9292
github.com/lightstep/go-expohisto v1.0.0 // indirect
9393
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect

cmd/opampsupervisor/go.sum

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/elasticsearchexporter/integrationtest/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ require (
9292
github.com/knadh/koanf/maps v0.1.2 // indirect
9393
github.com/knadh/koanf/providers/confmap v1.0.0 // indirect
9494
github.com/knadh/koanf/v2 v2.3.0 // indirect
95-
github.com/leodido/go-syslog/v4 v4.2.0 // indirect
95+
github.com/leodido/go-syslog/v4 v4.3.0 // indirect
9696
github.com/leodido/ragel-machinery v0.0.0-20190525184631-5f46317e436b // indirect
9797
github.com/lestrrat-go/strftime v1.1.1 // indirect
9898
github.com/lightstep/go-expohisto v1.0.0 // indirect

exporter/elasticsearchexporter/integrationtest/go.sum

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/syslogexporter/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24.0
44

55
require (
66
github.com/cenkalti/backoff/v5 v5.0.3
7+
github.com/leodido/go-syslog/v4 v4.3.0
78
github.com/stretchr/testify v1.11.1
89
go.opentelemetry.io/collector/component v1.43.0
910
go.opentelemetry.io/collector/component/componenttest v0.137.0

exporter/syslogexporter/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/syslogexporter/rfc5424_formatter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"fmt"
88
"strconv"
99
"strings"
10-
"time"
1110

11+
"github.com/leodido/go-syslog/v4/rfc5424"
1212
"go.opentelemetry.io/collector/pdata/pcommon"
1313
"go.opentelemetry.io/collector/pdata/plog"
1414
)
@@ -51,7 +51,7 @@ func (*rfc5424Formatter) formatVersion(logRecord plog.LogRecord) string {
5151
}
5252

5353
func (*rfc5424Formatter) formatTimestamp(logRecord plog.LogRecord) string {
54-
return logRecord.Timestamp().AsTime().Format(time.RFC3339Nano)
54+
return logRecord.Timestamp().AsTime().Format(rfc5424.RFC3339MICRO)
5555
}
5656

5757
func (*rfc5424Formatter) formatHostname(logRecord plog.LogRecord) string {

exporter/syslogexporter/rfc5424_formatter_test.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/leodido/go-syslog/v4/rfc5424"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
1516
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -25,7 +26,7 @@ func TestRFC5424Formatter(t *testing.T) {
2526
logRecord.Attributes().PutInt("priority", 165)
2627
logRecord.Attributes().PutStr("proc_id", "8710")
2728
logRecord.Attributes().PutInt("version", 1)
28-
timestamp, err := time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003Z")
29+
timestamp, err := time.Parse(rfc5424.RFC3339MICRO, "2003-08-24T05:14:15.000003Z")
2930
require.NoError(t, err)
3031
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
3132

@@ -43,7 +44,7 @@ func TestRFC5424Formatter(t *testing.T) {
4344
logRecord.Attributes().PutInt("priority", 165)
4445
logRecord.Attributes().PutStr("proc_id", "111")
4546
logRecord.Attributes().PutInt("version", 1)
46-
timestamp, err = time.Parse(time.RFC3339Nano, "2003-10-11T22:14:15.003Z")
47+
timestamp, err = time.Parse(rfc5424.RFC3339MICRO, "2003-10-11T22:14:15.003Z")
4748
require.NoError(t, err)
4849
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
4950

@@ -72,7 +73,7 @@ func TestRFC5424Formatter(t *testing.T) {
7273
structuredDataSubmap.Map().PutStr("UserHostAddress", "192.168.2.132")
7374
structuredDataSubmap.Map().PutStr("UserID", "Tester2")
7475
logRecord.Attributes().PutInt("version", 1)
75-
timestamp, err = time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
76+
timestamp, err = time.Parse(rfc5424.RFC3339MICRO, "2003-08-24T05:14:15.000003-07:00")
7677
require.NoError(t, err)
7778
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
7879

@@ -112,7 +113,7 @@ func TestRFC5424Formatter(t *testing.T) {
112113
structuredDataSubmap.Map().PutStr("UserHostAddress", "192.168.2.132")
113114
logRecord.Attributes().PutInt("version", 1)
114115

115-
timestamp, err = time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
116+
timestamp, err = time.Parse(rfc5424.RFC3339MICRO, "2003-08-24T05:14:15.000003-07:00")
116117
require.NoError(t, err)
117118
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
118119

@@ -151,10 +152,40 @@ func TestRFC5424Formatter(t *testing.T) {
151152
// Test defaults
152153
expected = "<165>1 2003-08-24T12:14:15.000003Z - - - - -\n"
153154
logRecord = plog.NewLogRecord()
154-
timestamp, err = time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
155+
timestamp, err = time.Parse(rfc5424.RFC3339MICRO, "2003-08-24T05:14:15.000003-07:00")
155156
require.NoError(t, err)
156157
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
157158

158159
actual = newRFC5424Formatter(false).format(logRecord)
159160
assert.Equal(t, expected, actual)
160161
}
162+
163+
func TestRFC5424Formatter_NanoFractionIsTruncatedToMicro(t *testing.T) {
164+
logRecord := plog.NewLogRecord()
165+
logRecord.Attributes().PutStr("appname", "myapp")
166+
logRecord.Attributes().PutStr("hostname", "myhost")
167+
logRecord.Attributes().PutStr("message", "nano->micro")
168+
logRecord.Attributes().PutInt("priority", 14)
169+
logRecord.Attributes().PutInt("version", 1)
170+
logRecord.Attributes().PutStr("proc_id", "1234")
171+
172+
timestamp, err := time.Parse(rfc5424.RFC3339MICRO, "2025-10-02T20:04:11.518870899Z")
173+
require.NoError(t, err)
174+
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
175+
176+
expectedPrefix := "<14>1 2025-10-02T20:04:11.51887Z myhost myapp 1234 - - nano->micro"
177+
actual := newRFC5424Formatter(false).format(logRecord)
178+
179+
// The formatted output should contain the truncated (not rounded) timestamp
180+
assert.Contains(t, actual, expectedPrefix)
181+
182+
// Validate timestamp parseability using RFC3339MICRO layout
183+
parts := strings.Split(actual, " ")
184+
require.GreaterOrEqual(t, len(parts), 3)
185+
_, err = time.Parse(rfc5424.RFC3339MICRO, parts[1])
186+
require.NoError(t, err)
187+
188+
// Check that octet counting mode also works correctly
189+
octetCounting := newRFC5424Formatter(true).format(logRecord)
190+
assert.True(t, strings.HasPrefix(octetCounting, fmt.Sprintf("%d ", len(actual))))
191+
}

testbed/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ require (
236236
github.com/knadh/koanf/v2 v2.3.0 // indirect
237237
github.com/kolo/xmlrpc v0.0.0-20220921171641-a4b6fa1dd06b // indirect
238238
github.com/kylelemons/godebug v1.1.0 // indirect
239-
github.com/leodido/go-syslog/v4 v4.2.0 // indirect
239+
github.com/leodido/go-syslog/v4 v4.3.0 // indirect
240240
github.com/leodido/ragel-machinery v0.0.0-20190525184631-5f46317e436b // indirect
241241
github.com/lightstep/go-expohisto v1.0.0 // indirect
242242
github.com/linode/linodego v1.52.2 // indirect

0 commit comments

Comments
 (0)