-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
285 lines (221 loc) · 6.84 KB
/
handler.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2024 The original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gslog
import (
"context"
"fmt"
"log/slog"
"os"
"runtime"
"slices"
"cloud.google.com/go/logging"
logpb "cloud.google.com/go/logging/apiv2/loggingpb"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
spb "google.golang.org/protobuf/types/known/structpb"
"m4o.io/gslog/internal/attr"
"m4o.io/gslog/internal/level"
"m4o.io/gslog/internal/options"
)
const (
// MessageKey is the key used for the message of the log call, per Google
// Cloud Logging. The associated value is a string.
MessageKey = "message"
)
// GcpHandler is a Google Cloud Logging backed slog handler.
type GcpHandler struct {
// *logging.Logger, except for testing
log Logger
level slog.Leveler
// addSource causes the handler to compute the source code position
// of the log statement and add a SourceKey attribute to the output.
addSource bool
entryAugmentors []options.EntryAugmentor
replaceAttr attr.Mapper
payload *spb.Struct
groups []string
}
var _ slog.Handler = (*GcpHandler)(nil)
// NewGcpHandler creates a Google Cloud Logging backed log.Logger.
func NewGcpHandler(logger Logger, opts ...options.OptionProcessor) *GcpHandler {
if logger == nil {
panic("client is nil")
}
o := options.ApplyOptions(opts...)
return newGcpLoggerWithOptions(logger, o)
}
func newGcpLoggerWithOptions(logger Logger, opts *options.Options) *GcpHandler {
handler := &GcpHandler{
log: logger,
level: opts.Level,
addSource: opts.AddSource,
entryAugmentors: opts.EntryAugmentors,
replaceAttr: attr.WrapAttrMapper(opts.ReplaceAttr),
payload: &spb.Struct{Fields: make(map[string]*spb.Value)},
groups: nil,
}
return handler
}
// WithLeveler returns a copy of the handler, provisioned with the supplied
// leveler.
func (h *GcpHandler) WithLeveler(leveler slog.Leveler) *GcpHandler {
if leveler == nil {
panic("Leveler is nil")
}
h2 := h.clone()
h2.level = leveler
return h2
}
// Enabled reports whether the handler handles records at the given level.
// The handler ignores records whose level is lower.
func (h *GcpHandler) Enabled(_ context.Context, level slog.Level) bool {
return h.level.Level() <= level
}
// Handle will handle a slog.Record, as described in the interface's
// documentation. It will translate the slog.Record into a logging.Entry
// that's filled with a *spb.Value as an Entry Payload.
func (h *GcpHandler) Handle(ctx context.Context, record slog.Record) error {
//nolint:forcetypeassert
payload2 := proto.Clone(h.payload).(*spb.Struct)
if payload2.Fields == nil {
payload2.Fields = make(map[string]*spb.Value)
}
setAndClean(h.groups, payload2, func(_ []string, payload *spb.Struct) {
record.Attrs(func(a slog.Attr) bool {
if h.replaceAttr != nil {
a = h.replaceAttr(h.groups, a)
}
attr.DecorateWith(payload, a)
return true
})
})
a := slog.String(MessageKey, record.Message)
if h.replaceAttr != nil {
a = h.replaceAttr(nil, a)
}
attr.DecorateWith(payload2, a)
var entry logging.Entry
entry.Payload = payload2
entry.Timestamp = record.Time.UTC()
entry.Severity = level.ToSeverity(record.Level)
if h.addSource {
addSourceLocation(&entry, &record)
}
for _, b := range h.entryAugmentors {
b(ctx, &entry, h.groups)
}
labelsEntryAugmentorFrom(ctx)(ctx, &entry, h.groups)
if entry.Severity >= logging.Critical {
err := h.log.LogSync(ctx, entry)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error logging: %s\n%s", record.Message, err)
}
} else {
h.log.Log(entry)
}
return nil
}
// WithAttrs returns a copy of the handler whose attributes consists
// of h's attributes followed by attrs.
func (h *GcpHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
handler2 := h.clone()
current := fromPath(handler2.payload, handler2.groups)
for _, a := range attrs {
if h.replaceAttr != nil {
a = h.replaceAttr(h.groups, a)
}
attr.DecorateWith(current, a)
}
return handler2
}
// WithGroup returns a copy of the handler with the given group
// appended to the receiver's existing groups.
func (h *GcpHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
handler2 := h.clone()
//nolint:forcetypeassert
payload2 := proto.Clone(h.payload).(*spb.Struct)
handler2.payload = payload2
current := fromPath(handler2.payload, handler2.groups)
current.Fields[name] = &spb.Value{
Kind: &spb.Value_StructValue{
StructValue: &spb.Struct{
Fields: make(map[string]*spb.Value),
},
},
}
handler2.groups = h.groups
handler2.groups = append(handler2.groups, name)
return handler2
}
// Flush blocks until all currently buffered log entries are sent.
//
// If any errors occurred since the last call to Flush from any Logger, or the
// creation of the client if this is the first call, then Flush returns a non-nil
// error with summary information about the errors. This information is unlikely to
// be actionable. For more accurate error reporting, set Client.OnError.
func (h *GcpHandler) Flush() error {
if err := h.log.Flush(); err != nil {
return errors.Wrap(err, "failed to flush handler")
}
return nil
}
func (h *GcpHandler) clone() *GcpHandler {
//nolint:forcetypeassert
payload2 := proto.Clone(h.payload).(*spb.Struct)
return &GcpHandler{
log: h.log,
level: h.level,
addSource: h.addSource,
entryAugmentors: h.entryAugmentors,
replaceAttr: h.replaceAttr,
payload: payload2,
groups: slices.Clip(h.groups),
}
}
func addSourceLocation(e *logging.Entry, r *slog.Record) {
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
e.SourceLocation = &logpb.LogEntrySourceLocation{
File: f.File,
Line: int64(f.Line),
Function: f.Function,
}
}
func fromPath(payload *spb.Struct, path []string) *spb.Struct {
for _, k := range path {
payload = payload.GetFields()[k].GetStructValue()
}
if payload.Fields == nil {
payload.Fields = make(map[string]*spb.Value)
}
return payload
}
func setAndClean(groups []string, payload *spb.Struct, decorate func(groups []string, payload *spb.Struct)) {
if len(groups) == 0 {
if payload.Fields == nil {
payload.Fields = make(map[string]*spb.Value)
}
decorate(groups, payload)
return
}
group := groups[0]
s := payload.GetFields()[group].GetStructValue()
setAndClean(groups[1:], s, decorate)
if len(s.GetFields()) == 0 {
delete(payload.GetFields(), group)
}
}