8000 feat: expose app insights as Prometheus metrics by mtojek · Pull Request #10346 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: expose app insights as Prometheus metrics #10346

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 20 commits into from
Nov 7, 2023
Merged
Prev Previous commit
Next Next commit
Unit test
  • Loading branch information
mtojek committed Nov 7, 2023
commit b2deb7adae4233a4484ed210662c834d4181e34a
36 changes: 25 additions & 11 deletions coderd/prometheusmetrics/insights/metricscollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"encoding/json"
"io"
"os"
"strings"
"testing"
"time"

"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -23,7 +25,7 @@ import (
"github.com/coder/coder/v2/testutil"
)

func TestCollect_TemplateInsights(t *testing.T) {
func TestCollectInsights(t *testing.T) {
t.Parallel()

logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
Expand Down Expand Up @@ -55,7 +57,9 @@ func TestCollect_TemplateInsights(t *testing.T) {
ProvisionPlan: echo.PlanComplete,
ProvisionApply: echo.ProvisionApplyWithAgent(authToken),
})
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
ctr.Name = "golden-template"
})
require.Empty(t, template.BuildTimeStats[codersdk.WorkspaceTransitionStart])

coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
Expand Down Expand Up @@ -97,6 +101,11 @@ func TestCollect_TemplateInsights(t *testing.T) {
err = sess.Start("cat")
require.NoError(t, err)

defer func() {
_ = sess.Close()
_ = sshConn.Close()
}()

goldenFile, err := os.ReadFile("testdata/insights-metrics.json")
require.NoError(t, err)
golden := map[string]int{}
Expand All @@ -112,22 +121,27 @@ func TestCollect_TemplateInsights(t *testing.T) {
// Then
for _, metric := range metrics {
switch metric.GetName() {
case "coderd_insights_applications_usage_seconds": // metric is valid, but it can't be verified using golden files
case "coderd_insights_templates_active_users":
case "coderd_insights_applications_usage_seconds", "coderd_insights_templates_active_users":
for _, m := range metric.Metric {
collected[metric.GetName()] = int(m.Gauge.GetValue())
key := metric.GetName()
if len(m.Label) > 0 {
key = key + "[" + metricLabelAsString(m) + "]"
}
collected[key] = int(m.Gauge.GetValue())
}
default:
require.FailNowf(t, "unexpected metric collected", "metric: %s", metric.GetName())
}
}

return assert.ObjectsAreEqualValues(golden, collected)
}, testutil.WaitMedium, testutil.IntervalFast, "template insights are missing")

// We got our latency metrics, close the connection.
_ = sess.Close()
_ = sshConn.Close()
}, testutil.WaitMedium, testutil.IntervalFast, "template insights are inconsistent with golden files, got: %v", collected)
}

require.EqualValues(t, golden, collected)
func metricLabelAsString(m *io_prometheus_client.Metric) string {
var labels []string
for _, labelPair := range m.Label {
labels = append(labels, labelPair.GetName()+"="+labelPair.GetValue())
}
return strings.Join(labels, ",")
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"coderd_insights_templates_active_users": 1
"coderd_insights_applications_usage_seconds[application_name=SSH,template_name=golden-template]": 60,
"coderd_insights_templates_active_users[template_name=golden-template]": 1
}
0