Skip to content

Commit c50f044

Browse files
authored
[*] change json sink to use one global encoder (#772)
add *json.Encoder property to 'JSONWriter' struct instead of recreating it on each write.
1 parent fb33d09 commit c50f044

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

internal/sinks/json.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
type JSONWriter struct {
1818
ctx context.Context
1919
lw *lumberjack.Logger
20+
enc *json.Encoder
2021
}
2122

2223
func NewJSONWriter(ctx context.Context, fname string) (*JSONWriter, error) {
@@ -26,6 +27,7 @@ func NewJSONWriter(ctx context.Context, fname string) (*JSONWriter, error) {
2627
ctx: ctx,
2728
lw: &lumberjack.Logger{Filename: fname, Compress: true},
2829
}
30+
jw.enc = json.NewEncoder(jw.lw)
2931
go jw.watchCtx()
3032
return jw, nil
3133
}
@@ -37,7 +39,6 @@ func (jw *JSONWriter) Write(msg metrics.MeasurementEnvelope) error {
3739
if len(msg.Data) == 0 {
3840
return nil
3941
}
40-
enc := json.NewEncoder(jw.lw)
4142
t1 := time.Now()
4243
written := 0
4344

@@ -47,7 +48,7 @@ func (jw *JSONWriter) Write(msg metrics.MeasurementEnvelope) error {
4748
"dbname": msg.DBName,
4849
"custom_tags": msg.CustomTags,
4950
}
50-
if err := enc.Encode(dataRow); err != nil {
51+
if err := jw.enc.Encode(dataRow); err != nil {
5152
return err
5253
}
5354
written += len(msg.Data)

0 commit comments

Comments
 (0)