-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngcplogger.go
502 lines (450 loc) · 13 KB
/
ngcplogger.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"reflect"
"runtime"
"sync"
"sync/atomic"
"time"
"google.golang.org/api/option"
"github.com/docker/docker/daemon/logger"
"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/logging"
"cloud.google.com/go/logging/apiv2/loggingpb"
"github.com/containerd/log"
mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
)
const (
name = "ngcplogs"
projectOptKey = "gcp-project"
logLabelsKey = "labels"
logLabelsRegexKey = "labels-regex"
logEnvKey = "env"
logEnvRegexKey = "env-regex"
logCmdKey = "gcp-log-cmd"
logZoneKey = "gcp-meta-zone"
logNameKey = "gcp-meta-name"
logIDKey = "gcp-meta-id"
clientCredentialsFile = "credentials-file"
clientCredentialsJSON = "credentials-json"
)
var (
// The number of logs the gcplogs driver has dropped.
droppedLogs uint64
onGCE bool
// instance metadata populated from the metadata server if available
projectID string
zone string
instanceName string
instanceID string
severityFields = []string{
"severity",
"level",
}
timestampFields = []string{
"timestamp",
"time",
"ts",
}
)
func init() {
if err := logger.RegisterLogDriver(name, New); err != nil {
panic(err)
}
if err := logger.RegisterLogOptValidator(name, ValidateLogOpts); err != nil {
panic(err)
}
}
type nGCPLogger struct {
client *logging.Client
logger *logging.Logger
instance *instanceInfo
container *containerInfo
projectID string
extractJsonMessage bool
extractSeverity bool
excludeTimestamp bool
extractMsg bool
extractGcp bool
extractCaddy bool
}
type dockerLogEntry struct {
Instance *instanceInfo `json:"instance,omitempty"`
Container *containerInfo `json:"container,omitempty"`
Message string `json:"message,omitempty"`
}
type instanceInfo struct {
Zone string `json:"zone,omitempty"`
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
}
type containerInfo struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
ImageName string `json:"imageName,omitempty"`
ImageID string `json:"imageId,omitempty"`
Created time.Time `json:"created,omitempty"`
Command string `json:"command,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
var initGCPOnce sync.Once
func initGCP() {
initGCPOnce.Do(func() {
onGCE = metadata.OnGCE()
if onGCE {
// These will fail on instances if the metadata service is
// down or the client is compiled with an API version that
// has been removed. Since these are not vital, let's ignore
// them and make their fields in the dockerLogEntry ,omitempty
projectID, _ = metadata.ProjectID()
zone, _ = metadata.Zone()
instanceName, _ = metadata.InstanceName()
instanceID, _ = metadata.InstanceID()
}
})
}
// New creates a new logger that logs to Google Cloud Logging using the application
// default credentials.
//
// See https://developers.google.com/identity/protocols/application-default-credentials
func New(info logger.Info) (logger.Logger, error) {
initGCP()
var project string
if projectID != "" {
project = projectID
}
if projectID, found := info.Config[projectOptKey]; found {
project = projectID
}
if project == "" {
return nil, fmt.Errorf("no project was specified and couldn't read project from the metadata server. Please specify a project")
}
var opts []option.ClientOption
if credentialsFile, found := info.Config[clientCredentialsFile]; found {
opts = append(opts, option.WithCredentialsFile(fmt.Sprintf("/host/%s", credentialsFile)))
} else if credentialsJSON, found := info.Config[clientCredentialsJSON]; found {
opts = append(opts, option.WithCredentialsJSON([]byte(credentialsJSON)))
}
c, err := logging.NewClient(context.Background(), project, opts...)
if err != nil {
return nil, err
}
var instanceResource *instanceInfo
if onGCE {
instanceResource = &instanceInfo{
Zone: zone,
Name: instanceName,
ID: instanceID,
}
} else if info.Config[logZoneKey] != "" || info.Config[logNameKey] != "" || info.Config[logIDKey] != "" {
instanceResource = &instanceInfo{
Zone: info.Config[logZoneKey],
Name: info.Config[logNameKey],
ID: info.Config[logIDKey],
}
}
var options []logging.LoggerOption
if instanceResource != nil {
vmMrpb := logging.CommonResource(
&mrpb.MonitoredResource{
Type: "gce_instance",
Labels: map[string]string{
"instance_id": instanceResource.ID,
"zone": instanceResource.Zone,
},
},
)
options = []logging.LoggerOption{vmMrpb}
}
lg := c.Logger("ngcplogs-docker-driver", options...)
if err := c.Ping(context.Background()); err != nil {
return nil, fmt.Errorf("unable to connect or authenticate with Google Cloud Logging: %v", err)
}
extraAttributes, err := info.ExtraAttributes(nil)
if err != nil {
return nil, err
}
l := &nGCPLogger{
client: c,
logger: lg,
container: &containerInfo{
Name: info.ContainerName,
ID: info.ContainerID,
ImageName: info.ContainerImageName,
ImageID: info.ContainerImageID,
Created: info.ContainerCreated,
Metadata: extraAttributes,
},
projectID: project,
extractJsonMessage: true,
extractSeverity: true,
excludeTimestamp: false,
extractMsg: true,
extractGcp: false,
extractCaddy: false,
}
if info.Config[logCmdKey] == "true" {
l.container.Command = info.Command()
}
if info.Config["extract-json-message"] == "false" {
l.extractJsonMessage = false
}
if info.Config["extract-severity"] == "false" {
l.extractSeverity = false
}
if info.Config["exclude-timestamp"] == "true" {
l.excludeTimestamp = true
}
if info.Config["extract-msg"] == "false" {
l.extractMsg = false
}
if info.Config["extract-gcp"] == "true" {
l.extractGcp = true
}
if info.Config["extract-caddy"] == "true" {
l.extractCaddy = true
}
if instanceResource != nil {
l.instance = instanceResource
}
// The logger "overflows" at a rate of 10,000 logs per second and this
// overflow func is called. We want to surface the error to the user
// without overly spamming /var/log/docker.log so we log the first time
// we overflow and every 1000th time after.
c.OnError = func(err error) {
if errors.Is(err, logging.ErrOverflow) {
if i := atomic.AddUint64(&droppedLogs, 1); i%1000 == 1 {
log.G(context.TODO()).Errorf("ngcplogs driver has dropped %v logs", i)
}
} else {
log.G(context.TODO()).Error(err)
}
}
return l, nil
}
// ValidateLogOpts validates the opts passed to the ngcplogs driver. Currently, the ngcplogs
// driver doesn't take any arguments.
func ValidateLogOpts(cfg map[string]string) error {
for k := range cfg {
switch k {
case projectOptKey, logLabelsKey, logLabelsRegexKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
default:
return fmt.Errorf("%q is not a valid option for the ngcplogs driver", k)
}
}
return nil
}
func (l *nGCPLogger) Log(lMsg *logger.Message) error {
logLine := lMsg.Line
ts := lMsg.Timestamp
logger.PutMessage(lMsg)
if len(logLine) > 0 {
var payload any
entry := logging.Entry{
Labels: map[string]string{},
Timestamp: ts,
Severity: logging.Default,
}
if l.extractJsonMessage && logLine[0] == '{' && logLine[len(logLine)-1] == '}' {
var m map[string]any
err := json.Unmarshal(logLine, &m)
if err != nil {
payload = fmt.Sprintf("Error parsing JSON: %s", string(logLine))
entry.Severity = logging.Critical
} else {
entry.Severity = l.extractSeverityFromPayload(m)
l.excludeTimestampFromPayload(m)
l.extractMsgFromPayload(m)
m["instance"] = l.instance
m["container"] = l.container
l.extractGcpFromPayload(m, &entry)
l.extractCaddyFromPayload(m, &entry)
payload = m
}
} else {
payload = dockerLogEntry{
Instance: l.instance,
Container: l.container,
Message: string(logLine),
}
}
entry.Payload = payload
l.logger.Log(entry)
}
return nil
}
func (l *nGCPLogger) extractSeverityFromPayload(m map[string]any) logging.Severity {
severity := logging.Default
if l.extractSeverity {
for _, severityField := range severityFields {
if rawSeverity, exists := m[severityField]; exists {
// check for some zap severity levels and translate
switch rawSeverity {
case "warn":
rawSeverity = "warning"
case "dpanic":
fallthrough
case "panic":
rawSeverity = "critical"
case "fatal":
rawSeverity = "alert"
}
if parsedSeverity, isString := rawSeverity.(string); isString {
severity = logging.ParseSeverity(parsedSeverity)
if severity != logging.Default { // severity was parsed correctly, we can remove it from the jsonPayload section
delete(m, severityField)
}
break
}
if parsedSeverity, isNumber := rawSeverity.(float64); isNumber {
severity = logging.Severity(parsedSeverity)
if severity != logging.Default { // severity was parsed correctly, we can remove it from the jsonPayload section
delete(m, severityField)
}
break
}
}
}
}
return severity
}
func (l *nGCPLogger) excludeTimestampFromPayload(m map[string]any) {
if l.excludeTimestamp {
for _, timestampField := range timestampFields {
if _, exists := m[timestampField]; exists {
delete(m, timestampField)
}
}
}
}
func (l *nGCPLogger) extractMsgFromPayload(m map[string]any) {
if l.extractMsg {
if msg, exists := m["msg"]; exists {
m["message"] = msg
delete(m, "msg")
}
}
}
func assertOrLog[T any](val any) T {
var v T
if val == nil {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "unknown"
}
slog.Error("unexpected nil value", "file", file, "line", line)
} else {
var ok bool
v, ok = val.(T)
if !ok {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "unknown"
}
slog.Error("unexpected type", "want", reflect.TypeOf(v).String(), "got", reflect.TypeOf(val).String(), "file", file, "line", line)
}
}
return v
}
func (l *nGCPLogger) extractGcpFromPayload(m map[string]any, entry *logging.Entry) {
if l.extractGcp {
if val, exists := m["logging.googleapis.com/sourceLocation"]; exists {
v := assertOrLog[map[string]any](val)
entry.SourceLocation = &loggingpb.LogEntrySourceLocation{
File: assertOrLog[string](v["file"]),
Line: int64(assertOrLog[float64](v["line"])),
Function: assertOrLog[string](v["function"]),
}
delete(m, "logging.googleapis.com/sourceLocation")
}
if val, exists := m["logging.googleapis.com/trace"]; exists {
entry.Trace = assertOrLog[string](val)
delete(m, "logging.googleapis.com/trace")
}
if val, exists := m["logging.googleapis.com/spanId"]; exists {
entry.SpanID = assertOrLog[string](val)
delete(m, "logging.googleapis.com/spanId")
}
if val, exists := m["logging.googleapis.com/trace_sampled"]; exists {
entry.TraceSampled = assertOrLog[bool](val)
delete(m, "logging.googleapis.com/trace_sampled")
}
if val, exists := m["logging.googleapis.com/labels"]; exists {
v := assertOrLog[map[string]any](val)
for k, v := range v {
entry.Labels[k] = assertOrLog[string](v)
}
delete(m, "logging.googleapis.com/labels")
}
}
}
func (l *nGCPLogger) extractCaddyFromPayload(m map[string]any, entry *logging.Entry) {
if l.extractCaddy {
if val, exists := m["request"]; exists {
hr := logging.HTTPRequest{
Request: &http.Request{
Header: make(http.Header),
},
}
v := assertOrLog[map[string]any](val)
hr.Request.Method = assertOrLog[string](v["method"])
_, isTLS := v["tls"]
var h = "http"
if isTLS {
h = "https"
}
hr.Request.URL = &url.URL{
Scheme: h,
Host: assertOrLog[string](v["host"]),
RawPath: assertOrLog[string](v["uri"]),
Path: assertOrLog[string](v["uri"]),
}
if t, ok := m["bytes_read"]; ok {
hr.RequestSize = int64(assertOrLog[float64](t))
}
if t, ok := m["status"]; ok {
hr.Status = int(assertOrLog[float64](t))
}
if t, ok := m["size"]; ok {
hr.ResponseSize = int64(assertOrLog[float64](t))
}
if t, ok := m["duration"]; ok {
hr.Latency = time.Duration(assertOrLog[float64](t) * float64(time.Second))
}
hr.Request.Proto = assertOrLog[string](v["proto"])
hr.RemoteIP = assertOrLog[string](v["remote_ip"]) + ":" + assertOrLog[string](v["remote_port"])
if t, ok := v["headers"]; ok {
headers := assertOrLog[map[string]any](t)
for h, v := range headers {
for _, s := range assertOrLog[[]any](v) {
hr.Request.Header.Add(h, assertOrLog[string](s))
}
}
}
entry.HTTPRequest = &hr
//Caddy request contains more data, don't
//delete.
//delete(m, "request")
}
if val, exists := m["traceID"]; exists {
entry.Trace = "projects/" + l.projectID + "/traces/" + val.(string)
delete(m, "traceID")
}
}
}
func (l *nGCPLogger) Close() error {
err := l.logger.Flush()
if err != nil {
return err
}
return l.client.Close()
}
func (l *nGCPLogger) Name() string {
return name
}