Skip to content

feat(agent): Expose magicsock metrics #7183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cli/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -36,6 +37,7 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
noReap bool
sshMaxTimeout time.Duration
tailnetListenPort int64
prometheusAddress string
)
cmd := &clibase.Cmd{
Use: "agent",
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 3 additions & 0 deletions cli/testdata/coder_agent_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down