Skip to content

Commit 72f5995

Browse files
authored
chore: add prometheus timing to latency endpoint (#7742)
* chore: Prometheus timing to latency endpoint
1 parent 944c42d commit 72f5995

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

coderd/coderd.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ func New(options *Options) *API {
405405
derpHandler := derphttp.Handler(api.DERPServer)
406406
derpHandler, api.derpCloseFunc = tailnet.WithWebsocketSupport(api.DERPServer, derpHandler)
407407
cors := httpmw.Cors(options.DeploymentValues.Dangerous.AllowAllCors.Value())
408+
prometheusMW := httpmw.Prometheus(options.PrometheusRegistry)
408409

409410
r.Use(
410411
cors,
@@ -414,7 +415,7 @@ func New(options *Options) *API {
414415
httpmw.AttachRequestID,
415416
httpmw.ExtractRealIP(api.RealIPConfig),
416417
httpmw.Logger(api.Logger),
417-
httpmw.Prometheus(options.PrometheusRegistry),
418+
prometheusMW,
418419
// SubdomainAppMW checks if the first subdomain is a valid app URL. If
419420
// it is, it will serve that application.
420421
//
@@ -826,7 +827,7 @@ func New(options *Options) *API {
826827
// This is the only route we add before all the middleware.
827828
// We want to time the latency of the request, so any middleware will
828829
// interfere with that timing.
829-
rootRouter.Get("/latency-check", cors(LatencyCheck(options.DeploymentValues.Dangerous.AllowAllCors.Value(), api.AccessURL)).ServeHTTP)
830+
rootRouter.Get("/latency-check", tracing.StatusWriterMiddleware(prometheusMW(LatencyCheck())).ServeHTTP)
830831
rootRouter.Mount("/", r)
831832
api.RootHandler = rootRouter
832833

coderd/latencycheck.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,24 @@ package coderd
22

33
import (
44
"net/http"
5-
"net/url"
6-
"strings"
75
)
86

97
// LatencyCheck is an endpoint for the web ui to measure latency with.
108
// allowAll allows any Origin to get timing information. The allowAll should
119
// only be set in dev modes.
1210
//
1311
//nolint:revive
14-
func LatencyCheck(allowAll bool, allowedOrigins ...*url.URL) http.HandlerFunc {
15-
allowed := make([]string, 0, len(allowedOrigins))
16-
for _, origin := range allowedOrigins {
17-
// Allow the origin without a path
18-
tmp := *origin
19-
tmp.Path = ""
20-
allowed = append(allowed, strings.TrimSuffix(origin.String(), "/"))
21-
}
22-
if allowAll {
23-
allowed = append(allowed, "*")
24-
}
25-
origins := strings.Join(allowed, ",")
12+
func LatencyCheck() http.HandlerFunc {
2613
return func(rw http.ResponseWriter, r *http.Request) {
2714
// Allowing timing information to be shared. This allows the browser
2815
// to exclude TLS handshake timing.
29-
rw.Header().Set("Timing-Allow-Origin", origins)
16+
rw.Header().Set("Timing-Allow-Origin", "*")
17+
// Always allow all CORs on this route.
18+
rw.Header().Set("Access-Control-Allow-Origin", "*")
19+
rw.Header().Set("Access-Control-Allow-Headers", "*")
20+
rw.Header().Set("Access-Control-Allow-Credentials", "false")
21+
rw.Header().Set("Access-Control-Allow-Methods", "*")
3022
rw.WriteHeader(http.StatusOK)
23+
_, _ = rw.Write([]byte("OK"))
3124
}
3225
}

enterprise/wsproxy/wsproxy.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
193193
// The primary coderd dashboard needs to make some GET requests to
194194
// the workspace proxies to check latency.
195195
corsMW := httpmw.Cors(opts.AllowAllCors, opts.DashboardURL.String())
196+
prometheusMW := httpmw.Prometheus(s.PrometheusRegistry)
196197

197198
// Routes
198199
apiRateLimiter := httpmw.RateLimit(opts.APIRateLimit, time.Minute)
@@ -205,7 +206,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
205206
httpmw.AttachRequestID,
206207
httpmw.ExtractRealIP(s.Options.RealIPConfig),
207208
httpmw.Logger(s.Logger),
208-
httpmw.Prometheus(s.PrometheusRegistry),
209+
prometheusMW,
209210
corsMW,
210211

211212
// HandleSubdomain is a middleware that handles all requests to the
@@ -258,7 +259,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
258259
// See coderd/coderd.go for why we need this.
259260
rootRouter := chi.NewRouter()
260261
// Make sure to add the cors middleware to the latency check route.
261-
rootRouter.Get("/latency-check", corsMW(coderd.LatencyCheck(opts.AllowAllCors, s.DashboardURL, s.AppServer.AccessURL)).ServeHTTP)
262+
rootRouter.Get("/latency-check", tracing.StatusWriterMiddleware(prometheusMW(coderd.LatencyCheck())).ServeHTTP)
262263
rootRouter.Mount("/", r)
263264
s.Handler = rootRouter
264265

site/src/contexts/useProxyLatency.ts

-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ export const useProxyLatency = (
140140
const proxyRequests = Object.keys(proxyChecks).map((latencyURL) => {
141141
return axios.get(latencyURL, {
142142
withCredentials: false,
143-
// Must add a custom header to make the request not a "simple request"
144-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests
145-
headers: { "X-LATENCY-CHECK": "true" },
146143
})
147144
})
148145

0 commit comments

Comments
 (0)