Skip to content

fix: handle new agent stat format correctly #14576

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 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
connect usage flag to metrics cache
  • Loading branch information
f0ssel committed Sep 11, 2024
commit f62203c9171f628ce8bcc10b24785e4b12492ff8
1 change: 1 addition & 0 deletions coderd/agentapi/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
workspaceAgent,
getWorkspaceAgentByIDRow.TemplateName,
req.Stats,
false,
)
if err != nil {
return nil, xerrors.Errorf("report agent stats: %w", err)
Expand Down
3 changes: 3 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,16 @@ func New(options *Options) *API {
panic(xerrors.Errorf("read site bin failed: %w", err))
}

experiments.Enabled(codersdk.ExperimentWorkspaceUsage)

metricsCache := metricscache.New(
options.Database,
options.Logger.Named("metrics_cache"),
metricscache.Intervals{
TemplateBuildTimes: options.MetricsCacheRefreshInterval,
DeploymentStats: options.AgentStatsRefreshInterval,
},
experiments.Enabled(codersdk.ExperimentWorkspaceUsage),
)

oauthConfigs := &httpmw.OAuth2Configs{
Expand Down
39 changes: 23 additions & 16 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions coderd/database/queries/workspaceagentstats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ INSERT INTO
session_count_jetbrains,
session_count_reconnecting_pty,
session_count_ssh,
connection_median_latency_ms
connection_median_latency_ms,
usage
)
SELECT
unnest(@id :: uuid[]) AS id,
Expand All @@ -36,7 +37,8 @@ SELECT
unnest(@session_count_jetbrains :: bigint[]) AS session_count_jetbrains,
unnest(@session_count_reconnecting_pty :: bigint[]) AS session_count_reconnecting_pty,
unnest(@session_count_ssh :: bigint[]) AS session_count_ssh,
unnest(@connection_median_latency_ms :: double precision[]) AS connection_median_latency_ms;
unnest(@connection_median_latency_ms :: double precision[]) AS connection_median_latency_ms,
unnest(@usage :: boolean[]) AS usage;

-- name: GetTemplateDAUs :many
SELECT
Expand Down Expand Up @@ -135,14 +137,15 @@ minute_buckets AS (
agent_id,
date_trunc('minute', created_at) AS minute_bucket,
SUM(session_count_vscode) AS session_count_vscode,
SUM(session_count_ssh) AS session_count_ssh,
SUM(session_count_jetbrains) AS session_count_jetbrains,
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty,
SUM(session_count_ssh) AS session_count_ssh
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty
FROM
workspace_agent_stats
WHERE
created_at >= $1
AND created_at < date_trunc('minute', now()) -- Exclude current partial minute
AND usage = true
GROUP BY
agent_id,
minute_bucket
Expand All @@ -164,9 +167,9 @@ latest_buckets AS (
latest_agent_stats AS (
SELECT
SUM(session_count_vscode) AS session_count_vscode,
SUM(session_count_ssh) AS session_count_ssh,
SUM(session_count_jetbrains) AS session_count_jetbrains,
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty,
SUM(session_count_ssh) AS session_count_ssh
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty
FROM
latest_buckets
)
Expand Down Expand Up @@ -223,14 +226,15 @@ minute_buckets AS (
agent_id,
date_trunc('minute', created_at) AS minute_bucket,
SUM(session_count_vscode) AS session_count_vscode,
SUM(session_count_ssh) AS session_count_ssh,
SUM(session_count_jetbrains) AS session_count_jetbrains,
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty,
SUM(session_count_ssh) AS session_count_ssh
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty
FROM
workspace_agent_stats
WHERE
created_at >= $1
AND created_at < date_trunc('minute', now()) -- Exclude current partial minute
AND usage = true
GROUP BY
agent_id,
minute_bucket,
Expand All @@ -256,9 +260,9 @@ latest_buckets AS (
SELECT
agent_id,
SUM(session_count_vscode) AS session_count_vscode,
SUM(session_count_ssh) AS session_count_ssh,
SUM(session_count_jetbrains) AS session_count_jetbrains,
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty,
SUM(session_count_ssh) AS session_count_ssh
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty
FROM
latest_buckets
)
Expand Down Expand Up @@ -336,6 +340,7 @@ WITH agent_stats AS (
coalesce(SUM(session_count_reconnecting_pty), 0)::bigint AS session_count_reconnecting_pty,
coalesce(SUM(connection_count), 0)::bigint AS connection_count
FROM workspace_agent_stats
WHERE usage = true
GROUP BY user_id, agent_id, workspace_id
), latest_agent_latencies AS (
SELECT
Expand Down
4 changes: 2 additions & 2 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
SessionCountJetbrains: stat.sessionCountJetBrains,
SessionCountReconnectingPty: stat.sessionCountReconnectingPTY,
SessionCountSsh: stat.sessionCountSSH,
})
}, false)
require.NoError(t, err, "want no error inserting agent stats")
createdAt = createdAt.Add(30 * time.Second)
}
Expand Down Expand Up @@ -1605,7 +1605,7 @@ func TestUserActivityInsights_Golden(t *testing.T) {
SessionCountJetbrains: stat.sessionCountJetBrains,
SessionCountReconnectingPty: stat.sessionCountReconnectingPTY,
SessionCountSsh: stat.sessionCountSSH,
})
}, false)
require.NoError(t, err, "want no error inserting agent stats")
createdAt = createdAt.Add(30 * time.Second)
}
Expand Down
29 changes: 24 additions & 5 deletions coderd/metricscache/metricscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ type Cache struct {

done chan struct{}
cancel func()

// usage is a experiment flag to enable new workspace usage tracking behavior and will be
// removed when the experiment is complete.
usage bool
}

type Intervals struct {
TemplateBuildTimes time.Duration
DeploymentStats time.Duration
}

func New(db database.Store, log slog.Logger, intervals Intervals) *Cache {
func New(db database.Store, log slog.Logger, intervals Intervals, usage bool) *Cache {
if intervals.TemplateBuildTimes <= 0 {
intervals.TemplateBuildTimes = time.Hour
}
Expand All @@ -56,6 +60,7 @@ func New(db database.Store, log slog.Logger, intervals Intervals) *Cache {
log: log,
done: make(chan struct{}),
cancel: cancel,
usage: usage,
}
go func() {
var wg sync.WaitGroup
Expand Down Expand Up @@ -125,11 +130,25 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error {
}

func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
from := dbtime.Now().Add(-15 * time.Minute)
agentStats, err := c.database.GetDeploymentWorkspaceAgentStats(ctx, from)
if err != nil {
return err
var (
from = dbtime.Now().Add(-15 * time.Minute)
agentStats database.GetDeploymentWorkspaceAgentStatsRow
err error
)

if c.usage {
agentUsageStats, err := c.database.GetDeploymentWorkspaceAgentUsageStats(ctx, from)
if err != nil {
return err
}
agentStats = database.GetDeploymentWorkspaceAgentStatsRow(agentUsageStats)
} else {
agentStats, err = c.database.GetDeploymentWorkspaceAgentStats(ctx, from)
if err != nil {
return err
}
}

workspaceStats, err := c.database.GetDeploymentWorkspaceStats(ctx)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions coderd/metricscache/metricscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestCache_TemplateWorkspaceOwners(t *testing.T) {
db = dbmem.New()
cache = metricscache.New(db, slogtest.Make(t, nil), metricscache.Intervals{
TemplateBuildTimes: testutil.IntervalFast,
})
}, false)
)

defer cache.Close()
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestCache_BuildTime(t *testing.T) {
db = dbmem.New()
cache = metricscache.New(db, slogtest.Make(t, nil), metricscache.Intervals{
TemplateBuildTimes: testutil.IntervalFast,
})
}, false)
)

defer cache.Close()
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestCache_DeploymentStats(t *testing.T) {
db := dbmem.New()
cache := metricscache.New(db, slogtest.Make(t, nil), metricscache.Intervals{
DeploymentStats: testutil.IntervalFast,
})
}, false)
defer cache.Close()

err := db.InsertWorkspaceAgentStats(context.Background(), database.InsertWorkspaceAgentStatsParams{
Expand Down
2 changes: 1 addition & 1 deletion coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ func (api *API) postWorkspaceUsage(rw http.ResponseWriter, r *http.Request) {
return
}

err = api.statsReporter.ReportAgentStats(ctx, dbtime.Now(), workspace, agent, template.Name, stat)
err = api.statsReporter.ReportAgentStats(ctx, dbtime.Now(), workspace, agent, template.Name, stat, true)
if err != nil {
httpapi.InternalServerError(rw, err)
return
Expand Down
Loading
Loading