mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
test: insights metrics: verify plugin usage (#11156)
This commit is contained in:
@ -17,6 +17,7 @@ import (
|
|||||||
|
|
||||||
"cdr.dev/slog"
|
"cdr.dev/slog"
|
||||||
"cdr.dev/slog/sloggers/slogtest"
|
"cdr.dev/slog/sloggers/slogtest"
|
||||||
|
"github.com/coder/coder/v2/agent"
|
||||||
"github.com/coder/coder/v2/agent/agenttest"
|
"github.com/coder/coder/v2/agent/agenttest"
|
||||||
"github.com/coder/coder/v2/coderd/coderdtest"
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
||||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||||
@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/coder/coder/v2/coderd/prometheusmetrics/insights"
|
"github.com/coder/coder/v2/coderd/prometheusmetrics/insights"
|
||||||
"github.com/coder/coder/v2/coderd/workspaceapps"
|
"github.com/coder/coder/v2/coderd/workspaceapps"
|
||||||
"github.com/coder/coder/v2/codersdk"
|
"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/provisioner/echo"
|
||||||
"github.com/coder/coder/v2/provisionersdk/proto"
|
"github.com/coder/coder/v2/provisionersdk/proto"
|
||||||
"github.com/coder/coder/v2/testutil"
|
"github.com/coder/coder/v2/testutil"
|
||||||
@ -78,9 +80,28 @@ func TestCollectInsights(t *testing.T) {
|
|||||||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
|
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
|
||||||
|
|
||||||
// Start an agent so that we can generate stats.
|
// 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)
|
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
|
// Fake app usage
|
||||||
reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize)
|
reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize)
|
||||||
//nolint:gocritic // This is a test.
|
//nolint:gocritic // This is a test.
|
||||||
|
@ -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=JetBrains,slug=,template_name=golden-template]": 60,
|
||||||
"coderd_insights_applications_usage_seconds[application_name=Visual Studio Code,slug=,template_name=golden-template]": 0,
|
"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=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=SSH,slug=,template_name=golden-template]": 60,
|
||||||
"coderd_insights_applications_usage_seconds[application_name=Golden Slug,slug=golden-slug,template_name=golden-template]": 120,
|
"coderd_insights_applications_usage_seconds[application_name=Golden Slug,slug=golden-slug,template_name=golden-template]": 120,
|
||||||
|
Reference in New Issue
Block a user