Skip to content

Commit ef4d1b6

Browse files
authored
test: insights metrics: verify plugin usage (#11156)
1 parent 8b8a763 commit ef4d1b6

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

coderd/prometheusmetrics/insights/metricscollector_test.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import (
1717

1818
"cdr.dev/slog"
1919
"cdr.dev/slog/sloggers/slogtest"
20+
"github.com/coder/coder/v2/agent"
2021
"github.com/coder/coder/v2/agent/agenttest"
2122
"github.com/coder/coder/v2/coderd/coderdtest"
2223
"github.com/coder/coder/v2/coderd/database/dbauthz"
2324
"github.com/coder/coder/v2/coderd/database/dbtestutil"
2425
"github.com/coder/coder/v2/coderd/prometheusmetrics/insights"
2526
"github.com/coder/coder/v2/coderd/workspaceapps"
2627
"github.com/coder/coder/v2/codersdk"
28+
"github.com/coder/coder/v2/codersdk/agentsdk"
2729
"github.com/coder/coder/v2/provisioner/echo"
2830
"github.com/coder/coder/v2/provisionersdk/proto"
2931
"github.com/coder/coder/v2/testutil"
@@ -78,9 +80,28 @@ func TestCollectInsights(t *testing.T) {
7880
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
7981

8082
// Start an agent so that we can generate stats.
81-
_ = agenttest.New(t, client.URL, authToken)
83+
agentClient := agentsdk.New(client.URL)
84+
agentClient.SetSessionToken(authToken)
85+
agentClient.SDK.SetLogger(logger.Leveled(slog.LevelDebug).Named("agent"))
86+
87+
_ = agenttest.New(t, client.URL, authToken, func(o *agent.Options) {
88+
o.Client = agentClient
89+
})
8290
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
8391

92+
// Fake app stats
93+
_, err = agentClient.PostStats(context.Background(), &agentsdk.Stats{
94+
// ConnectionsByProto can't be nil, otherwise stats get rejected
95+
ConnectionsByProto: map[string]int64{"TCP": 1},
96+
// ConnectionCount must be positive as database query ignores stats with no active connections at the time frame
97+
ConnectionCount: 74,
98+
// SessionCountJetBrains, SessionCountVSCode must be positive, but the exact value is ignored.
99+
// Database query approximates it to 60s of usage.
100+
SessionCountJetBrains: 47,
101+
SessionCountVSCode: 34,
102+
})
103+
require.NoError(t, err, "unable to post fake stats")
104+
84105
// Fake app usage
85106
reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize)
86107
//nolint:gocritic // This is a test.

coderd/prometheusmetrics/insights/testdata/insights-metrics.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"coderd_insights_applications_usage_seconds[application_name=JetBrains,slug=,template_name=golden-template]": 0,
3-
"coderd_insights_applications_usage_seconds[application_name=Visual Studio Code,slug=,template_name=golden-template]": 0,
2+
"coderd_insights_applications_usage_seconds[application_name=JetBrains,slug=,template_name=golden-template]": 60,
3+
"coderd_insights_applications_usage_seconds[application_name=Visual Studio Code,slug=,template_name=golden-template]": 60,
44
"coderd_insights_applications_usage_seconds[application_name=Web Terminal,slug=,template_name=golden-template]": 0,
55
"coderd_insights_applications_usage_seconds[application_name=SSH,slug=,template_name=golden-template]": 60,
66
"coderd_insights_applications_usage_seconds[application_name=Golden Slug,slug=golden-slug,template_name=golden-template]": 120,

0 commit comments

Comments
 (0)