-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathlogs.go
219 lines (190 loc) · 7.18 KB
/
logs.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
package google_cloudsql
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"sync"
"time"
"cloud.google.com/go/pubsub"
"google.golang.org/api/option"
"github.com/pganalyze/collector/config"
"github.com/pganalyze/collector/logs"
"github.com/pganalyze/collector/state"
"github.com/pganalyze/collector/util"
)
type googleLogResource struct {
ResourceType string `json:"type"`
Labels map[string]string `json:"labels"`
}
type googleLogMessage struct {
InsertID string `json:"insertId"`
LogName string `json:"logName"`
ReceiveTimestamp string `json:"receiveTimestamp"`
Resource googleLogResource `json:"resource"`
Severity string `json:"severity"`
TextPayload string `json:"textPayload"`
Timestamp string `json:"timestamp"`
Labels map[string]string `json:"labels"`
}
type LogStreamItem struct {
GcpProjectID string
GcpCloudSQLInstanceID string
GcpAlloyDBClusterID string
GcpAlloyDBInstanceID string
OccurredAt time.Time
Content string
}
func setupPubSubSubscriber(ctx context.Context, wg *sync.WaitGroup, logger *util.Logger, config config.ServerConfig, gcpLogStream chan LogStreamItem) error {
if strings.Count(config.GcpPubsubSubscription, "/") != 3 {
return fmt.Errorf("Unsupported subscription format - must be \"projects/PROJECT_NAME/subscriptions/SUBSCRIPTION_NAME\", got: %s", config.GcpPubsubSubscription)
}
idParts := strings.SplitN(config.GcpPubsubSubscription, "/", 4)
projectID := idParts[1]
subID := idParts[3]
var opts []option.ClientOption
if config.GcpCredentialsFile != "" {
logger.PrintVerbose("Using GCP credentials file located at: %s", config.GcpCredentialsFile)
opts = append(opts, option.WithCredentialsFile(config.GcpCredentialsFile))
} else {
logger.PrintVerbose("No GCP credentials file provided; assuming GKE workload identity or VM-associated service account")
}
client, err := pubsub.NewClient(ctx, projectID, opts...)
if err != nil {
return fmt.Errorf("Failed to create Google PubSub client: %v", err)
}
sub := client.Subscription(subID)
go func(ctx context.Context, wg *sync.WaitGroup, logger *util.Logger, sub *pubsub.Subscription) {
wg.Add(1)
for {
logger.PrintVerbose("Initializing Google Pub/Sub handler")
err := sub.Receive(ctx, func(ctx context.Context, pubsubMsg *pubsub.Message) {
pubsubMsg.Ack()
var msg googleLogMessage
err = json.Unmarshal(pubsubMsg.Data, &msg)
if err != nil {
logger.PrintError("Error parsing JSON: %s", err)
return
}
if msg.Resource.ResourceType == "cloudsql_database" {
if !strings.HasSuffix(msg.LogName, "postgres.log") {
return
}
databaseID, ok := msg.Resource.Labels["database_id"]
if !ok || strings.Count(databaseID, ":") != 1 {
return
}
parts := strings.SplitN(databaseID, ":", 2) // project_id:instance_id
t, _ := time.Parse(time.RFC3339Nano, msg.Timestamp)
gcpLogStream <- LogStreamItem{
GcpProjectID: parts[0],
GcpCloudSQLInstanceID: parts[1],
Content: msg.TextPayload,
OccurredAt: t,
}
return
} else if msg.Resource.ResourceType == "alloydb.googleapis.com/Instance" {
if !strings.HasSuffix(msg.LogName, "postgres.log") {
return
}
clusterID, ok := msg.Resource.Labels["cluster_id"]
if !ok {
return
}
instanceID, ok := msg.Resource.Labels["instance_id"]
if !ok {
return
}
projectID, ok := msg.Labels["CONSUMER_PROJECT"]
if !ok {
return
}
t, _ := time.Parse(time.RFC3339Nano, msg.Timestamp)
gcpLogStream <- LogStreamItem{
GcpProjectID: projectID,
GcpAlloyDBClusterID: clusterID,
GcpAlloyDBInstanceID: instanceID,
Content: msg.TextPayload,
OccurredAt: t,
}
return
}
})
if err == nil || err == context.Canceled {
break
}
logger.PrintError("Failed to receive from Google PubSub, retrying in 1 minute: %v", err)
time.Sleep(1 * time.Minute)
}
wg.Done()
}(ctx, wg, logger, sub)
return nil
}
func SetupLogSubscriber(ctx context.Context, wg *sync.WaitGroup, globalCollectionOpts state.CollectionOpts, logger *util.Logger, servers []*state.Server, parsedLogStream chan state.ParsedLogStreamItem) error {
gcpLogStream := make(chan LogStreamItem, state.LogStreamBufferLen)
setupLogTransformer(ctx, wg, servers, gcpLogStream, parsedLogStream, logger)
// This map is used to avoid duplicate receivers to the same subscriber
gcpPubSubHandlers := make(map[string]bool)
for _, server := range servers {
prefixedLogger := logger.WithPrefix(server.Config.SectionName)
if server.Config.GcpPubsubSubscription != "" {
_, ok := gcpPubSubHandlers[server.Config.GcpPubsubSubscription]
if ok {
continue
}
err := setupPubSubSubscriber(ctx, wg, prefixedLogger, server.Config, gcpLogStream)
if err != nil {
if globalCollectionOpts.TestRun {
return err
}
prefixedLogger.PrintWarning("Skipping logs, could not setup log subscriber: %s", err)
continue
}
gcpPubSubHandlers[server.Config.GcpPubsubSubscription] = true
}
}
return nil
}
func setupLogTransformer(ctx context.Context, wg *sync.WaitGroup, servers []*state.Server, in <-chan LogStreamItem, out chan state.ParsedLogStreamItem, logger *util.Logger) {
wg.Add(1)
go func() {
defer wg.Done()
// Only ingest log lines that were written in the last minute before startup
linesNewerThan := time.Now().Add(-1 * time.Minute)
for {
select {
case <-ctx.Done():
return
case in, ok := <-in:
if !ok {
return
}
// We ignore failures here since we want the per-backend stitching logic
// that runs later on (and any other parsing errors will just be ignored).
// Note that we need to restore the original trailing newlines since
// AnalyzeStreamInGroups expects them and they are not present in the GCP
// log stream.
logLine, _ := logs.ParseLogLineWithPrefix("", in.Content+"\n", nil)
logLine.OccurredAt = in.OccurredAt
// Ignore loglines which are outside our time window
if !logLine.OccurredAt.IsZero() && logLine.OccurredAt.Before(linesNewerThan) {
continue
}
for _, server := range servers {
if in.GcpProjectID == server.Config.GcpProjectID && in.GcpCloudSQLInstanceID != "" && in.GcpCloudSQLInstanceID == server.Config.GcpCloudSQLInstanceID {
out <- state.ParsedLogStreamItem{Identifier: server.Config.Identifier, LogLine: logLine}
}
if in.GcpProjectID == server.Config.GcpProjectID && in.GcpAlloyDBClusterID != "" && in.GcpAlloyDBClusterID == server.Config.GcpAlloyDBClusterID && in.GcpAlloyDBInstanceID != "" && in.GcpAlloyDBInstanceID == server.Config.GcpAlloyDBInstanceID {
// AlloyDB adds a special [filename:lineno] prefix to all log lines (not part of log_line_prefix)
parts := regexp.MustCompile(`(?s)^\[[\w.-]+:\d+\] (.*)`).FindStringSubmatch(string(logLine.Content))
if len(parts) == 2 {
logLine.Content = parts[1]
}
out <- state.ParsedLogStreamItem{Identifier: server.Config.Identifier, LogLine: logLine}
}
}
}
}
}()
}