Files
coder/coderd/agentapi/stats.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.6 KiB
Go
Raw Normal View History

2023-12-18 04:53:28 -08:00
package agentapi
import (
"context"
"time"
"github.com/google/uuid"
2023-12-18 04:53:28 -08:00
"golang.org/x/xerrors"
"google.golang.org/protobuf/types/known/durationpb"
"cdr.dev/slog/v3"
2023-12-18 04:53:28 -08:00
agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/workspacestats"
"github.com/coder/coder/v2/codersdk"
2023-12-18 04:53:28 -08:00
)
type StatsAPI struct {
AgentID uuid.UUID
AgentName string
Workspace *CachedWorkspaceFields
2023-12-18 04:53:28 -08:00
Database database.Store
Log slog.Logger
StatsReporter *workspacestats.Reporter
2023-12-18 04:53:28 -08:00
AgentStatsRefreshInterval time.Duration
Experiments codersdk.Experiments
2024-01-26 17:04:19 +10:00
TimeNowFn func() time.Time // defaults to dbtime.Now()
}
func (a *StatsAPI) now() time.Time {
if a.TimeNowFn != nil {
return a.TimeNowFn()
}
return dbtime.Now()
2023-12-18 04:53:28 -08:00
}
func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsRequest) (*agentproto.UpdateStatsResponse, error) {
2024-01-26 17:04:19 +10:00
res := &agentproto.UpdateStatsResponse{
ReportInterval: durationpb.New(a.AgentStatsRefreshInterval),
}
// An empty stat means it's just looking for the report interval.
if req.Stats == nil {
2024-01-26 17:04:19 +10:00
return res, nil
}
// If cache is empty (prebuild or invalid), fall back to DB
var ws database.WorkspaceIdentity
var ok bool
if ws, ok = a.Workspace.AsWorkspaceIdentity(); !ok {
w, err := a.Database.GetWorkspaceByAgentID(ctx, a.AgentID)
if err != nil {
return nil, xerrors.Errorf("get workspace by agent ID %q: %w", a.AgentID, err)
}
ws = database.WorkspaceIdentityFromWorkspace(w)
2023-12-18 04:53:28 -08:00
}
// The payload is unnormalized and unbounded until the batcher caps it, so
// log scalars rather than its contents.
2023-12-18 04:53:28 -08:00
a.Log.Debug(ctx, "read stats report",
slog.F("interval", a.AgentStatsRefreshInterval),
slog.F("workspace_id", ws.ID),
slog.F("connection_count", req.Stats.GetConnectionCount()),
slog.F("session_count_keys", len(req.Stats.GetSessionCounts())),
slog.F("connections_by_proto_keys", len(req.Stats.GetConnectionsByProto())),
2023-12-18 04:53:28 -08:00
)
if a.Experiments.Enabled(codersdk.ExperimentWorkspaceUsage) {
// while the experiment is enabled we will not report
// session stats from the agent. This is because it is
// being handled by the CLI and the postWorkspaceUsage route.
workspacestats.ClearSessionCounts(req.Stats)
}
err := a.StatsReporter.ReportAgentStats(
ctx,
a.now(),
ws,
a.AgentID,
a.AgentName,
req.Stats,
false,
)
2023-12-18 04:53:28 -08:00
if err != nil {
return nil, xerrors.Errorf("report agent stats: %w", err)
2023-12-18 04:53:28 -08:00
}
return res, nil
}