Skip to content

Commit 738b7f1

Browse files
pbuskochombium
authored andcommitted
Add option to use nontransparent framing
Co-authored-by: Pavel Busko <[email protected]>
1 parent 5bc704b commit 738b7f1

File tree

15 files changed

+933
-1
lines changed

15 files changed

+933
-1
lines changed

src/cmd/syslog-server/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type Config struct {
2424

2525
SyslogClientTrustedCAFile string `env:"SYSLOG_CLIENT_TRUSTED_CA_FILE, report"`
2626

27+
SyslogNonTransparentFraming bool `env:"SYSLOG_NON_TRANSPARENT_FRAMING, report"`
28+
2729
MetricsServer config.MetricsServer
2830
UseRFC339 bool `env:"USE_RFC339"`
2931
}

src/cmd/syslog-server/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func main() {
8484
serverOptions = append(serverOptions, syslog.WithSyslogClientCA(cfg.SyslogClientTrustedCAFile))
8585
}
8686

87+
if cfg.SyslogNonTransparentFraming {
88+
serverOptions = append(serverOptions, syslog.WithNonTransparentFraming())
89+
}
90+
8791
server := syslog.NewServer(
8892
loggr,
8993
m,

src/internal/syslog/server.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
metrics "code.cloudfoundry.org/go-metric-registry"
1313
"code.cloudfoundry.org/tlsconfig"
1414
"github.com/leodido/go-syslog/v4"
15+
"github.com/leodido/go-syslog/v4/nontransparent"
1516
"github.com/leodido/go-syslog/v4/octetcounting"
1617
"github.com/leodido/go-syslog/v4/rfc5424"
1718

@@ -34,9 +35,11 @@ type Server struct {
3435
idleTimeout time.Duration
3536
maxMessageLength int
3637
trimMessageWhitespace bool
38+
nonTransparentFraming bool
3739

3840
ingress metrics.Counter
3941
invalidIngress metrics.Counter
42+
newParserFunc func(...syslog.ParserOption) syslog.Parser
4043

4144
loggr *log.Logger
4245
}
@@ -58,12 +61,17 @@ func NewServer(
5861
idleTimeout: 2 * time.Minute,
5962
maxMessageLength: 65 * 1024, // Diego should never send logs bigger than 64Kib
6063
trimMessageWhitespace: true,
64+
newParserFunc: octetcounting.NewParser,
6165
}
6266

6367
for _, o := range opts {
6468
o(s)
6569
}
6670

71+
if s.nonTransparentFraming {
72+
s.newParserFunc = nontransparent.NewParser
73+
}
74+
6775
s.ingress = m.NewCounter(
6876
"ingress",
6977
"Total syslog messages ingressed successfully.",
@@ -107,6 +115,12 @@ func WithSyslogClientCA(syslogClientCA string) ServerOption {
107115
}
108116
}
109117

118+
func WithNonTransparentFraming() ServerOption {
119+
return func(s *Server) {
120+
s.nonTransparentFraming = true
121+
}
122+
}
123+
110124
func WithIdleTimeout(d time.Duration) ServerOption {
111125
return func(s *Server) {
112126
s.idleTimeout = d
@@ -159,7 +173,7 @@ func (s *Server) handleConnection(conn net.Conn) {
159173
defer conn.Close()
160174
s.setReadDeadline(conn)
161175

162-
p := octetcounting.NewParser(
176+
p := s.newParserFunc(
163177
syslog.WithMaxMessageLength(s.maxMessageLength),
164178
syslog.WithListener(s.parseListenerForConnection(conn)),
165179
)

src/internal/syslog/server_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"log"
99
"net"
10+
"strings"
1011
"time"
1112

1213
"code.cloudfoundry.org/go-loggregator/v10/rpc/loggregator_v2"
@@ -27,6 +28,15 @@ const (
2728
TIMER_MSG = `128 <14>1 1970-01-01T00:00:00.012345+00:00 test-hostname test-app-id [APP/2] - [timer@47450 name="some-name" start="10" stop="20"] ` + "\n"
2829
)
2930

31+
var (
32+
// Same messages but without the MSGLEN
33+
_, LOG_MSG_NT, _ = strings.Cut(LOG_MSG, " ")
34+
_, COUNTER_MSG_NT, _ = strings.Cut(COUNTER_MSG, " ")
35+
_, GAUGE_MSG_NT, _ = strings.Cut(GAUGE_MSG, " ")
36+
_, EVENT_MSG_NT, _ = strings.Cut(EVENT_MSG, " ")
37+
_, TIMER_MSG_NT, _ = strings.Cut(TIMER_MSG, " ")
38+
)
39+
3040
var _ = Describe("Server", func() {
3141
var (
3242
spyRegistry *testhelpers.SpyMetricsRegistry
@@ -221,6 +231,89 @@ var _ = Describe("Server", func() {
221231
Entry("explicitly enabled", syslog.WithServerTrimMessageWhitespace(true), "just a test with with whitespace"),
222232
)
223233

234+
DescribeTableSubtree("nontransparent framing",
235+
func(msg string, expected *loggregator_v2.Envelope) {
236+
BeforeEach(func() {
237+
serverOpts = append(serverOpts, syslog.WithNonTransparentFraming())
238+
})
239+
240+
It("correctly parses RFC5424 formatted messages without MSGLEN", func() {
241+
_, err := fmt.Fprint(clientConn, msg)
242+
Expect(err).ToNot(HaveOccurred())
243+
244+
Expect(server.Stream(context.Background(), &loggregator_v2.EgressBatchRequest{})()).Should(ContainElement(expected))
245+
})
246+
},
247+
Entry("log", LOG_MSG_NT, &loggregator_v2.Envelope{
248+
Tags: map[string]string{
249+
"source_type": "actual-source-type",
250+
"key": "value",
251+
},
252+
InstanceId: "2",
253+
Timestamp: 12345000,
254+
SourceId: "test-app-id",
255+
Message: &loggregator_v2.Envelope_Log{
256+
Log: &loggregator_v2.Log{
257+
Payload: []byte("just a test"),
258+
Type: loggregator_v2.Log_OUT,
259+
},
260+
},
261+
}),
262+
Entry("counter", COUNTER_MSG_NT, &loggregator_v2.Envelope{
263+
InstanceId: "1",
264+
Timestamp: 12345000,
265+
SourceId: "test-app-id",
266+
Tags: map[string]string{
267+
"key": "value",
268+
},
269+
Message: &loggregator_v2.Envelope_Counter{
270+
Counter: &loggregator_v2.Counter{
271+
Name: "test",
272+
Delta: 5,
273+
Total: 10,
274+
},
275+
},
276+
}),
277+
Entry("gauge", GAUGE_MSG_NT, &loggregator_v2.Envelope{
278+
InstanceId: "3",
279+
Timestamp: 12345000,
280+
SourceId: "test-app-id",
281+
Tags: map[string]string{},
282+
Message: &loggregator_v2.Envelope_Gauge{
283+
Gauge: &loggregator_v2.Gauge{
284+
Metrics: map[string]*loggregator_v2.GaugeValue{
285+
"cpu": {Unit: "percentage", Value: 0.23},
286+
},
287+
},
288+
},
289+
}),
290+
Entry("event", EVENT_MSG_NT, &loggregator_v2.Envelope{
291+
InstanceId: "2",
292+
Timestamp: 12345000,
293+
SourceId: "test-app-id",
294+
Tags: map[string]string{},
295+
Message: &loggregator_v2.Envelope_Event{
296+
Event: &loggregator_v2.Event{
297+
Title: "event-title",
298+
Body: "event-body",
299+
},
300+
},
301+
}),
302+
Entry("timer", TIMER_MSG_NT, &loggregator_v2.Envelope{
303+
InstanceId: "2",
304+
Timestamp: 12345000,
305+
SourceId: "test-app-id",
306+
Tags: map[string]string{},
307+
Message: &loggregator_v2.Envelope_Timer{
308+
Timer: &loggregator_v2.Timer{
309+
Name: "some-name",
310+
Start: 10,
311+
Stop: 20,
312+
},
313+
},
314+
}),
315+
)
316+
224317
Context("when max message length is exceeded", func() {
225318
BeforeEach(func() {
226319
serverOpts = append(serverOpts, syslog.WithServerMaxMessageLength(128))

0 commit comments

Comments
 (0)