Skip to content

Daily Active User Metrics #3735

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 28 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
502aa52
agent: add ConnStats
ammario Aug 29, 2022
3893045
agent: add StatsReporter
ammario Aug 29, 2022
c03ad90
Frontend tests pass
ammario Sep 1, 2022
1bd9cec
Split DAUChart into its own file
ammario Sep 1, 2022
e46329b
Get FE tests passing with real data!
ammario Sep 1, 2022
d472b13
Test frontend
ammario Sep 1, 2022
e0295e0
Fix compilation error
ammario Sep 1, 2022
2f1a423
Rename ConnStats to StatsConn
ammario Sep 1, 2022
0a50cc9
continues instead of returns
ammario Sep 1, 2022
7feab5e
Fix some test failures
ammario Sep 1, 2022
a4d2cf7
Redo tests
ammario Sep 1, 2022
52c9d10
Address review comments
ammario Sep 1, 2022
3f9901e
REVAMP — backend tests work
ammario Sep 1, 2022
7840509
Black triangle
ammario Sep 1, 2022
39170cf
Consolidate template state machines
ammario Sep 1, 2022
eb373d6
Move workspaceagent endpoint
ammario Sep 1, 2022
a3d87b8
Address most review comments
ammario Sep 1, 2022
31ba0c6
Improve contrast in chart
ammario Sep 1, 2022
5b906c1
Add more agent tests
ammario Sep 1, 2022
49d9386
Fix JS ci
ammario Sep 1, 2022
b14a077
A bunch of minor touch ups
ammario Sep 1, 2022
8da24c4
Stabilize protoc
ammario Sep 1, 2022
00ec953
Merge remote-tracking branch 'origin/main' into metrics
ammario Sep 1, 2022
4940319
Update lockfile
ammario Sep 1, 2022
22b2028
Address comments + attempt to fix protoc
ammario Sep 1, 2022
0157365
fixup! Address comments + attempt to fix protoc
ammario Sep 1, 2022
b166cdd
Try to fix protoc...
ammario Sep 1, 2022
4201998
PROTO!
ammario Sep 1, 2022
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
Prev Previous commit
Next Next commit
Address review comments
  • Loading branch information
ammario committed Sep 1, 2022
commit 52c9d103f4e419bf85aec77207fdcda7b7dec344
1 change: 1 addition & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (a *agent) runTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) {
}
a.network.SetForwardTCPCallback(func(conn net.Conn, listenerExists bool) net.Conn {
if listenerExists {
// If a listener already exists, we would double-wrap the conn.
return conn
}
return a.stats.wrapConn(conn)
Expand Down
6 changes: 3 additions & 3 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command {
spooky bool
verbose bool
metricsCacheRefreshInterval time.Duration
agentStatReportInterval time.Duration
agentStatRefreshInterval time.Duration
)

root := &cobra.Command{
Expand Down Expand Up @@ -363,7 +363,7 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command {
Telemetry: telemetry.NewNoop(),
AutoImportTemplates: validatedAutoImportTemplates,
MetricsCacheRefreshInterval: metricsCacheRefreshInterval,
AgentStatsReportInterval: agentStatReportInterval,
AgentStatsRefreshInterval: agentStatRefreshInterval,
}

if oauth2GithubClientSecret != "" {
Expand Down Expand Up @@ -843,7 +843,7 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command {

cliflag.DurationVarP(root.Flags(), &metricsCacheRefreshInterval, "metrics-cache-refresh-interval", "", "CODER_METRICS_CACHE_REFRESH_INTERVAL", time.Hour, "How frequently metrics are refreshed")
_ = root.Flags().MarkHidden("metrics-cache-refresh-interval")
cliflag.DurationVarP(root.Flags(), &agentStatReportInterval, "agent-stats-report-interval", "", "CODER_AGENT_STATS_REPORT_INTERVAL", time.Minute*10, "How frequently agent stats are recorded")
cliflag.DurationVarP(root.Flags(), &agentStatRefreshInterval, "agent-stats-refresh-interval", "", "CODER_AGENT_STATS_REFRESH_INTERVAL", time.Minute*10, "How frequently agent stats are recorded")
_ = root.Flags().MarkHidden("agent-stats-report-interval")

return root
Expand Down
4 changes: 2 additions & 2 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type Options struct {

// Metrics related intervals.
MetricsCacheRefreshInterval time.Duration
AgentStatsReportInterval time.Duration
AgentStatsRefreshInterval time.Duration
}

// New constructs a Coder API handler.
Expand Down Expand Up @@ -378,7 +378,7 @@ func New(options *Options) *API {
r.Use(
apiKeyMiddleware,
)
r.Get("/daus", api.daus)
r.Get("/daus", api.metricsDAUs)
})
})
r.Route("/workspaceagents", func(r chi.Router) {
Expand Down
2 changes: 1 addition & 1 deletion coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func newWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *c
},
AutoImportTemplates: options.AutoImportTemplates,
MetricsCacheRefreshInterval: time.Millisecond * 100,
AgentStatsReportInterval: time.Millisecond * 100,
AgentStatsRefreshInterval: time.Millisecond * 100,
})
t.Cleanup(func() {
_ = coderAPI.Close()
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (q *fakeQuerier) AcquireProvisionerJob(_ context.Context, arg database.Acqu
}
return database.ProvisionerJob{}, sql.ErrNoRows
}
func (q *fakeQuerier) DeleteOldAgentStats(_ context.Context) error {
func (_ *fakeQuerier) DeleteOldAgentStats(_ context.Context) error {
// no-op
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions coderd/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/coder/coder/codersdk"
)

func (api *API) daus(rw http.ResponseWriter, r *http.Request) {
func (api *API) metricsDAUs(rw http.ResponseWriter, r *http.Request) {
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceUser) {
httpapi.Forbidden(rw)
return
Expand Down Expand Up @@ -78,7 +78,7 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques

// Allow overriding the stat interval for debugging and testing purposes.
ctx := r.Context()
timer := time.NewTicker(api.AgentStatsReportInterval)
timer := time.NewTicker(api.AgentStatsRefreshInterval)
var lastReport codersdk.AgentStatsReportResponse
for {
err := wsjson.Write(ctx, conn, codersdk.AgentStatsReportRequest{})
Expand Down Expand Up @@ -113,7 +113,7 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
var insert = !reflect.DeepEqual(lastReport, rep)

api.Logger.Debug(ctx, "read stats report",
slog.F("interval", api.AgentStatsReportInterval),
slog.F("interval", api.AgentStatsRefreshInterval),
slog.F("agent", workspaceAgent.ID),
slog.F("resource", resource.ID),
slog.F("workspace", workspace.ID),
Expand Down
21 changes: 12 additions & 9 deletions coderd/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

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

"cdr.dev/slog/sloggers/slogtest"
Expand Down Expand Up @@ -95,20 +96,22 @@ func TestMetrics(t *testing.T) {
_, err = session.Output("echo hello")
require.NoError(t, err)

// Give enough time for stats to hit DB
// and metrics cache to refresh.
time.Sleep(time.Second * 5)

daus, err = client.GetDAUsFromAgentStats(context.Background())
require.NoError(t, err)

require.Equal(t, &codersdk.DAUsResponse{
want := &codersdk.DAUsResponse{
Entries: []codersdk.DAUEntry{
{

Date: time.Now().UTC().Truncate(time.Hour * 24),
DAUs: 1,
},
},
}, daus)
}
require.Eventuallyf(t, func() bool {
daus, err = client.GetDAUsFromAgentStats(context.Background())
require.NoError(t, err)

return assert.ObjectsAreEqual(want, daus)
},
testutil.WaitShort, testutil.IntervalFast,
"got %+v != %+v", daus, want,
)
}
7 changes: 5 additions & 2 deletions coderd/metricscache/metricscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/coder/retry"
)

// Cache holds the DAU cache and, later, the
// user activity cache. The aggregation queries responsible for these values
// Cache holds the DAU cache.
// The aggregation queries responsible for these values
// can take up to a minute on large deployments, but the cache has near zero
// effect on most deployments.
type Cache struct {
Expand Down Expand Up @@ -110,6 +110,9 @@ func (c *Cache) run(ctx context.Context) {
start := time.Now()
err := c.refresh(ctx)
if err != nil {
if ctx.Err() != nil {
return
}
c.log.Error(ctx, "refresh", slog.Error(err))
continue
}
Expand Down
14 changes: 9 additions & 5 deletions coderd/metricscache/metricscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/databasefake"
"github.com/coder/coder/coderd/metricscache"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/testutil"
)

func date(year, month, day int) time.Time {
Expand Down Expand Up @@ -164,12 +166,14 @@ func TestCache(t *testing.T) {
db.InsertAgentStat(context.Background(), row)
}

// Wait for value to refresh..
time.Sleep(time.Second)
var got codersdk.DAUsResponse

if got := cache.DAUs(); !reflect.DeepEqual(got.Entries, tt.want) {
t.Errorf("GetDAUs() = %v, want %v", got, tt.want)
}
require.Eventuallyf(t, func() bool {
got = cache.DAUs()
return reflect.DeepEqual(got.Entries, tt.want)
}, testutil.WaitShort, testutil.IntervalFast,
"GetDAUs() = %v, want %v", got, tt.want,
)
})
}
}
6 changes: 6 additions & 0 deletions codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,9 @@ func (e *Error) Error() string {
}
return builder.String()
}

type CloseFunc func() error

func (c CloseFunc) Close() error {
return c()
}
6 changes: 0 additions & 6 deletions codersdk/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
"github.com/coder/retry"
)

type CloseFunc func() error

func (c CloseFunc) Close() error {
return c()
}

// AgentReportStats begins a stat streaming connection with the Coder server.
// It is resilient to network failures and intermittent coderd issues.
func (c *Client) AgentReportStats(
Expand Down