This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
/
api.go
276 lines (242 loc) 路 11.7 KB
/
api.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/api/api.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package api
import (
"context"
"flag"
"fmt"
"net/http"
"strings"
"github.com/bufbuild/connect-go"
"github.com/felixge/fgprof"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gorilla/mux"
"github.com/grafana/dskit/kv/memberlist"
grpcgw "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/weaveworks/common/middleware"
"github.com/weaveworks/common/server"
"github.com/grafana/phlare/public"
agentv1 "github.com/grafana/phlare/api/gen/proto/go/agent/v1"
"github.com/grafana/phlare/api/gen/proto/go/agent/v1/agentv1connect"
"github.com/grafana/phlare/api/gen/proto/go/ingester/v1/ingesterv1connect"
"github.com/grafana/phlare/api/gen/proto/go/push/v1/pushv1connect"
"github.com/grafana/phlare/api/gen/proto/go/querier/v1/querierv1connect"
statusv1 "github.com/grafana/phlare/api/gen/proto/go/status/v1"
"github.com/grafana/phlare/api/gen/proto/go/storegateway/v1/storegatewayv1connect"
"github.com/grafana/phlare/api/openapiv2"
"github.com/grafana/phlare/pkg/agent"
"github.com/grafana/phlare/pkg/distributor"
"github.com/grafana/phlare/pkg/frontend"
"github.com/grafana/phlare/pkg/frontend/frontendpb/frontendpbconnect"
"github.com/grafana/phlare/pkg/ingester"
"github.com/grafana/phlare/pkg/ingester/pyroscope"
"github.com/grafana/phlare/pkg/querier"
"github.com/grafana/phlare/pkg/scheduler"
"github.com/grafana/phlare/pkg/scheduler/schedulerpb/schedulerpbconnect"
"github.com/grafana/phlare/pkg/storegateway"
"github.com/grafana/phlare/pkg/util"
"github.com/grafana/phlare/pkg/util/gziphandler"
"github.com/grafana/phlare/pkg/validation/exporter"
)
type Config struct {
// The following configs are injected by the upstream caller.
HTTPAuthMiddleware middleware.Interface `yaml:"-"`
GrpcAuthMiddleware connect.Option `yaml:"-"`
BaseURL string `yaml:"base-url"`
}
type API struct {
server *server.Server
httpAuthMiddleware middleware.Interface
grpcGatewayMux *grpcgw.ServeMux
grpcAuthMiddleware connect.Option
grpcLogMiddleware connect.Option
cfg Config
logger log.Logger
indexPage *IndexPageContent
}
func New(cfg Config, s *server.Server, grpcGatewayMux *grpcgw.ServeMux, logger log.Logger) (*API, error) {
api := &API{
cfg: cfg,
httpAuthMiddleware: cfg.HTTPAuthMiddleware,
server: s,
logger: logger,
indexPage: NewIndexPageContent(),
grpcGatewayMux: grpcGatewayMux,
grpcAuthMiddleware: cfg.GrpcAuthMiddleware,
grpcLogMiddleware: connect.WithInterceptors(util.NewLogInterceptor(logger)),
}
// If no authentication middleware is present in the config, use the default authentication middleware.
if cfg.HTTPAuthMiddleware == nil {
api.httpAuthMiddleware = middleware.AuthenticateUser
}
return api, nil
}
// RegisterRoute registers a single route enforcing HTTP methods. A single
// route is expected to be specific about which HTTP methods are supported.
func (a *API) RegisterRoute(path string, handler http.Handler, auth, gzipEnabled bool, method string, methods ...string) {
methods = append([]string{method}, methods...)
level.Debug(a.logger).Log("msg", "api: registering route", "methods", strings.Join(methods, ","), "path", path, "auth", auth, "gzip", gzipEnabled)
a.newRoute(path, handler, false, auth, gzipEnabled, methods...)
}
func (a *API) RegisterRoutesWithPrefix(prefix string, handler http.Handler, auth, gzipEnabled bool, methods ...string) {
level.Debug(a.logger).Log("msg", "api: registering route", "methods", strings.Join(methods, ","), "prefix", prefix, "auth", auth, "gzip", gzipEnabled)
a.newRoute(prefix, handler, true, auth, gzipEnabled, methods...)
}
//nolint:unparam
func (a *API) newRoute(path string, handler http.Handler, isPrefix, auth, gzip bool, methods ...string) (route *mux.Route) {
if auth {
handler = a.httpAuthMiddleware.Wrap(handler)
}
if gzip {
handler = gziphandler.GzipHandler(handler)
}
if isPrefix {
route = a.server.HTTP.PathPrefix(path)
} else {
route = a.server.HTTP.Path(path)
}
if len(methods) > 0 {
route = route.Methods(methods...)
}
route = route.Handler(handler)
return route
}
// RegisterAPI registers the standard endpoints associated with a running Mimir.
func (a *API) RegisterAPI(statusService statusv1.StatusServiceServer) error {
// register index page
a.RegisterRoute("/", indexHandler("", a.indexPage), false, true, "GET")
// expose openapiv2 definition
openapiv2Handler, err := openapiv2.Handler()
if err != nil {
return fmt.Errorf("unable to initialize openapiv2 handler: %w", err)
}
a.RegisterRoute("/api/swagger.json", openapiv2Handler, false, true, "GET")
a.indexPage.AddLinks(openAPIDefinitionWeight, "OpenAPI definition", []IndexPageLink{
{Desc: "Swagger JSON", Path: "/api/swagger.json"},
})
// register grpc-gateway api
a.RegisterRoutesWithPrefix("/api", a.grpcGatewayMux, false, true, "GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
// register fgprof
a.RegisterRoute("/debug/fgprof", fgprof.Handler(), false, true, "GET")
// register static assets
a.RegisterRoutesWithPrefix("/static/", http.FileServer(http.FS(staticFiles)), false, true, "GET")
// register ui
uiAssets, err := public.Assets()
if err != nil {
return fmt.Errorf("unable to initialize the ui: %w", err)
}
uiIndexHandler, err := public.NewIndexHandler(a.cfg.BaseURL)
if err != nil {
return fmt.Errorf("unable to initialize the ui: %w", err)
}
// All assets are served as static files
a.RegisterRoutesWithPrefix("/ui/assets/", http.StripPrefix("/ui/", http.FileServer(uiAssets)), false, true, "GET")
// Serve index to all other pages
a.RegisterRoutesWithPrefix("/ui/", uiIndexHandler, false, true, "GET")
// Redirect `/ui` to `/ui/`.
// See more: https://github.com/grafana/phlare/pull/649#issuecomment-1522958157.
a.RegisterRoute("/ui", http.RedirectHandler("/ui/", http.StatusFound), false, true, "GET")
// register status service providing config and buildinfo at grpc gateway
if err := statusv1.RegisterStatusServiceHandlerServer(context.Background(), a.grpcGatewayMux, statusService); err != nil {
return err
}
a.indexPage.AddLinks(buildInfoWeight, "Build information", []IndexPageLink{
{Desc: "Build information", Path: "/api/v1/status/buildinfo"},
})
a.indexPage.AddLinks(configWeight, "Current config", []IndexPageLink{
{Desc: "Including the default values", Path: "/api/v1/status/config"},
{Desc: "Only values that differ from the defaults", Path: "/api/v1/status/config/diff"},
{Desc: "Default values", Path: "/api/v1/status/config/default"},
})
return nil
}
// RegisterRuntimeConfig registers the endpoints associates with the runtime configuration
func (a *API) RegisterRuntimeConfig(runtimeConfigHandler http.HandlerFunc, userLimitsHandler http.HandlerFunc) {
a.RegisterRoute("/runtime_config", runtimeConfigHandler, false, true, "GET")
a.RegisterRoute("/api/v1/tenant_limits", userLimitsHandler, true, true, "GET")
a.indexPage.AddLinks(runtimeConfigWeight, "Current runtime config", []IndexPageLink{
{Desc: "Entire runtime config (including overrides)", Path: "/runtime_config"},
{Desc: "Only values that differ from the defaults", Path: "/runtime_config?mode=diff"},
})
}
// RegisterOverridesExporter registers the endpoints associated with the overrides exporter.
func (a *API) RegisterOverridesExporter(oe *exporter.OverridesExporter) {
a.RegisterRoute("/overrides-exporter/ring", http.HandlerFunc(oe.RingHandler), false, true, "GET", "POST")
a.indexPage.AddLinks(defaultWeight, "Overrides-exporter", []IndexPageLink{
{Desc: "Ring status", Path: "/overrides-exporter/ring"},
})
}
// RegisterDistributor registers the endpoints associated with the distributor.
func (a *API) RegisterDistributor(d *distributor.Distributor) {
pyroscopeHandler := pyroscope.NewPyroscopeIngestHandler(d, a.logger)
a.RegisterRoute("/ingest", pyroscopeHandler, true, true, "POST")
a.RegisterRoute("/pyroscope/ingest", pyroscopeHandler, true, true, "POST")
pushv1connect.RegisterPusherServiceHandler(a.server.HTTP, d, a.grpcAuthMiddleware)
a.RegisterRoute("/distributor/ring", d, false, true, "GET", "POST")
a.indexPage.AddLinks(defaultWeight, "Distributor", []IndexPageLink{
{Desc: "Ring status", Path: "/distributor/ring"},
})
}
// RegisterMemberlistKV registers the endpoints associated with the memberlist KV store.
func (a *API) RegisterMemberlistKV(pathPrefix string, kvs *memberlist.KVInitService) {
a.RegisterRoute("/memberlist", MemberlistStatusHandler(pathPrefix, kvs), false, true, "GET")
a.indexPage.AddLinks(memberlistWeight, "Memberlist", []IndexPageLink{
{Desc: "Status", Path: "/memberlist"},
})
}
// RegisterRing registers the ring UI page associated with the distributor for writes.
func (a *API) RegisterRing(r http.Handler) {
a.RegisterRoute("/ring", r, false, true, "GET", "POST")
a.indexPage.AddLinks(defaultWeight, "Ingester", []IndexPageLink{
{Desc: "Ring status", Path: "/ring"},
})
}
// RegisterQuerier registers the endpoints associated with the querier.
func (a *API) RegisterQuerier(svc querierv1connect.QuerierServiceHandler) {
querierv1connect.RegisterQuerierServiceHandler(a.server.HTTP, svc, a.grpcAuthMiddleware, a.grpcLogMiddleware)
}
func (a *API) RegisterPyroscopeHandlers(client querierv1connect.QuerierServiceClient) {
handlers := querier.NewHTTPHandlers(client)
a.RegisterRoute("/pyroscope/render", http.HandlerFunc(handlers.Render), true, true, "GET")
a.RegisterRoute("/pyroscope/render-diff", http.HandlerFunc(handlers.RenderDiff), true, true, "GET")
a.RegisterRoute("/pyroscope/label-values", http.HandlerFunc(handlers.LabelValues), true, true, "GET")
}
// RegisterAgent registers the endpoints associated with the agent.
func (a *API) RegisterAgent(ag *agent.Agent) error {
// register endpoint at grpc gateway
if err := agentv1.RegisterAgentServiceHandlerServer(context.Background(), a.grpcGatewayMux, ag); err != nil {
return err
}
agentv1connect.RegisterAgentServiceHandler(a.server.HTTP, ag.ConnectHandler())
return nil
}
// RegisterIngester registers the endpoints associated with the ingester.
func (a *API) RegisterIngester(svc *ingester.Ingester) {
ingesterv1connect.RegisterIngesterServiceHandler(a.server.HTTP, svc, a.grpcAuthMiddleware)
}
func (a *API) RegisterStoreGateway(svc *storegateway.StoreGateway) {
storegatewayv1connect.RegisterStoreGatewayServiceHandler(a.server.HTTP, svc, a.grpcAuthMiddleware)
a.indexPage.AddLinks(defaultWeight, "Store-gateway", []IndexPageLink{
{Desc: "Ring status", Path: "/store-gateway/ring"},
{Desc: "Tenants & Blocks", Path: "/store-gateway/tenants"},
})
a.RegisterRoute("/store-gateway/ring", http.HandlerFunc(svc.RingHandler), false, true, "GET", "POST")
a.RegisterRoute("/store-gateway/tenants", http.HandlerFunc(svc.TenantsHandler), false, true, "GET")
a.RegisterRoute("/store-gateway/tenant/{tenant}/blocks", http.HandlerFunc(svc.BlocksHandler), false, true, "GET")
}
// RegisterQueryFrontend registers the endpoints associated with the query frontend.
func (a *API) RegisterQueryFrontend(frontendSvc *frontend.Frontend) {
frontendpbconnect.RegisterFrontendForQuerierHandler(a.server.HTTP, frontendSvc, a.grpcAuthMiddleware)
}
// RegisterQueryScheduler registers the endpoints associated with the query scheduler.
func (a *API) RegisterQueryScheduler(s *scheduler.Scheduler) {
schedulerpbconnect.RegisterSchedulerForFrontendHandler(a.server.HTTP, s)
schedulerpbconnect.RegisterSchedulerForQuerierHandler(a.server.HTTP, s)
}
// RegisterFlags registers api-related flags.
func (cfg *Config) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(&cfg.BaseURL, "api.base-url", "", "base URL for when the server is behind a reverse proxy with a different path")
}