-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
278 lines (260 loc) · 8.32 KB
/
main.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"math"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gogo/status"
"github.com/oklog/run"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/thanos-io/thanos/pkg/api/query/querypb"
"github.com/thanos-io/thanos/pkg/component"
"github.com/thanos-io/thanos/pkg/info/infopb"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb/prompb"
"google.golang.org/api/option"
apihttp "google.golang.org/api/transport/http"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
var (
configFileAddress = flag.String("query.config-file", "", "Thanos-promql-connector configuration file path.")
connectorAddress = flag.String("connector-address", ":8081",
"Address on which to expose the query grpc server.")
metricsAddress = flag.String("metrics-address", ":9090",
"Address on which to expose metrics")
)
type queryServer struct {
queryBackendClient v1.API
}
func (qs *queryServer) Query(req *querypb.QueryRequest, srv querypb.Query_QueryServer) error {
ts := time.Unix(req.TimeSeconds, 0)
timeout := time.Duration(req.TimeoutSeconds) * time.Second
values, warnings, err := qs.queryBackendClient.Query(srv.Context(), req.Query, ts, v1.WithTimeout(timeout))
if err != nil {
return status.Error(codes.Aborted, err.Error())
}
if len(warnings) > 0 {
errs := make([]error, 0, len(warnings))
for _, warning := range warnings {
errs = append(errs, errors.New(warning))
}
if err = srv.SendMsg(querypb.NewQueryWarningsResponse(errs...)); err != nil {
return err
}
}
switch results := values.(type) {
case model.Vector:
for _, result := range results {
series := &prompb.TimeSeries{
Samples: []prompb.Sample{{Value: float64(result.Value), Timestamp: int64(result.Timestamp)}},
Labels: zLabelsFromMetric(result.Metric),
}
if err := srv.Send(querypb.NewQueryResponse(series)); err != nil {
return err
}
}
case *model.Scalar:
series := &prompb.TimeSeries{Samples: []prompb.Sample{{Value: float64(results.Value), Timestamp: int64(results.Timestamp)}}}
return srv.Send(querypb.NewQueryResponse(series))
}
return nil
}
func (qs *queryServer) QueryRange(req *querypb.QueryRangeRequest, srv querypb.Query_QueryRangeServer) error {
timeout := time.Duration(req.TimeoutSeconds) * time.Second
interval := v1.Range{
Start: time.Unix(req.StartTimeSeconds, 0),
End: time.Unix(req.EndTimeSeconds, 0),
Step: time.Duration(req.IntervalSeconds) * time.Second}
values, warnings, err := qs.queryBackendClient.QueryRange(srv.Context(), req.Query, interval, v1.WithTimeout(timeout))
if err != nil {
return status.Error(codes.Aborted, err.Error())
}
if len(warnings) > 0 {
errs := make([]error, 0, len(warnings))
for _, warning := range warnings {
errs = append(errs, errors.New(warning))
}
if err = srv.SendMsg(querypb.NewQueryRangeWarningsResponse(errs...)); err != nil {
return err
}
}
switch results := values.(type) {
case model.Matrix:
for _, result := range results {
series := &prompb.TimeSeries{
Samples: samplesFromModel(result.Values),
Labels: zLabelsFromMetric(result.Metric),
}
if err := srv.Send(querypb.NewQueryRangeResponse(series)); err != nil {
return err
}
}
case model.Vector:
for _, result := range results {
series := &prompb.TimeSeries{
Samples: []prompb.Sample{{Value: float64(result.Value), Timestamp: int64(result.Timestamp)}},
Labels: zLabelsFromMetric(result.Metric),
}
if err := srv.Send(querypb.NewQueryRangeResponse(series)); err != nil {
return err
}
}
case *model.Scalar:
series := &prompb.TimeSeries{Samples: []prompb.Sample{{Value: float64(results.Value), Timestamp: int64(results.Timestamp)}}}
return srv.Send(querypb.NewQueryRangeResponse(series))
}
return nil
}
// samplesFromModel converts model.SamplePair to prompb.Sample.
func samplesFromModel(samples []model.SamplePair) []prompb.Sample {
result := make([]prompb.Sample, 0, len(samples))
for _, s := range samples {
result = append(result, prompb.Sample{
Value: float64(s.Value),
Timestamp: int64(s.Timestamp),
})
}
return result
}
// zLabelsFromMetric converts model.Metric to labelpb.Zlabel.
func zLabelsFromMetric(metric model.Metric) []labelpb.ZLabel {
zlabels := make([]labelpb.ZLabel, 0, len(metric))
for labelName, labelValue := range metric {
zlabel := labelpb.ZLabel{Name: string(labelName), Value: string(labelValue)}
zlabels = append(zlabels, zlabel)
}
return zlabels
}
type infoServer struct {
queryBackend string
}
func (info *infoServer) Info(ctx context.Context, in *infopb.InfoRequest) (*infopb.InfoResponse, error) {
return &infopb.InfoResponse{
ComponentType: component.Query.String(),
LabelSets: labelpb.ZLabelSetsFromPromLabels(labels.FromStrings("query-backend", info.queryBackend)),
Store: &infopb.StoreInfo{
MinTime: math.MinInt64,
MaxTime: math.MaxInt64,
SupportsWithoutReplicaLabels: true,
TsdbInfos: []infopb.TSDBInfo{
{
MinTime: math.MinInt64,
MaxTime: math.MaxInt64,
},
},
},
Query: &infopb.QueryAPIInfo{},
}, nil
}
// createQueryBackendClient creates a connection with the backend that the queries will be forwarded to.
func createQueryBackendClient(queryConfig queryBackendConfig) (v1.API, error) {
opts := []option.ClientOption{
option.WithScopes(queryConfig.Auth.Scopes...),
option.WithCredentialsFile(queryConfig.Auth.CredentialsFile),
}
transport, err := apihttp.NewTransport(context.Background(), http.DefaultTransport, opts...)
if err != nil {
return nil, fmt.Errorf("error creating proxy HTTP transport: %s", err)
}
client, err := api.NewClient(api.Config{
Address: queryConfig.QueryTargetURL,
RoundTripper: transport,
})
if err != nil {
return nil, fmt.Errorf("error creating client: %s", err)
}
return v1.NewAPI(client), nil
}
func main() {
flag.Parse()
logger := log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
var err error
// Configuration Loading.
queryConfig, err := loadQueryConfig(*configFileAddress)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
// Query client setup.
queryBackendClient, err := createQueryBackendClient(*queryConfig)
if err != nil {
level.Error(logger).Log("msg", "Error creating client", "err", err)
os.Exit(1)
}
var g run.Group
{
term := make(chan os.Signal, 1)
cancel := make(chan struct{})
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
g.Add(
func() error {
select {
case <-term:
level.Info(logger).Log("msg", "received SIGTERM, exiting gracefully...")
case <-cancel:
}
return nil
},
func(err error) {
close(cancel)
},
)
}
{
//grpc server.
listener, err := net.Listen("tcp", *connectorAddress)
if err != nil {
panic(err)
}
server := grpc.NewServer()
querypb.RegisterQueryServer(server, &queryServer{queryBackendClient: queryBackendClient})
infopb.RegisterInfoServer(server, &infoServer{queryBackend: queryConfig.QueryTargetURL})
g.Add(func() error {
level.Info(logger).Log("msg", "Starting grpc server for query endpoint", "listen", *connectorAddress)
return server.Serve(listener)
}, func(err error) {
server.GracefulStop()
})
}
{
// http server.
ctx, cancel := context.WithCancel(context.Background())
server := &http.Server{Addr: *metricsAddress}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/-/healthy", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK")
})
http.HandleFunc("/-/ready", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK")
})
g.Add(func() error {
level.Info(logger).Log("msg", "Starting web server for metrics", "listen", *metricsAddress)
return server.ListenAndServe()
}, func(err error) {
server.Shutdown(ctx)
cancel()
})
}
if err := g.Run(); err != nil {
level.Error(logger).Log("msg", "running reloader failed", "err", err)
os.Exit(1)
}
}