@@ -33,14 +33,15 @@ import (
33
33
"sync/atomic"
34
34
"time"
35
35
36
+ "github.com/prometheus/client_golang/prometheus/collectors"
37
+ "github.com/prometheus/client_golang/prometheus/promhttp"
38
+
36
39
"github.com/coreos/go-oidc/v3/oidc"
37
40
"github.com/coreos/go-systemd/daemon"
38
41
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
39
42
"github.com/google/go-github/v43/github"
40
43
"github.com/google/uuid"
41
44
"github.com/prometheus/client_golang/prometheus"
42
- "github.com/prometheus/client_golang/prometheus/collectors"
43
- "github.com/prometheus/client_golang/prometheus/promhttp"
44
45
"github.com/spf13/afero"
45
46
"go.opentelemetry.io/otel/trace"
46
47
"golang.org/x/mod/semver"
@@ -660,33 +661,33 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
660
661
defer options .Telemetry .Close ()
661
662
}
662
663
663
- // This prevents the pprof import from being accidentally deleted.
664
- _ = pprof .Handler
665
- if cfg .Pprof .Enable {
666
- //nolint:revive
667
- defer serveHandler (ctx , logger , nil , cfg .Pprof .Address .String (), "pprof" )()
668
- }
669
- if cfg .Prometheus .Enable {
670
- options .PrometheusRegistry .MustRegister (collectors .NewGoCollector ())
671
- options .PrometheusRegistry .MustRegister (collectors .NewProcessCollector (collectors.ProcessCollectorOpts {}))
672
-
673
- closeUsersFunc , err := prometheusmetrics .ActiveUsers (ctx , options .PrometheusRegistry , options .Database , 0 )
674
- if err != nil {
675
- return xerrors .Errorf ("register active users prometheus metric: %w" , err )
676
- }
677
- defer closeUsersFunc ()
678
-
679
- closeWorkspacesFunc , err := prometheusmetrics .Workspaces (ctx , options .PrometheusRegistry , options .Database , 0 )
680
- if err != nil {
681
- return xerrors .Errorf ("register workspaces prometheus metric: %w" , err )
682
- }
683
- defer closeWorkspacesFunc ()
684
-
685
- //nolint:revive
686
- defer serveHandler (ctx , logger , promhttp .InstrumentMetricHandler (
687
- options .PrometheusRegistry , promhttp .HandlerFor (options .PrometheusRegistry , promhttp.HandlerOpts {}),
688
- ), cfg .Prometheus .Address .String (), "prometheus" )()
689
- }
664
+ //// This prevents the pprof import from being accidentally deleted.
665
+ // _ = pprof.Handler
666
+ // if cfg.Pprof.Enable {
667
+ // //nolint:revive
668
+ // defer serveHandler(ctx, logger, nil, cfg.Pprof.Address.String(), "pprof")()
669
+ // }
670
+ // if cfg.Prometheus.Enable {
671
+ // options.PrometheusRegistry.MustRegister(collectors.NewGoCollector())
672
+ // options.PrometheusRegistry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
673
+ //
674
+ // closeUsersFunc, err := prometheusmetrics.ActiveUsers(ctx, options.PrometheusRegistry, options.Database, 0)
675
+ // if err != nil {
676
+ // return xerrors.Errorf("register active users prometheus metric: %w", err)
677
+ // }
678
+ // defer closeUsersFunc()
679
+ //
680
+ // closeWorkspacesFunc, err := prometheusmetrics.Workspaces(ctx, options.PrometheusRegistry, options.Database, 0)
681
+ // if err != nil {
682
+ // return xerrors.Errorf("register workspaces prometheus metric: %w", err)
683
+ // }
684
+ // defer closeWorkspacesFunc()
685
+ //
686
+ // //nolint:revive
687
+ // defer serveHandler(ctx, logger, promhttp.InstrumentMetricHandler(
688
+ // options.PrometheusRegistry, promhttp.HandlerFor(options.PrometheusRegistry, promhttp.HandlerOpts{}),
689
+ // ), cfg.Prometheus.Address.String(), "prometheus")()
690
+ // }
690
691
691
692
if cfg .Swagger .Enable {
692
693
options .SwaggerEndpoint = cfg .Swagger .Enable .Value ()
@@ -701,6 +702,18 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
701
702
}
702
703
703
704
if cfg .Prometheus .Enable {
705
+ closeUsersFunc , err := prometheusmetrics .ActiveUsers (ctx , options .PrometheusRegistry , options .Database , 0 )
706
+ if err != nil {
707
+ return xerrors .Errorf ("register active users prometheus metric: %w" , err )
708
+ }
709
+ defer closeUsersFunc ()
710
+
711
+ closeWorkspacesFunc , err := prometheusmetrics .Workspaces (ctx , options .PrometheusRegistry , options .Database , 0 )
712
+ if err != nil {
713
+ return xerrors .Errorf ("register workspaces prometheus metric: %w" , err )
714
+ }
715
+ defer closeWorkspacesFunc ()
716
+
704
717
// Agent metrics require reference to the tailnet coordinator, so must be initiated after Coder API.
705
718
closeAgentsFunc , err := prometheusmetrics .Agents (ctx , logger , options .PrometheusRegistry , coderAPI .Database , & coderAPI .TailnetCoordinator , options .DERPMap , coderAPI .Options .AgentInactiveDisconnectTimeout , 0 )
706
719
if err != nil {
@@ -720,7 +733,6 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
720
733
},
721
734
}
722
735
}
723
- defer client .HTTPClient .CloseIdleConnections ()
724
736
725
737
// This is helpful for tests, but can be silently ignored.
726
738
// Coder may be ran as users that don't have permission to write in the homedir,
@@ -819,6 +831,11 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
819
831
}
820
832
}()
821
833
834
+ autobuildPoller := time .NewTicker (cfg .AutobuildPollInterval .Value ())
835
+ defer autobuildPoller .Stop ()
836
+ autobuildExecutor := executor .New (ctx , options .Database , coderAPI .TemplateScheduleStore , logger , autobuildPoller .C )
837
+ autobuildExecutor .Run ()
838
+
822
839
cliui .Infof (inv .Stdout , "\n ==> Logs will stream in below (press ctrl+c to gracefully exit):" )
823
840
824
841
// Updates the systemd status from activating to activated.
@@ -827,11 +844,6 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
827
844
return xerrors .Errorf ("notify systemd: %w" , err )
828
845
}
829
846
830
- autobuildPoller := time .NewTicker (cfg .AutobuildPollInterval .Value ())
831
- defer autobuildPoller .Stop ()
832
- autobuildExecutor := executor .New (ctx , options .Database , coderAPI .TemplateScheduleStore , logger , autobuildPoller .C )
833
- autobuildExecutor .Run ()
834
-
835
847
// Currently there is no way to ask the server to shut
836
848
// itself down, so any exit signal will result in a non-zero
837
849
// exit of the server.
@@ -1214,9 +1226,10 @@ type CommonServerCmd struct {
1214
1226
HTTPServers * HTTPServers
1215
1227
HTTPClient * http.Client
1216
1228
LocalURL * url.URL
1217
- Logger slog.Logger
1218
1229
1219
- Tracer trace.TracerProvider
1230
+ Logger slog.Logger
1231
+ PrometheusRegistry * prometheus.Registry
1232
+ Tracer trace.TracerProvider
1220
1233
// SQLDriver is the driver that the tracer is active on.
1221
1234
SQLDriver string
1222
1235
@@ -1328,12 +1341,6 @@ func SetupServerCmd(inv *clibase.Invocation, cfg *codersdk.DeploymentValues) (_
1328
1341
// A newline is added before for visibility in terminal output.
1329
1342
cliui .Infof (inv .Stdout , "\n View the Web UI: %s" , cfg .AccessURL .String ())
1330
1343
1331
- // Used for zero-trust instance identity with Google Cloud.
1332
- googleTokenValidator , err := idtoken .NewValidator (ctx , option .WithoutAuthentication ())
1333
- if err != nil {
1334
- return nil , err
1335
- }
1336
-
1337
1344
c .AppHostname = cfg .WildcardAccessURL .String ()
1338
1345
if c .AppHostname != "" {
1339
1346
c .AppHostnameRegex , err = httpapi .CompileHostnamePattern (c .AppHostname )
@@ -1347,6 +1354,29 @@ func SetupServerCmd(inv *clibase.Invocation, cfg *codersdk.DeploymentValues) (_
1347
1354
return nil , xerrors .Errorf ("parse real ip config: %w" , err )
1348
1355
}
1349
1356
1357
+ if cfg .Pprof .Enable {
1358
+ // This prevents the pprof import from being accidentally deleted.
1359
+ // pprof has an init function that attaches itself to the default handler.
1360
+ // By passing a nil handler to 'serverHandler', it will automatically use
1361
+ // the default, which has pprof attached.
1362
+ _ = pprof .Handler
1363
+ //nolint:revive
1364
+ closeFunc := serveHandler (ctx , logger , nil , cfg .Pprof .Address .String (), "pprof" )
1365
+ c .addClose (closeFunc )
1366
+ }
1367
+
1368
+ c .PrometheusRegistry = prometheus .NewRegistry ()
1369
+ if cfg .Prometheus .Enable {
1370
+ c .PrometheusRegistry .MustRegister (collectors .NewGoCollector ())
1371
+ c .PrometheusRegistry .MustRegister (collectors .NewProcessCollector (collectors.ProcessCollectorOpts {}))
1372
+
1373
+ //nolint:revive
1374
+ closeFunc := serveHandler (ctx , logger , promhttp .InstrumentMetricHandler (
1375
+ c .PrometheusRegistry , promhttp .HandlerFor (c .PrometheusRegistry , promhttp.HandlerOpts {}),
1376
+ ), cfg .Prometheus .Address .String (), "prometheus" )
1377
+ c .addClose (closeFunc )
1378
+ }
1379
+
1350
1380
return c , nil
1351
1381
}
1352
1382
0 commit comments