-
Notifications
You must be signed in to change notification settings - Fork 2
/
gaelog.go
300 lines (256 loc) · 9.18 KB
/
gaelog.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
// Package gaelog provides easy Stackdriver Logging on Google App Engine Standard second generation runtimes.
package gaelog
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/logging"
"google.golang.org/genproto/googleapis/api/monitoredres"
)
const (
// DefaultLogID is the default log ID of the underlying Stackdriver Logging logger. Request
// logs are logged under the ID "request_log", so use "app_log" for consistency. To use a
// different ID create your logger with NewWithID.
DefaultLogID = "app_log"
// GAEAppResourceType is the type set on the logger's MonitoredResource for App Engine apps.
// This matches the type that App Engine itself assigns to request logs.
GAEAppResourceType = "gae_app"
// CloudRunResourceType is the type set on the logger's MonitoredResource for Cloud Run revisions.
// This matches the type that Cloud Run itself assigns to request logs.
CloudRunResourceType = "cloud_run_revision"
traceContextHeaderName = "X-Cloud-Trace-Context"
)
var (
metadataOnce sync.Once
metadataProjectID string
metadataProjectIDErr error
)
// projectIDFromMetadataService fetches the project ID from the metadata server,
// memoizing the result for use on all but the first call.
func projectIDFromMetadataService() (string, error) {
metadataOnce.Do(func() {
metadataProjectID, metadataProjectIDErr = metadata.ProjectID()
})
return metadataProjectID, metadataProjectIDErr
}
func traceID(projectID, trace string) string {
return fmt.Sprintf("projects/%s/traces/%s", projectID, trace)
}
type serviceInfo struct {
projectID string
resource *monitoredres.MonitoredResource
}
func newServiceInfo() (serviceInfo, error) {
// First try getting the project ID from the env var it's exposed as on App Engine.
gaeProjectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
if gaeProjectID != "" {
gaeService := os.Getenv("GAE_SERVICE")
gaeVersion := os.Getenv("GAE_VERSION")
if gaeService == "" || gaeVersion == "" {
return serviceInfo{}, fmt.Errorf("gaelog: $GOOGLE_CLOUD_PROJECT is set so $GAE_SERVICE and $GAE_VERSION are expected to be set, but one or both are not. Falling back to standard library log.")
}
return serviceInfo{
projectID: gaeProjectID,
resource: &monitoredres.MonitoredResource{
Labels: map[string]string{
"project_id": gaeProjectID,
"module_id": gaeService,
"version_id": gaeVersion,
},
Type: GAEAppResourceType,
},
}, nil
}
// Get and check the env vars expected to be set on Cloud Run.
crService := os.Getenv("K_SERVICE")
crRevision := os.Getenv("K_REVISION")
crConfiguration := os.Getenv("K_CONFIGURATION")
if crService == "" || crRevision == "" || crConfiguration == "" {
return serviceInfo{}, fmt.Errorf("gaelog: GAE env vars were not set so Cloud Run vars $K_SERVICE, $K_REVISION, and $K_CONFIGURATION are expected to be set, but one or more are not. Falling back to standard library log.")
}
// Finally, try the metadata service for the project ID.
crProjectID, err := projectIDFromMetadataService()
if err != nil {
return serviceInfo{}, err
}
return serviceInfo{
projectID: crProjectID,
resource: &monitoredres.MonitoredResource{
Labels: map[string]string{
"project_id": crProjectID,
"service_name": crService,
"revision_name": crRevision,
"configuration_name": crConfiguration,
},
Type: CloudRunResourceType,
},
}, nil
}
// A Logger logs messages to Stackdriver Logging (though in certain cases it may fall back to the
// standard library's "log" package; see New). Logs will be correlated with requests in Stackdriver.
type Logger struct {
client *logging.Client
logger *logging.Logger
monRes *monitoredres.MonitoredResource
trace string
}
// NewWithID creates a new Logger. The Logger is initialized using environment variables that are
// present on App Engine:
//
// • GOOGLE_CLOUD_PROJECT
// • GAE_SERVICE
// • GAE_VERSION
//
// If they are not present then it is initialized using environment variables present on Cloud Run:
//
// • K_SERVICE
// • K_REVISION
// • K_CONFIGURATION
// • Project ID is fetched from the metadata server, not an env var
//
// The given log ID will be passed through to the underlying Stackdriver Logging logger.
//
// Additionally, options (of type LoggerOption, from cloud.google.com/go/logging) will be passed
// through to the underlying Stackdriver Logging logger. Note that the option CommonResource will
// have no effect because the MonitoredResource is set when each log entry is made, thus overriding
// any value set with CommonResource. This is intended: much of the value of this package is in
// setting up the MonitoredResource so that log entries correlate with requests.
//
// The Logger will be valid in all cases, even when the error is non-nil. In the case of a non-nil
// error the Logger will fall back to the standard library's "log" package. There are three cases
// in which the error will be non-nil:
//
// 1. Any of the aforementioned environment variables are not set.
// 2. The given http.Request does not have the X-Cloud-Trace-Context header.
// 3. Initialization of the underlying Stackdriver Logging client produced an error.
func NewWithID(r *http.Request, logID string, options ...logging.LoggerOption) (*Logger, error) {
info, err := newServiceInfo()
if err != nil {
return &Logger{}, err
}
traceContext := r.Header.Get(traceContextHeaderName)
if traceContext == "" {
return &Logger{}, fmt.Errorf("gaelog: %s header is not set, falling back to standard library log", traceContextHeaderName)
}
client, err := logging.NewClient(r.Context(), fmt.Sprintf("projects/%s", info.projectID))
if err != nil {
return &Logger{}, err
}
return &Logger{
client: client,
logger: client.Logger(logID, options...),
monRes: info.resource,
trace: traceID(info.projectID, strings.Split(traceContext, "/")[0]),
}, nil
}
// New is identical to NewWithID with the exception that it uses the default log ID.
func New(r *http.Request, options ...logging.LoggerOption) (*Logger, error) {
return NewWithID(r, DefaultLogID, options...)
}
// Close closes the Logger, ensuring all logs are flushed and closing the underlying
// Stackdriver Logging client.
func (lg *Logger) Close() error {
if lg.client != nil {
return lg.client.Close()
}
return nil
}
// Logf logs with the given severity. Remaining arguments are handled in the manner of fmt.Printf.
func (lg *Logger) Logf(severity logging.Severity, format string, v ...interface{}) {
if lg.logger == nil {
log.Printf(format, v...)
return
}
lg.logger.Log(logging.Entry{
Timestamp: time.Now(),
Severity: severity,
Payload: fmt.Sprintf(format, v...),
Trace: lg.trace,
Resource: lg.monRes,
})
}
// Debugf calls Logf with debug severity.
func (lg *Logger) Debugf(format string, v ...interface{}) {
lg.Logf(logging.Debug, format, v...)
}
// Infof calls Logf with info severity.
func (lg *Logger) Infof(format string, v ...interface{}) {
lg.Logf(logging.Info, format, v...)
}
// Noticef calls Logf with notice severity.
func (lg *Logger) Noticef(format string, v ...interface{}) {
lg.Logf(logging.Notice, format, v...)
}
// Warningf calls Logf with warning severity.
func (lg *Logger) Warningf(format string, v ...interface{}) {
lg.Logf(logging.Warning, format, v...)
}
// Errorf calls Logf with error severity.
func (lg *Logger) Errorf(format string, v ...interface{}) {
lg.Logf(logging.Error, format, v...)
}
// Criticalf calls Logf with critical severity.
func (lg *Logger) Criticalf(format string, v ...interface{}) {
lg.Logf(logging.Critical, format, v...)
}
// Alertf calls Logf with alert severity.
func (lg *Logger) Alertf(format string, v ...interface{}) {
lg.Logf(logging.Alert, format, v...)
}
// Emergencyf calls Logf with emergency severity.
func (lg *Logger) Emergencyf(format string, v ...interface{}) {
lg.Logf(logging.Emergency, format, v...)
}
// Log logs with the given severity. v must be either a string, or something that
// marshals via the encoding/json package to a JSON object (and not any other type
// of JSON value).
func (lg *Logger) Log(severity logging.Severity, v interface{}) {
if lg.logger == nil {
log.Print(v)
return
}
lg.logger.Log(logging.Entry{
Timestamp: time.Now(),
Severity: severity,
Payload: v,
Trace: lg.trace,
Resource: lg.monRes,
})
}
// Debug calls Log with debug severity.
func (lg *Logger) Debug(v interface{}) {
lg.Log(logging.Debug, v)
}
// Info calls Log with info severity.
func (lg *Logger) Info(v interface{}) {
lg.Log(logging.Info, v)
}
// Notice calls Log with notice severity.
func (lg *Logger) Notice(v interface{}) {
lg.Log(logging.Notice, v)
}
// Warning calls Log with warning severity.
func (lg *Logger) Warning(v interface{}) {
lg.Log(logging.Warning, v)
}
// Error calls Log with error severity.
func (lg *Logger) Error(v interface{}) {
lg.Log(logging.Error, v)
}
// Critical calls Log with critical severity.
func (lg *Logger) Critical(v interface{}) {
lg.Log(logging.Critical, v)
}
// Alert calls Log with alert severity.
func (lg *Logger) Alert(v interface{}) {
lg.Log(logging.Alert, v)
}
// Emergency calls Log with emergency severity.
func (lg *Logger) Emergency(v interface{}) {
lg.Log(logging.Emergency, v)
}