Skip to content

Commit f90bed2

Browse files
committed
stop stats reporting at agent level
1 parent 3248673 commit f90bed2

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

agent/agent.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ type Options struct {
8686
PrometheusRegistry *prometheus.Registry
8787
ReportMetadataInterval time.Duration
8888
ServiceBannerRefreshInterval time.Duration
89+
ExperimentRefreshInterval time.Duration
90+
FetchExperiments func(ctx context.Context) (codersdk.Experiments, error)
8991
Syscaller agentproc.Syscaller
9092
// ModifiedProcesses is used for testing process priority management.
9193
ModifiedProcesses chan []*agentproc.Process
@@ -134,6 +136,14 @@ func New(options Options) Agent {
134136
return "", nil
135137
}
136138
}
139+
if options.FetchExperiments == nil {
140+
options.FetchExperiments = func(ctx context.Context) (codersdk.Experiments, error) {
141+
return codersdk.Experiments{}, nil
142+
}
143+
}
144+
if options.ExperimentRefreshInterval == 0 {
145+
options.ExperimentRefreshInterval = 5 * time.Minute
146+
}
137147
if options.ReportMetadataInterval == 0 {
138148
options.ReportMetadataInterval = time.Second
139149
}
@@ -167,6 +177,7 @@ func New(options Options) Agent {
167177
environmentVariables: options.EnvironmentVariables,
168178
client: options.Client,
169179
exchangeToken: options.ExchangeToken,
180+
fetchExperiments: options.FetchExperiments,
170181
filesystem: options.Filesystem,
171182
logDir: options.LogDir,
172183
tempDir: options.TempDir,
@@ -249,6 +260,10 @@ type agent struct {
249260
lifecycleStates []agentsdk.PostLifecycleRequest
250261
lifecycleLastReportedIndex int // Keeps track of the last lifecycle state we successfully reported.
251262

263+
fetchExperiments func(ctx context.Context) (codersdk.Experiments, error)
264+
fetchExperimentsInterval time.Duration
265+
experiments atomic.Pointer[codersdk.Experiments]
266+
252267
network *tailnet.Conn
253268
addresses []netip.Prefix
254269
statsReporter *statsReporter
@@ -737,6 +752,28 @@ func (a *agent) fetchServiceBannerLoop(ctx context.Context, conn drpc.Conn) erro
737752
}
738753
}
739754

755+
// fetchExperimentsLoop fetches experiments on an interval.
756+
func (a *agent) fetchExperimentsLoop(ctx context.Context) error {
757+
ticker := time.NewTicker(a.fetchExperimentsInterval)
758+
defer ticker.Stop()
759+
for {
760+
select {
761+
case <-ctx.Done():
762+
return ctx.Err()
763+
case <-ticker.C:
764+
experiments, err := a.fetchExperiments(ctx)
765+
if err != nil {
766+
if ctx.Err() != nil {
767+
return ctx.Err()
768+
}
769+
a.logger.Error(ctx, "failed to update experiments", slog.Error(err))
770+
return err
771+
}
772+
a.experiments.Store(&experiments)
773+
}
774+
}
775+
}
776+
740777
func (a *agent) run() (retErr error) {
741778
// This allows the agent to refresh it's token if necessary.
742779
// For instance identity this is required, since the instance
@@ -747,6 +784,12 @@ func (a *agent) run() (retErr error) {
747784
}
748785
a.sessionToken.Store(&sessionToken)
749786

787+
exp, err := a.fetchExperiments(a.hardCtx)
788+
if err != nil {
789+
return xerrors.Errorf("fetch experiments: %w", err)
790+
}
791+
a.experiments.Store(&exp)
792+
750793
// ConnectRPC returns the dRPC connection we use for the Agent and Tailnet v2+ APIs
751794
conn, err := a.client.ConnectRPC(a.hardCtx)
752795
if err != nil {
@@ -856,6 +899,10 @@ func (a *agent) run() (retErr error) {
856899

857900
connMan.start("fetch service banner loop", gracefulShutdownBehaviorStop, a.fetchServiceBannerLoop)
858901

902+
connMan.start("fetch experiments loop", gracefulShutdownBehaviorStop, func(ctx context.Context, _ drpc.Conn) error {
903+
return a.fetchExperimentsLoop(ctx)
904+
})
905+
859906
connMan.start("stats report loop", gracefulShutdownBehaviorStop, func(ctx context.Context, conn drpc.Conn) error {
860907
if err := networkOK.wait(ctx); err != nil {
861908
return xerrors.Errorf("no network: %w", err)

agent/stats.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"sync"
66
"time"
77

8+
"go.uber.org/atomic"
89
"golang.org/x/xerrors"
910
"tailscale.com/types/netlogtype"
1011

1112
"cdr.dev/slog"
1213
"github.com/coder/coder/v2/agent/proto"
14+
"github.com/coder/coder/v2/codersdk"
1315
)
1416

1517
const maxConns = 2048
@@ -36,9 +38,10 @@ type statsReporter struct {
3638
unreported bool
3739
lastInterval time.Duration
3840

39-
source networkStatsSource
40-
collector statsCollector
41-
logger slog.Logger
41+
source networkStatsSource
42+
collector statsCollector
43+
logger slog.Logger
44+
experiments atomic.Pointer[codersdk.Experiments]
4245
}
4346

4447
func newStatsReporter(logger slog.Logger, source networkStatsSource, collector statsCollector) *statsReporter {
@@ -112,6 +115,17 @@ func (s *statsReporter) reportLocked(
112115
s.L.Unlock()
113116
defer s.L.Lock()
114117
stats := s.collector.Collect(ctx, networkStats)
118+
119+
// if the experiment is enabled we zero out certain session stats
120+
// as we migrate to the client reporting these stats instead.
121+
if s.experiments.Load().Enabled(codersdk.ExperimentWorkspaceUsage) {
122+
stats.SessionCountSsh = 0
123+
// TODO: More session types will be enabled as we migrate over.
124+
// stats.SessionCountVscode = 0
125+
// stats.SessionCountJetbrains = 0
126+
// stats.SessionCountReconnectingPty = 0
127+
}
128+
115129
resp, err := dest.UpdateStats(ctx, &proto.UpdateStatsRequest{Stats: stats})
116130
if err != nil {
117131
return err

cli/agent.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
306306
client.SetSessionToken(resp.SessionToken)
307307
return resp.SessionToken, nil
308308
},
309+
FetchExperiments: func(ctx context.Context) (codersdk.Experiments, error) {
310+
return client.SDK.Experiments(ctx)
311+
},
309312
EnvironmentVariables: environmentVariables,
310313
IgnorePorts: ignorePorts,
311314
SSHMaxTimeout: sshMaxTimeout,

0 commit comments

Comments
 (0)