-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathfrontend.go
214 lines (180 loc) · 7.19 KB
/
frontend.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
package frontend
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/weaveworks/common/user"
"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/modules/storage"
"github.com/grafana/tempo/pkg/api"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/grafana/tempo/tempodb"
)
const (
traceByIDOp = "traces"
searchOp = "search"
)
type QueryFrontend struct {
TraceByID, Search http.Handler
logger log.Logger
queriesPerTenant *prometheus.CounterVec
store storage.Store
}
// New returns a new QueryFrontend
func New(cfg Config, next http.RoundTripper, o *overrides.Overrides, store storage.Store, logger log.Logger, registerer prometheus.Registerer) (*QueryFrontend, error) {
level.Info(logger).Log("msg", "creating middleware in query frontend")
if cfg.QueryShards < minQueryShards || cfg.QueryShards > maxQueryShards {
return nil, fmt.Errorf("frontend query shards should be between %d and %d (both inclusive)", minQueryShards, maxQueryShards)
}
if cfg.Search.Sharder.ConcurrentRequests <= 0 {
return nil, fmt.Errorf("frontend search concurrent requests should be greater than 0")
}
if cfg.Search.Sharder.TargetBytesPerRequest <= 0 {
return nil, fmt.Errorf("frontend search target bytes per request should be greater than 0")
}
if cfg.Search.Sharder.QueryIngestersUntil < cfg.Search.Sharder.QueryBackendAfter {
return nil, fmt.Errorf("query backend after should be less than or equal to query ingester until")
}
queriesPerTenant := promauto.With(registerer).NewCounterVec(prometheus.CounterOpts{
Namespace: "tempo",
Name: "query_frontend_queries_total",
Help: "Total queries received per tenant.",
}, []string{"tenant", "op"})
retryWare := newRetryWare(cfg.MaxRetries, registerer)
// tracebyid middleware
traceByIDMiddleware := MergeMiddlewares(newTraceByIDMiddleware(cfg, logger), retryWare)
searchMiddleware := MergeMiddlewares(newSearchMiddleware(cfg, o, store, logger), retryWare)
traceByIDCounter := queriesPerTenant.MustCurryWith(prometheus.Labels{
"op": traceByIDOp,
})
searchCounter := queriesPerTenant.MustCurryWith(prometheus.Labels{
"op": searchOp,
})
traces := traceByIDMiddleware.Wrap(next)
search := searchMiddleware.Wrap(next)
return &QueryFrontend{
TraceByID: newHandler(traces, traceByIDCounter, logger),
Search: newHandler(search, searchCounter, logger),
logger: logger,
queriesPerTenant: queriesPerTenant,
store: store,
}, nil
}
// newTraceByIDMiddleware creates a new frontend middleware responsible for handling get traces requests.
func newTraceByIDMiddleware(cfg Config, logger log.Logger) Middleware {
return MiddlewareFunc(func(next http.RoundTripper) http.RoundTripper {
// We're constructing middleware in this statement, each middleware wraps the next one from left-to-right
// - the Deduper dedupes Span IDs for Zipkin support
// - the ShardingWare shards queries by splitting the block ID space
// - the RetryWare retries requests that have failed (error or http status 500)
rt := NewRoundTripper(next, newDeduper(logger), newTraceByIDSharder(cfg.QueryShards, cfg.TolerateFailedBlocks, logger))
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
// validate traceID
_, err := api.ParseTraceID(r)
if err != nil {
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(strings.NewReader(err.Error())),
Header: http.Header{},
}, nil
}
//validate start and end parameter
_, _, _, _, _, reqErr := api.ValidateAndSanitizeRequest(r)
if reqErr != nil {
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(strings.NewReader(reqErr.Error())),
Header: http.Header{},
}, nil
}
// check marshalling format
marshallingFormat := api.HeaderAcceptJSON
if r.Header.Get(api.HeaderAccept) == api.HeaderAcceptProtobuf {
marshallingFormat = api.HeaderAcceptProtobuf
}
// enforce all communication internal to Tempo to be in protobuf bytes
r.Header.Set(api.HeaderAccept, api.HeaderAcceptProtobuf)
resp, err := rt.RoundTrip(r)
// todo : should all of this request/response content type be up a level and be used for all query types?
if resp != nil && resp.StatusCode == http.StatusOK {
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, errors.Wrap(err, "error reading response body at query frontend")
}
responseObject := &tempopb.TraceByIDResponse{}
err = proto.Unmarshal(body, responseObject)
if err != nil {
return nil, err
}
if responseObject.Metrics.FailedBlocks > 0 {
resp.StatusCode = http.StatusPartialContent
}
if marshallingFormat == api.HeaderAcceptJSON {
var jsonTrace bytes.Buffer
marshaller := &jsonpb.Marshaler{}
err = marshaller.Marshal(&jsonTrace, responseObject.Trace)
if err != nil {
return nil, err
}
resp.Body = io.NopCloser(bytes.NewReader(jsonTrace.Bytes()))
} else {
traceBuffer, err := proto.Marshal(responseObject.Trace)
if err != nil {
return nil, err
}
resp.Body = io.NopCloser(bytes.NewReader(traceBuffer))
}
if resp.Header != nil {
resp.Header.Set(api.HeaderContentType, marshallingFormat)
}
}
span := opentracing.SpanFromContext(r.Context())
if span != nil {
span.SetTag("contentType", marshallingFormat)
}
return resp, err
})
})
}
// newSearchMiddleware creates a new frontend middleware to handle search and search tags requests.
func newSearchMiddleware(cfg Config, o *overrides.Overrides, reader tempodb.Reader, logger log.Logger) Middleware {
return MiddlewareFunc(func(next http.RoundTripper) http.RoundTripper {
ingesterSearchRT := next
backendSearchRT := NewRoundTripper(next, newSearchSharder(reader, o, cfg.Search.Sharder, logger))
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
// backend search queries require sharding so we pass through a special roundtripper
if api.IsBackendSearch(r) {
return backendSearchRT.RoundTrip(r)
}
// ingester search queries only need to be proxied to a single querier
orgID, _ := user.ExtractOrgID(r.Context())
r.Header.Set(user.OrgIDHeaderName, orgID)
r.RequestURI = buildUpstreamRequestURI(r.RequestURI, nil)
return ingesterSearchRT.RoundTrip(r)
})
})
}
// buildUpstreamRequestURI returns a uri based on the passed parameters
// we do this because weaveworks/common uses the RequestURI field to translate from http.Request to httpgrpc.Request
// https://github.com/weaveworks/common/blob/47e357f4e1badb7da17ad74bae63e228bdd76e8f/httpgrpc/server/server.go#L48
func buildUpstreamRequestURI(originalURI string, params url.Values) string {
const queryDelimiter = "?"
uri := path.Join(api.PathPrefixQuerier, originalURI)
if len(params) > 0 {
uri += queryDelimiter + params.Encode()
}
return uri
}