feat(agent): Expose magicsock metrics (#7183)

* feat: Expose magicsock metrics

* golden-files
This commit is contained in:
Marcin Tojek
2023-04-19 09:09:23 +02:00
committed by GitHub
parent fbf329fbb7
commit f94ac55f02
2 changed files with 28 additions and 0 deletions
+25
View File
@@ -18,6 +18,7 @@ import (
"cloud.google.com/go/compute/metadata"
"golang.org/x/xerrors"
"gopkg.in/natefinch/lumberjack.v2"
"tailscale.com/util/clientmetric"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
@@ -36,6 +37,7 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
noReap bool
sshMaxTimeout time.Duration
tailnetListenPort int64
prometheusAddress string
)
cmd := &clibase.Cmd{
Use: "agent",
@@ -126,6 +128,13 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
agentPorts[port] = "pprof"
}
prometheusSrvClose := ServeHandler(ctx, logger, prometheusMetricsHandler(), prometheusAddress, "prometheus")
defer prometheusSrvClose()
// Do a best effort here. If this fails, it's not a big deal.
if port, err := urlPort(prometheusAddress); err == nil {
agentPorts[port] = "prometheus"
}
// exchangeToken returns a session token.
// This is abstracted to allow for the same looping condition
// regardless of instance identity auth type.
@@ -257,6 +266,13 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
Description: "Specify a static port for Tailscale to use for listening.",
Value: clibase.Int64Of(&tailnetListenPort),
},
{
Flag: "prometheus-address",
Default: "127.0.0.1:2112",
Env: "CODER_AGENT_PROMETHEUS_ADDRESS",
Value: clibase.StringOf(&prometheusAddress),
Description: "The bind address to serve Prometheus metrics.",
},
}
return cmd
@@ -343,3 +359,12 @@ func urlPort(u string) (int, error) {
}
return -1, xerrors.Errorf("invalid port: %s", u)
}
func prometheusMetricsHandler() http.Handler {
// We don't have any other internal metrics so far, so it's safe to expose metrics this way.
// Based on: https://github.com/tailscale/tailscale/blob/280255acae604796a1113861f5a84e6fa2dc6121/ipn/localapi/localapi.go#L489
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
clientmetric.WritePrometheusExpositionFormat(w)
})
}
+3
View File
@@ -15,6 +15,9 @@ Starts the Coder workspace agent.
--pprof-address string, $CODER_AGENT_PPROF_ADDRESS (default: 127.0.0.1:6060)
The address to serve pprof.
--prometheus-address string, $CODER_AGENT_PROMETHEUS_ADDRESS (default: 127.0.0.1:2112)
The bind address to serve Prometheus metrics.
--ssh-max-timeout duration, $CODER_AGENT_SSH_MAX_TIMEOUT (default: 0)
Specify the max timeout for a SSH connection.