-
Notifications
You must be signed in to change notification settings - Fork 487
/
formatter.go
132 lines (108 loc) · 3.92 KB
/
formatter.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
package gcplogtarget
// This code is copied from Promtail. The gcplogtarget package is used to
// configure and run the targets that can read log entries from cloud resource
// logs like bucket logs, load balancer logs, and Kubernetes cluster logs
// from GCP.
import (
"fmt"
"strings"
"time"
"github.com/grafana/loki/pkg/logproto"
json "github.com/json-iterator/go"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"github.com/grafana/agent/component/common/loki"
)
// GCPLogEntry that will be written to the pubsub topic according to the following spec.
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
type GCPLogEntry struct {
// why was this here?? nolint:revive
LogName string `json:"logName"`
Resource struct {
Type string `json:"type"`
Labels map[string]string `json:"labels"`
} `json:"resource"`
Timestamp string `json:"timestamp"`
// The time the log entry was received by Logging.
// Its important that `Timestamp` is optional in GCE log entry.
ReceiveTimestamp string `json:"receiveTimestamp"`
// Optional. The severity of the log entry. The default value is DEFAULT.
// DEFAULT, DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
Severity string `json:"severity"`
// Optional. A map of key, value pairs that provides additional information about the log entry.
// The labels can be user-defined or system-defined.
Labels map[string]string `json:"labels"`
TextPayload string `json:"textPayload"`
// NOTE(kavi): There are other fields on GCPLogEntry. but we need only need
// above fields for now anyway we will be sending the entire entry to Loki.
}
func parseGCPLogsEntry(data []byte, other model.LabelSet, otherInternal labels.Labels, useIncomingTimestamp bool, useFullLine bool, relabelConfig []*relabel.Config) (loki.Entry, error) {
var ge GCPLogEntry
if err := json.Unmarshal(data, &ge); err != nil {
return loki.Entry{}, err
}
// Adding mandatory labels for gcplog
lbs := labels.NewBuilder(otherInternal)
lbs.Set("__gcp_logname", ge.LogName)
lbs.Set("__gcp_resource_type", ge.Resource.Type)
lbs.Set("__gcp_severity", ge.Severity)
// resource labels from gcp log entry. Add it as internal labels
for k, v := range ge.Resource.Labels {
lbs.Set("__gcp_resource_labels_"+convertToLokiCompatibleLabel(k), v)
}
// labels from gcp log entry. Add it as internal labels
for k, v := range ge.Labels {
lbs.Set("__gcp_labels_"+convertToLokiCompatibleLabel(k), v)
}
var processed labels.Labels
// apply relabeling
if len(relabelConfig) > 0 {
processed, _ = relabel.Process(lbs.Labels(nil), relabelConfig...)
} else {
processed = lbs.Labels(nil)
}
// final labelset that will be sent to loki
labels := make(model.LabelSet)
for _, lbl := range processed {
// ignore internal labels
if strings.HasPrefix(lbl.Name, "__") {
continue
}
// ignore invalid labels
if !model.LabelName(lbl.Name).IsValid() || !model.LabelValue(lbl.Value).IsValid() {
continue
}
labels[model.LabelName(lbl.Name)] = model.LabelValue(lbl.Value)
}
// add labels coming from scrapeconfig
labels = labels.Merge(other)
ts := time.Now()
line := string(data)
if useIncomingTimestamp {
tt := ge.Timestamp
if tt == "" {
tt = ge.ReceiveTimestamp
}
var err error
ts, err = time.Parse(time.RFC3339, tt)
if err != nil {
return loki.Entry{}, fmt.Errorf("invalid timestamp format: %w", err)
}
if ts.IsZero() {
return loki.Entry{}, fmt.Errorf("no timestamp found in the log entry")
}
}
// Send only `ge.textPayload` as log line if its present and user don't explicitly ask for the whole log.
if !useFullLine && strings.TrimSpace(ge.TextPayload) != "" {
line = ge.TextPayload
}
return loki.Entry{
Labels: labels,
Entry: logproto.Entry{
Timestamp: ts,
Line: line,
},
}, nil
}