open-consul/agent/consul/reporting/reporting.go

56 lines
1.4 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2023-04-18 15:03:05 +00:00
package reporting
import (
"sync"
"time"
2023-04-18 15:03:05 +00:00
"github.com/hashicorp/consul/agent/consul/state"
"github.com/hashicorp/consul/agent/structs"
2023-04-18 15:03:05 +00:00
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-memdb"
2023-04-18 15:03:05 +00:00
)
type ReportingManager struct {
logger hclog.Logger
server ServerDelegate
stateProvider StateDelegate
tickerInterval time.Duration
2023-04-18 15:03:05 +00:00
EntDeps
sync.RWMutex
}
const (
SystemMetadataReportingProcessID = "reporting-process-id"
ReportingInterval = 1 * time.Hour
2023-04-18 15:03:05 +00:00
)
//go:generate mockery --name ServerDelegate --inpackage
type ServerDelegate interface {
GetSystemMetadata(key string) (string, error)
SetSystemMetadataKey(key, val string) error
IsLeader() bool
}
type StateDelegate interface {
NodeUsage() (uint64, state.NodeUsage, error)
ServiceUsage(ws memdb.WatchSet) (uint64, structs.ServiceUsage, error)
}
func NewReportingManager(logger hclog.Logger, deps EntDeps, server ServerDelegate, stateProvider StateDelegate) *ReportingManager {
2023-04-18 15:03:05 +00:00
rm := &ReportingManager{
logger: logger.Named("reporting"),
server: server,
stateProvider: stateProvider,
tickerInterval: ReportingInterval,
2023-04-18 15:03:05 +00:00
}
err := rm.initEnterpriseReporting(deps)
if err != nil {
rm.logger.Error("Error initializing reporting manager", "error", err)
return nil
}
return rm
}