-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream-stackdriver.go
116 lines (107 loc) · 3.06 KB
/
stream-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
package logger
import (
"context"
"encoding/json"
"os"
"sync"
"time"
"cloud.google.com/go/logging"
"github.com/gildas/go-errors"
googleoption "google.golang.org/api/option"
)
// GCPStream is the Stream that writes to the standard output
type StackDriverStream struct {
LogID string
Parent string
KeyFilename string
Key interface{}
Converter Converter
FilterLevel Level
mutex sync.Mutex
client *logging.Client
target *logging.Logger
}
// SetFilterLevel sets the filter level
func (stream *StackDriverStream) SetFilterLevel(level Level) Streamer {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevel = level
return stream
}
// Write writes the given Record
func (stream *StackDriverStream) Write(record Record) (err error) {
// implements logger.Stream
stream.mutex.Lock()
defer stream.mutex.Unlock()
if stream.client == nil {
ctx := context.Background()
if len(stream.Parent) == 0 {
projectID, ok := os.LookupEnv("GOOGLE_PROJECT_ID")
if !ok {
return errors.EnvironmentMissingError.WithWhat("GOOGLE_PROJECT_ID")
}
stream.Parent = "projects/" + projectID
}
stream.Converter = &StackDriverConverter{}
options := []googleoption.ClientOption{}
if stream.Key != nil {
payload, err := json.Marshal(stream.Key)
if err != nil {
return errors.JSONMarshalError.Wrap(err)
}
options = append(options, googleoption.WithCredentialsJSON(payload))
} else if len(stream.KeyFilename) != 0 {
options = append(options, googleoption.WithCredentialsFile(stream.KeyFilename))
}
// if Key and KeyFilename are not provided, $GOOGLE_APPLICATION_CREDENTIALS is used.
stream.client, err = logging.NewClient(ctx, stream.Parent, options...)
if err != nil {
return errors.WithStack(err)
}
stream.target = stream.client.Logger(stream.LogID)
if stream.FilterLevel == 0 {
stream.FilterLevel = GetLevelFromEnvironment()
}
}
grecord := stream.Converter.Convert(record)
stamp, _ := time.Parse(time.RFC3339, grecord["time"].(string))
severity := grecord["severity"].(logging.Severity)
delete(grecord, "time")
delete(grecord, "severity")
stream.target.Log(logging.Entry{
Timestamp: stamp,
Severity: severity,
Payload: grecord,
})
return nil
}
// ShouldWrite tells if the given level should be written to this stream
func (stream *StackDriverStream) ShouldWrite(level Level) bool {
// implements logger.Stream
return level.ShouldWrite(stream.FilterLevel)
}
// Flush flushes the stream (makes sure records are actually written)
func (stream *StackDriverStream) Flush() {
// implements logger.Stream
if stream.target != nil {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.target.Flush()
}
}
// Close closes the stream
func (stream *StackDriverStream) Close() {
stream.mutex.Lock()
defer stream.mutex.Unlock()
if stream.target != nil {
stream.target.Flush()
}
if stream.client != nil {
stream.client.Close()
}
}
// String gets a string version
func (stream *StackDriverStream) String() string {
// implements the fmt.Stringer interface
return "Stream to Google StackDriver"
}