Skip to content

Commit 3963bdc

Browse files
authored
Merge pull request #114 from kube-logging/framing
loggen: add octet-counting framing functionality
2 parents c630276 + e9a6337 commit 3963bdc

7 files changed

Lines changed: 52 additions & 10 deletions

File tree

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ curl --location --request POST 'localhost:11000/loggen' \
7575
--header 'Content-Type: application/json' \
7676
--data-raw '{
7777
"type": "web",
78-
"format": "nginx.access",
79-
"count": 1000
78+
"format": "nginx",
79+
"count": 1000,
80+
"framing": false
8081
}'
8182
```
8283

@@ -85,8 +86,9 @@ Response:
8586
```json
8687
{
8788
"type": "web",
88-
"format": "nginx.access",
89-
"count": 1000
89+
"format": "nginx",
90+
"count": 1000,
91+
"framing": false
9092
}
9193
```
9294

conf/config.ini.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@
2929

3030
#[nginx]
3131
#enabled = true
32+
33+
#[destination]
34+
#network = "tcp"
35+
#address = "127.0.0.1:514"

formats/golang/golang.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type GolangLog struct {
4242
Level string `json:"level"`
4343
MSG string `json:"msg"`
4444
Time string `json:"time"`
45+
46+
isFramed bool
4547
}
4648

4749
func (g GolangLog) newRandomMessage() string {
@@ -65,7 +67,7 @@ func (g GolangLog) newRandomMessage() string {
6567
return msgList[g.Level]
6668
}
6769

68-
func NewGolangLogRandom(i GolangLogIntensity) GolangLog {
70+
func NewGolangLogRandom(i GolangLogIntensity) *GolangLog {
6971
rand.Seed(time.Now().UTC().UnixNano())
7072
c, err := wr.NewChooser(
7173
wr.Choice{Item: "error", Weight: *i.ErrorWeight},
@@ -76,7 +78,7 @@ func NewGolangLogRandom(i GolangLogIntensity) GolangLog {
7678
if err != nil {
7779
log.Error(err)
7880
}
79-
return GolangLog{
81+
return &GolangLog{
8082
Application: randomdata.StringSample("webshop", "blog"),
8183
Environment: randomdata.StringSample("production", "sandbox", "demo"),
8284
Component: randomdata.StringSample("frontend", "backend", "worker"),
@@ -104,6 +106,14 @@ func (g GolangLog) String() (string, float64) {
104106
return message, float64(len([]byte(message)))
105107
}
106108

109+
func (l *GolangLog) IsFramed() bool {
110+
return l.isFramed
111+
}
112+
113+
func (l *GolangLog) SetFramed(f bool) {
114+
l.isFramed = f
115+
}
116+
107117
func (g GolangLog) Labels() prometheus.Labels {
108118
return prometheus.Labels{
109119
"type": "golang",

log/log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ import (
2121
type Log interface {
2222
String() (string, float64)
2323
Labels() prometheus.Labels
24+
IsFramed() bool
25+
SetFramed(bool)
2426
}

log/template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type LogTemplate struct {
3434

3535
template *template.Template
3636
data LogTemplateData
37+
isFramed bool
3738
}
3839

3940
func NewLogTemplate(format string, fs fs.FS, data LogTemplateData) (*LogTemplate, error) {
@@ -70,6 +71,14 @@ func (l *LogTemplate) String() (string, float64) {
7071
return str, float64(len([]byte(str)))
7172
}
7273

74+
func (l *LogTemplate) IsFramed() bool {
75+
return l.isFramed
76+
}
77+
78+
func (l *LogTemplate) SetFramed(f bool) {
79+
l.isFramed = f
80+
}
81+
7382
func (l *LogTemplate) Labels() prometheus.Labels {
7483
return prometheus.Labels{
7584
"type": l.Format,

loggen/loggen.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ type LogGen struct {
5252
}
5353

5454
type LogGenRequest struct {
55-
Type string `json:"type"`
56-
Format string `json:"format"`
57-
Count int `json:"count"`
55+
Type string `json:"type"`
56+
Format string `json:"format"`
57+
Count int `json:"count"`
58+
Framing bool `json:"framing"`
5859
}
5960

6061
func New() *LogGen {
@@ -214,6 +215,10 @@ func (l *LogGen) processRequests() bool {
214215
continue
215216
}
216217

218+
if request.Framing {
219+
msg.SetFramed(true)
220+
}
221+
217222
logs = append(logs, msg)
218223
e = e.Next()
219224
}

loggen/writers.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func newStdoutWriter() LogWriter {
4040

4141
func (*StdoutLogWriter) Send(l log.Log) {
4242
msg, size := l.String()
43+
44+
if l.IsFramed() {
45+
msg = fmt.Sprintf("%d %s", len(msg), msg)
46+
}
47+
4348
fmt.Println(msg)
4449

4550
metrics.EventEmitted.With(l.Labels()).Inc()
@@ -67,7 +72,12 @@ func newNetworkWriter(network string, address string) LogWriter {
6772
func (nlw *NetworkLogWriter) Send(l log.Log) {
6873
msg, size := l.String()
6974

70-
msg += "\n"
75+
if l.IsFramed() {
76+
msg = fmt.Sprintf("%d %s", len(msg), msg)
77+
} else {
78+
msg += "\n"
79+
}
80+
7181
written := 0
7282
for {
7383
data := msg[written:]

0 commit comments

Comments
 (0)