1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
package server
import (
"context"
"time"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/gitlab"
gapi "gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/gitlab/api"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modserver"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modshared"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/usage_metrics"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/errz"
"go.uber.org/zap"
)
type module struct {
log *zap.Logger
api modserver.Api
usageTracker usage_metrics.UsageTrackerCollector
gitLabClient gitlab.ClientInterface
usageReportingPeriod time.Duration
}
func (m *module) Run(ctx context.Context) error {
if m.usageReportingPeriod == 0 {
return nil
}
ticker := time.NewTicker(m.usageReportingPeriod)
defer ticker.Stop()
done := ctx.Done()
for {
select {
case <-done:
ctxExit, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// Flush metrics before exiting
m.sendUsage(ctxExit) // nolint: contextcheck
cancel()
return nil
case <-ticker.C:
m.sendUsage(ctx)
}
}
}
func (m *module) sendUsage(ctx context.Context) {
if err := m.sendUsageInternal(ctx); err != nil {
if !errz.ContextDone(err) {
m.api.HandleProcessingError(ctx, m.log, modshared.NoAgentId, "Failed to send usage data", err)
}
}
}
func (m *module) sendUsageInternal(ctx context.Context) error {
usageData := m.usageTracker.CloneUsageData()
if usageData.IsEmpty() {
return nil
}
data := gapi.UsagePingData{
Counters: usageData.Counters,
UniqueCounters: usageData.UniqueCounters,
}
err := gapi.SendUsagePing(ctx, m.gitLabClient, data, gitlab.WithoutRetries())
if err != nil {
return err // don't wrap
}
// Subtract the increments we've just sent
m.usageTracker.Subtract(usageData)
return nil
}
func (m *module) Name() string {
return usage_metrics.ModuleName
}
|