-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parquet.go
161 lines (133 loc) · 2.89 KB
/
parquet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package slogparquet
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"math"
"sort"
"sync"
"time"
"github.com/gofrs/uuid"
"github.com/parquet-go/parquet-go"
"github.com/samber/lo"
"github.com/thanos-io/objstore"
)
type ParquetBuffer interface {
Append(time.Time, slog.Level, string, map[string]any) error
Flush(bool) error
}
type parquetBuffer struct {
prefix string
id string
maxRecords int
maxInterval time.Duration
rowGroupSize int
mutex sync.Mutex // ☢️
bucket objstore.Bucket
start time.Time
buffer *parquet.GenericBuffer[log]
}
func NewParquetBuffer(bucket objstore.Bucket, prefix string, maxRecords int, maxInterval time.Duration) ParquetBuffer {
return &parquetBuffer{
prefix: prefix,
id: lo.Must(uuid.NewV4()).String()[0:5],
maxRecords: maxRecords,
maxInterval: maxInterval,
rowGroupSize: int(math.Ceil(float64(maxRecords) / 10)),
mutex: sync.Mutex{},
bucket: bucket,
start: time.Now(),
buffer: resetBuffer(),
}
}
func (b *parquetBuffer) Append(tIme time.Time, logLevel slog.Level, message string, attributes map[string]any) error {
// bearer:disable go_lang_deserialization_of_user_input
serializedAttrs, err := json.Marshal(attributes)
if err != nil {
return err
}
b.mutex.Lock()
_, err = b.buffer.Write([]log{
{
Time: tIme,
LogLevel: logLevel.String(),
Message: message,
Attributes: serializedAttrs,
Source: library,
},
})
if err != nil {
return err
}
if b.buffer.Len() >= b.maxRecords || b.start.Add(b.maxInterval).Before(time.Now()) {
b.mutex.Unlock()
return b.Flush(false)
}
b.mutex.Unlock()
return nil
}
func (b *parquetBuffer) Flush(sync bool) error {
if sync {
return b.flush()
}
go b.flush()
return nil
}
func (b *parquetBuffer) flush() error {
b.mutex.Lock()
buffer := b.buffer
b.start = time.Now()
b.buffer = resetBuffer()
b.mutex.Unlock()
if buffer.Len() == 0 {
return nil
}
sort.Sort(buffer)
buff := bytes.NewBuffer(nil)
writer := parquet.NewGenericWriter[log](
buff,
parquet.CreatedBy(goModulePath, semver, buildSHA1),
parquet.Compression(&parquet.Lz4Raw),
parquet.MaxRowsPerRowGroup(int64(b.rowGroupSize)),
parquet.BloomFilters(
parquet.SplitBlockFilter(4, "log_level"),
),
)
_, err := parquet.CopyRows(writer, buffer.Rows())
if err != nil {
return err
}
err = writer.Close()
if err != nil {
return err
}
err = writer.Flush()
if err != nil {
return err
}
key := fmt.Sprintf(
"%s/%s/%s.%s.parquet",
b.prefix,
b.start.UTC().Format("2006-01-02"),
b.start.UTC().Format("15:04:05"),
b.id,
)
return b.bucket.Upload(
context.Background(),
key,
buff,
)
}
func resetBuffer() *parquet.GenericBuffer[log] {
return parquet.NewGenericBuffer[log](
parquet.SchemaOf(new(log)),
parquet.ColumnBufferCapacity(16384),
parquet.SortingRowGroupConfig(
parquet.SortingColumns(
parquet.Ascending("time"),
),
),
)
}