-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_stackdriver.go
131 lines (111 loc) · 3.32 KB
/
encoder_stackdriver.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
package clogger
import (
"bytes"
"context"
"encoding/json"
"fmt"
"sync"
"time"
"cloud.google.com/go/compute/metadata"
)
var (
projectID = ""
once sync.Once
)
func init() {
once.Do(func() {
pID, err := metadata.ProjectID()
if err != nil {
Errorf(context.Background(), "Unable to extract the GCP project ID from the env – your logs might not be displayed correctly in Stackdriver")
}
projectID = pID
})
}
/*
https://cloud.google.com/logging/docs/structured-logging
{
"severity":"ERROR",
"message":"There was an error in the application.",
"httpRequest":{
"requestMethod":"GET"
},
"time":"2020-10-12T07:20:50.52Z",
"logging.googleapis.com/labels":{
"user_label_1":"value_1",
"user_label_2":"value_2"
},
"logging.googleapis.com/sourceLocation":{
"file":"get_data.py",
"line":"142",
"function":"getData"
},
"logging.googleapis.com/spanId":"000000000000004a",
"logging.googleapis.com/trace":"projects/my-projectid/traces/06796866738c859f2f19b7cfb3214824",
"logging.googleapis.com/trace_sampled":false
}
*/
type StackdriverEncoder struct{}
func (j *StackdriverEncoder) EncodeLogEntry(entry *LogEntry) ([]byte, error) {
fields := make(map[string]interface{})
for _, field := range entry.fields.fields() {
switch field.key {
case opencensusSpanID, otelSpanID:
fields["logging.googleapis.com/spanId"] = field.value
case opencensusTraceID, otelTraceID:
// "projects/my-projectid/traces/06796866738c859f2f19b7cfb3214824"
// "projects/_my-project-id_/traces/_trace-id_"
fields["logging.googleapis.com/trace"] = fmt.Sprintf("projects/%s/traces/%s", projectID, field.value)
case opencensusSampled, otelSampled:
fields["logging.googleapis.com/trace_sampled"] = field.value
default:
fields[field.key] = field.value
}
}
// TODO: sourceLocation
fields["message"] = entry.message
fields["timestamp"] = entry.timestamp.Format(time.RFC3339Nano)
fields["severity"] = entry.severity.String()
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(fields)
return buf.Bytes(), err
}
func (j *StackdriverEncoder) EncodeEvent(event *Event) ([]byte, error) {
fields := make(map[string]interface{})
// Fields
for _, field := range event.fields.fields() {
switch field.key {
case opencensusSpanID, otelSpanID:
fields["logging.googleapis.com/spanId"] = field.value
v, _ := field.value.(string)
fields["logging.googleapis.com/trace_sampled"] = v != ""
case opencensusTraceID, otelTraceID:
fields["logging.googleapis.com/trace"] = field.value
v, _ := field.value.(string)
fields["logging.googleapis.com/trace_sampled"] = v != ""
default:
fields["fields"] = map[string]interface{}{
field.key: field.value,
}
}
}
// Err fields
for _, field := range event.errFields.fields() {
fields["errors"] = map[string]interface{}{
field.key: field.value,
}
}
// Labels
for _, field := range event.labels.fields() {
fields["logging.googleapis.com/labels"] = map[string]interface{}{
field.key: field.value,
}
}
// TODO: sourceLocation
fields["message"] = event.message
fields["timestamp"] = event.timestamp.Format(time.RFC3339Nano)
fields["latencySeconds"] = time.Since(event.timestamp).String()
fields["severity"] = event.severity.String()
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(fields)
return buf.Bytes(), err
}