mirror of https://github.com/coder/coder.git
chore: add prometheus timing to latency endpoint (#7742)
* chore: Prometheus timing to latency endpoint
This commit is contained in:
parent
944c42dcb6
commit
72f59950f2
|
@ -405,6 +405,7 @@ func New(options *Options) *API {
|
||||||
derpHandler := derphttp.Handler(api.DERPServer)
|
derpHandler := derphttp.Handler(api.DERPServer)
|
||||||
derpHandler, api.derpCloseFunc = tailnet.WithWebsocketSupport(api.DERPServer, derpHandler)
|
derpHandler, api.derpCloseFunc = tailnet.WithWebsocketSupport(api.DERPServer, derpHandler)
|
||||||
cors := httpmw.Cors(options.DeploymentValues.Dangerous.AllowAllCors.Value())
|
cors := httpmw.Cors(options.DeploymentValues.Dangerous.AllowAllCors.Value())
|
||||||
|
prometheusMW := httpmw.Prometheus(options.PrometheusRegistry)
|
||||||
|
|
||||||
r.Use(
|
r.Use(
|
||||||
cors,
|
cors,
|
||||||
|
@ -414,7 +415,7 @@ func New(options *Options) *API {
|
||||||
httpmw.AttachRequestID,
|
httpmw.AttachRequestID,
|
||||||
httpmw.ExtractRealIP(api.RealIPConfig),
|
httpmw.ExtractRealIP(api.RealIPConfig),
|
||||||
httpmw.Logger(api.Logger),
|
httpmw.Logger(api.Logger),
|
||||||
httpmw.Prometheus(options.PrometheusRegistry),
|
prometheusMW,
|
||||||
// SubdomainAppMW checks if the first subdomain is a valid app URL. If
|
// SubdomainAppMW checks if the first subdomain is a valid app URL. If
|
||||||
// it is, it will serve that application.
|
// it is, it will serve that application.
|
||||||
//
|
//
|
||||||
|
@ -826,7 +827,7 @@ func New(options *Options) *API {
|
||||||
// This is the only route we add before all the middleware.
|
// This is the only route we add before all the middleware.
|
||||||
// We want to time the latency of the request, so any middleware will
|
// We want to time the latency of the request, so any middleware will
|
||||||
// interfere with that timing.
|
// interfere with that timing.
|
||||||
rootRouter.Get("/latency-check", cors(LatencyCheck(options.DeploymentValues.Dangerous.AllowAllCors.Value(), api.AccessURL)).ServeHTTP)
|
rootRouter.Get("/latency-check", tracing.StatusWriterMiddleware(prometheusMW(LatencyCheck())).ServeHTTP)
|
||||||
rootRouter.Mount("/", r)
|
rootRouter.Mount("/", r)
|
||||||
api.RootHandler = rootRouter
|
api.RootHandler = rootRouter
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,6 @@ package coderd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// LatencyCheck is an endpoint for the web ui to measure latency with.
|
// LatencyCheck is an endpoint for the web ui to measure latency with.
|
||||||
|
@ -11,22 +9,17 @@ import (
|
||||||
// only be set in dev modes.
|
// only be set in dev modes.
|
||||||
//
|
//
|
||||||
//nolint:revive
|
//nolint:revive
|
||||||
func LatencyCheck(allowAll bool, allowedOrigins ...*url.URL) http.HandlerFunc {
|
func LatencyCheck() http.HandlerFunc {
|
||||||
allowed := make([]string, 0, len(allowedOrigins))
|
|
||||||
for _, origin := range allowedOrigins {
|
|
||||||
// Allow the origin without a path
|
|
||||||
tmp := *origin
|
|
||||||
tmp.Path = ""
|
|
||||||
allowed = append(allowed, strings.TrimSuffix(origin.String(), "/"))
|
|
||||||
}
|
|
||||||
if allowAll {
|
|
||||||
allowed = append(allowed, "*")
|
|
||||||
}
|
|
||||||
origins := strings.Join(allowed, ",")
|
|
||||||
return func(rw http.ResponseWriter, r *http.Request) {
|
return func(rw http.ResponseWriter, r *http.Request) {
|
||||||
// Allowing timing information to be shared. This allows the browser
|
// Allowing timing information to be shared. This allows the browser
|
||||||
// to exclude TLS handshake timing.
|
// to exclude TLS handshake timing.
|
||||||
rw.Header().Set("Timing-Allow-Origin", origins)
|
rw.Header().Set("Timing-Allow-Origin", "*")
|
||||||
|
// Always allow all CORs on this route.
|
||||||
|
rw.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
rw.Header().Set("Access-Control-Allow-Headers", "*")
|
||||||
|
rw.Header().Set("Access-Control-Allow-Credentials", "false")
|
||||||
|
rw.Header().Set("Access-Control-Allow-Methods", "*")
|
||||||
rw.WriteHeader(http.StatusOK)
|
rw.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = rw.Write([]byte("OK"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,6 +193,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
|
||||||
// The primary coderd dashboard needs to make some GET requests to
|
// The primary coderd dashboard needs to make some GET requests to
|
||||||
// the workspace proxies to check latency.
|
// the workspace proxies to check latency.
|
||||||
corsMW := httpmw.Cors(opts.AllowAllCors, opts.DashboardURL.String())
|
corsMW := httpmw.Cors(opts.AllowAllCors, opts.DashboardURL.String())
|
||||||
|
prometheusMW := httpmw.Prometheus(s.PrometheusRegistry)
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
apiRateLimiter := httpmw.RateLimit(opts.APIRateLimit, time.Minute)
|
apiRateLimiter := httpmw.RateLimit(opts.APIRateLimit, time.Minute)
|
||||||
|
@ -205,7 +206,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
|
||||||
httpmw.AttachRequestID,
|
httpmw.AttachRequestID,
|
||||||
httpmw.ExtractRealIP(s.Options.RealIPConfig),
|
httpmw.ExtractRealIP(s.Options.RealIPConfig),
|
||||||
httpmw.Logger(s.Logger),
|
httpmw.Logger(s.Logger),
|
||||||
httpmw.Prometheus(s.PrometheusRegistry),
|
prometheusMW,
|
||||||
corsMW,
|
corsMW,
|
||||||
|
|
||||||
// HandleSubdomain is a middleware that handles all requests to the
|
// HandleSubdomain is a middleware that handles all requests to the
|
||||||
|
@ -258,7 +259,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
|
||||||
// See coderd/coderd.go for why we need this.
|
// See coderd/coderd.go for why we need this.
|
||||||
rootRouter := chi.NewRouter()
|
rootRouter := chi.NewRouter()
|
||||||
// Make sure to add the cors middleware to the latency check route.
|
// Make sure to add the cors middleware to the latency check route.
|
||||||
rootRouter.Get("/latency-check", corsMW(coderd.LatencyCheck(opts.AllowAllCors, s.DashboardURL, s.AppServer.AccessURL)).ServeHTTP)
|
rootRouter.Get("/latency-check", tracing.StatusWriterMiddleware(prometheusMW(coderd.LatencyCheck())).ServeHTTP)
|
||||||
rootRouter.Mount("/", r)
|
rootRouter.Mount("/", r)
|
||||||
s.Handler = rootRouter
|
s.Handler = rootRouter
|
||||||
|
|
||||||
|
|
|
@ -140,9 +140,6 @@ export const useProxyLatency = (
|
||||||
const proxyRequests = Object.keys(proxyChecks).map((latencyURL) => {
|
const proxyRequests = Object.keys(proxyChecks).map((latencyURL) => {
|
||||||
return axios.get(latencyURL, {
|
return axios.get(latencyURL, {
|
||||||
withCredentials: false,
|
withCredentials: false,
|
||||||
// Must add a custom header to make the request not a "simple request"
|
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests
|
|
||||||
headers: { "X-LATENCY-CHECK": "true" },
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue