2023-04-20 12:40:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-04-18 15:03:05 +00:00
|
|
|
package reporting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2023-04-29 00:07:03 +00:00
|
|
|
"time"
|
2023-04-18 15:03:05 +00:00
|
|
|
|
2023-04-29 00:07:03 +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"
|
2023-04-29 00:07:03 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2023-04-18 15:03:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReportingManager struct {
|
2023-04-29 00:07:03 +00:00
|
|
|
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"
|
2023-04-29 00:07:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-29 00:07:03 +00:00
|
|
|
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{
|
2023-04-29 00:07:03 +00:00
|
|
|
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
|
|
|
|
}
|