Skip to content

test: insights metrics: verify plugin usage #11156

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
Dec 13, 2023
PHP-Proxy

PHP-Proxy

Error accessing resource: 429 - Too Many Requests

Merged
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
23 changes: 22 additions & 1 deletion coderd/prometheusmetrics/insights/metricscollector_test.go
Original file line number Diff line number Diff line change
@@ -17,13 +17,15 @@ import (

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/agent"
"github.com/coder/coder/v2/agent/agenttest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/prometheusmetrics/insights"
"github.com/coder/coder/v2/coderd/workspaceapps"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/coder/coder/v2/provisioner/echo"
"github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/coder/v2/testutil"
@@ -78,9 +80,28 @@ func TestCollectInsights(t *testing.T) {
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)

// Start an agent so that we can generate stats.
_ = agenttest.New(t, client.URL, authToken)
agentClient := agentsdk.New(client.URL)
agentClient.SetSessionToken(authToken)
agentClient.SDK.SetLogger(logger.Leveled(slog.LevelDebug).Named("agent"))

_ = agenttest.New(t, client.URL, authToken, func(o *agent.Options) {
o.Client = agentClient
})
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)

// Fake app stats
_, err = agentClient.PostStats(context.Background(), &agentsdk.Stats{
// ConnectionsByProto can't be nil, otherwise stats get rejected
ConnectionsByProto: map[string]int64{"TCP": 1},
// ConnectionCount must be positive as database query ignores stats with no active connections at the time frame
ConnectionCount: 74,
// SessionCountJetBrains, SessionCountVSCode must be positive, but the exact value is ignored.
// Database query approximates it to 60s of usage.
SessionCountJetBrains: 47,
SessionCountVSCode: 34,
})
require.NoError(t, err, "unable to post fake stats")

// Fake app usage
reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize)
//nolint:gocritic // This is a test.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"coderd_insights_applications_usage_seconds[application_name=JetBrains,slug=,template_name=golden-template]": 0,
"coderd_insights_applications_usage_seconds[application_name=Visual Studio Code,slug=,template_name=golden-template]": 0,
"coderd_insights_applications_usage_seconds[application_name=JetBrains,slug=,template_name=golden-template]": 60,
"coderd_insights_applications_usage_seconds[application_name=Visual Studio Code,slug=,template_name=golden-template]": 60,
"coderd_insights_applications_usage_seconds[application_name=Web Terminal,slug=,template_name=golden-template]": 0,
"coderd_insights_applications_usage_seconds[application_name=SSH,slug=,template_name=golden-template]": 60,
"coderd_insights_applications_usage_seconds[application_name=Golden Slug,slug=golden-slug,template_name=golden-template]": 120,