Skip to content

feat: expose agent metrics via Prometheus endpoint #7011

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 16 commits into from
Apr 7, 2023
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
mtojek committed Apr 5, 2023
commit f8d6f464fe93d3f7799463be9c6e0c9c6c6f5135
40 changes: 40 additions & 0 deletions coderd/prometheusmetrics/prometheusmetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ import (
"testing"
"time"

"sync/atomic"

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

"cdr.dev/slog/sloggers/slogtest"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbfake"
"github.com/coder/coder/coderd/database/dbgen"
"github.com/coder/coder/coderd/prometheusmetrics"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/tailnet"
"github.com/coder/coder/tailnet/tailnettest"
"github.com/coder/coder/testutil"
)

Expand Down Expand Up @@ -239,3 +245,37 @@ func TestWorkspaces(t *testing.T) {
})
}
}

func TestAgents(t *testing.T) {
t.Parallel()

// given
db := dbfake.New()

coordinator := tailnet.NewCoordinator()
coordinatorPtr := atomic.Pointer[tailnet.Coordinator]{}
coordinatorPtr.Store(&coordinator)
derpMap := tailnettest.RunDERPAndSTUN(t)
agentInactiveDisconnectTimeout := 1 * time.Hour
registry := prometheus.NewRegistry()

// when
cancel, err := prometheusmetrics.Agents(context.Background(), slogtest.Make(t, nil), registry, db, &coordinatorPtr, derpMap, agentInactiveDisconnectTimeout, time.Millisecond)
t.Cleanup(cancel)

// then
require.NoError(t, err)
require.Eventually(t, func() bool {
metrics, err := registry.Gather()
assert.NoError(t, err)

if len(metrics) < 1 {
return false
}

for _, metric := range metrics[0].Metric {
panic(metric)
}
return true
}, testutil.WaitShort, testutil.IntervalFast)
}