From 46beedee6545925af248b730ab05cf7ec0b991a8 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 12 Dec 2023 14:11:56 +0100 Subject: [PATCH 1/3] post fake stats --- .../insights/metricscollector_test.go | 20 ++++++++++++++++++- .../insights/testdata/insights-metrics.json | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/coderd/prometheusmetrics/insights/metricscollector_test.go b/coderd/prometheusmetrics/insights/metricscollector_test.go index 05619a2965336..93759c07b526c 100644 --- a/coderd/prometheusmetrics/insights/metricscollector_test.go +++ b/coderd/prometheusmetrics/insights/metricscollector_test.go @@ -17,6 +17,7 @@ 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" @@ -24,6 +25,7 @@ import ( "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,25 @@ 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: map[string]int64{"TCP": 1}, + ConnectionCount: 74, + ConnectionMedianLatencyMS: 774, + + SessionCountJetBrains: 47, + }) + require.NoError(t, err, "unable to submit fake stats") + // Fake app usage reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize) //nolint:gocritic // This is a test. diff --git a/coderd/prometheusmetrics/insights/testdata/insights-metrics.json b/coderd/prometheusmetrics/insights/testdata/insights-metrics.json index 2923565c97de4..b3a3059042f2d 100644 --- a/coderd/prometheusmetrics/insights/testdata/insights-metrics.json +++ b/coderd/prometheusmetrics/insights/testdata/insights-metrics.json @@ -1,5 +1,5 @@ { - "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=Web Terminal,slug=,template_name=golden-template]": 0, "coderd_insights_applications_usage_seconds[application_name=SSH,slug=,template_name=golden-template]": 60, From 695f04458f75ea206e66e7be576e0670816ac8b3 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 12 Dec 2023 15:03:10 +0100 Subject: [PATCH 2/3] more comments --- .../insights/metricscollector_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/coderd/prometheusmetrics/insights/metricscollector_test.go b/coderd/prometheusmetrics/insights/metricscollector_test.go index 93759c07b526c..a69b36f0ff1fe 100644 --- a/coderd/prometheusmetrics/insights/metricscollector_test.go +++ b/coderd/prometheusmetrics/insights/metricscollector_test.go @@ -91,13 +91,15 @@ func TestCollectInsights(t *testing.T) { // Fake app stats _, err = agentClient.PostStats(context.Background(), &agentsdk.Stats{ - ConnectionsByProto: map[string]int64{"TCP": 1}, - ConnectionCount: 74, - ConnectionMedianLatencyMS: 774, - + // 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 must be positive, but the exact value is ignored. + // Database query approximates it to 60s of usage. SessionCountJetBrains: 47, }) - require.NoError(t, err, "unable to submit fake stats") + require.NoError(t, err, "unable to post fake stats") // Fake app usage reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize) From 15ea095fcc90232ec511a93ce0f768f4f4351f35 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 12 Dec 2023 15:10:51 +0100 Subject: [PATCH 3/3] Same for VS code --- coderd/prometheusmetrics/insights/metricscollector_test.go | 3 ++- .../prometheusmetrics/insights/testdata/insights-metrics.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/coderd/prometheusmetrics/insights/metricscollector_test.go b/coderd/prometheusmetrics/insights/metricscollector_test.go index a69b36f0ff1fe..47591815a52bc 100644 --- a/coderd/prometheusmetrics/insights/metricscollector_test.go +++ b/coderd/prometheusmetrics/insights/metricscollector_test.go @@ -95,9 +95,10 @@ func TestCollectInsights(t *testing.T) { 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 must be positive, but the exact value is ignored. + // 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") diff --git a/coderd/prometheusmetrics/insights/testdata/insights-metrics.json b/coderd/prometheusmetrics/insights/testdata/insights-metrics.json index b3a3059042f2d..13753474e5c42 100644 --- a/coderd/prometheusmetrics/insights/testdata/insights-metrics.json +++ b/coderd/prometheusmetrics/insights/testdata/insights-metrics.json @@ -1,6 +1,6 @@ { "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=SSH,slug=,template_name=golden-template]": 60, "coderd_insights_applications_usage_seconds[application_name=Golden Slug,slug=golden-slug,template_name=golden-template]": 120,