-
Notifications
You must be signed in to change notification settings - Fork 487
/
push_target.go
157 lines (138 loc) · 5.04 KB
/
push_target.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
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 (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/relabel"
"github.com/grafana/agent/component/common/loki"
fnet "github.com/grafana/agent/component/common/net"
)
// PushTarget defines a server for receiving messages from a GCP PubSub push
// subscription.
type PushTarget struct {
logger log.Logger
jobName string
metrics *Metrics
config *PushConfig
entries chan<- loki.Entry
handler loki.EntryHandler
relabelConfigs []*relabel.Config
server *fnet.TargetServer
}
// NewPushTarget constructs a PushTarget.
func NewPushTarget(metrics *Metrics, logger log.Logger, handler loki.EntryHandler, jobName string, config *PushConfig, relabel []*relabel.Config, reg prometheus.Registerer) (*PushTarget, error) {
wrappedLogger := log.With(logger, "component", "gcp_push")
srv, err := fnet.NewTargetServer(wrappedLogger, jobName+"_push_target", reg, config.Server)
if err != nil {
return nil, fmt.Errorf("failed to create loki http server: %w", err)
}
pt := &PushTarget{
server: srv,
logger: wrappedLogger,
jobName: jobName,
metrics: metrics,
config: config,
entries: handler.Chan(),
handler: handler,
relabelConfigs: relabel,
}
err = pt.server.MountAndRun(func(router *mux.Router) {
router.Path("/gcp/api/v1/push").Methods("POST").Handler(http.HandlerFunc(pt.push))
})
if err != nil {
return nil, err
}
return pt, nil
}
func (p *PushTarget) push(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
// Create no-op context.WithTimeout returns to simplify logic
ctx := r.Context()
cancel := context.CancelFunc(func() {})
if p.config.PushTimeout != 0 {
ctx, cancel = context.WithTimeout(r.Context(), p.config.PushTimeout)
}
defer cancel()
pushMessage := PushMessage{}
bs, err := io.ReadAll(r.Body)
if err != nil {
p.metrics.gcpPushErrors.WithLabelValues("read_error").Inc()
level.Warn(p.logger).Log("msg", "failed to read incoming gcp push request", "err", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = json.Unmarshal(bs, &pushMessage)
if err != nil {
p.metrics.gcpPushErrors.WithLabelValues("format").Inc()
level.Warn(p.logger).Log("msg", "failed to unmarshall gcp push request", "err", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err = pushMessage.Validate(); err != nil {
p.metrics.gcpPushErrors.WithLabelValues("invalid_message").Inc()
level.Warn(p.logger).Log("msg", "invalid gcp push request", "err", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
entry, err := translate(pushMessage, p.Labels(), p.config.UseIncomingTimestamp, p.config.UseFullLine, p.relabelConfigs, r.Header.Get("X-Scope-OrgID"))
if err != nil {
p.metrics.gcpPushErrors.WithLabelValues("translation").Inc()
level.Warn(p.logger).Log("msg", "failed to translate gcp push request", "err", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
level.Debug(p.logger).Log("msg", fmt.Sprintf("Received line: %s", entry.Line))
if err := p.doSendEntry(ctx, entry); err != nil {
// NOTE: timeout errors can be tracked with from the metrics exposed by
// the spun weaveworks server.
// loki.source.gcplog.componentid_push_target_request_duration_seconds_count{status_code="503"}
level.Warn(p.logger).Log("msg", "error sending log entry", "err", err.Error())
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
p.metrics.gcpPushEntries.WithLabelValues().Inc()
w.WriteHeader(http.StatusNoContent)
}
func (p *PushTarget) doSendEntry(ctx context.Context, entry loki.Entry) error {
select {
// Timeout the loki.Entry channel send operation, which is the only blocking operation in the handler
case <-ctx.Done():
return fmt.Errorf("timeout exceeded: %w", ctx.Err())
case p.entries <- entry:
return nil
}
}
// Labels return the model.LabelSet that the target applies to log entries.
func (p *PushTarget) Labels() model.LabelSet {
lbls := make(model.LabelSet, len(p.config.Labels))
for k, v := range p.config.Labels {
lbls[model.LabelName(k)] = model.LabelValue(v)
}
return lbls
}
// Details returns some debug information about the target.
func (p *PushTarget) Details() map[string]string {
return map[string]string{
"strategy": "push",
"labels": p.Labels().String(),
"server_address": p.server.HTTPListenAddr(),
}
}
// Stop shuts down the push target.
func (p *PushTarget) Stop() error {
level.Info(p.logger).Log("msg", "stopping gcp push target", "job", p.jobName)
p.server.StopAndShutdown()
p.handler.Stop()
return nil
}