*: cleanup collector API 1 (#1547)

This commit is contained in:
Jan-Otto Kröpke
2024-08-05 15:50:41 +02:00
committed by GitHub
parent dffc53eff8
commit d1e3a63f93
86 changed files with 2162 additions and 1930 deletions

View File

@@ -16,11 +16,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
const (
Name = "smb"
FlagSmbListAllCollectors = "collectors.smb.list"
FlagSmbCollectorsEnabled = "collectors.smb.enabled"
)
const Name = "smb"
type Config struct {
CollectorsEnabled string `yaml:"collectors_enabled"`
@@ -30,66 +26,72 @@ var ConfigDefaults = Config{
CollectorsEnabled: "",
}
type collector struct {
type Collector struct {
logger log.Logger
smbListAllCollectors *bool
smbCollectorsEnabled *string
TreeConnectCount *prometheus.Desc
CurrentOpenFileCount *prometheus.Desc
treeConnectCount *prometheus.Desc
currentOpenFileCount *prometheus.Desc
enabledCollectors []string
}
// All available collector functions
// All available Collector functions
var smbAllCollectorNames = []string{
"ServerShares",
}
func New(logger log.Logger, config *Config) types.Collector {
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}
smbListAllCollectors := false
c := &collector{
c := &Collector{
smbCollectorsEnabled: &config.CollectorsEnabled,
smbListAllCollectors: &smbListAllCollectors,
}
c.SetLogger(logger)
return c
}
func NewWithFlags(app *kingpin.Application) types.Collector {
return &collector{
func NewWithFlags(app *kingpin.Application) *Collector {
return &Collector{
smbListAllCollectors: app.Flag(
FlagSmbListAllCollectors,
"collectors.smb.list",
"List the collectors along with their perflib object name/ids",
).Bool(),
smbCollectorsEnabled: app.Flag(
FlagSmbCollectorsEnabled,
"collectors.smb.enabled",
"Comma-separated list of collectors to use. Defaults to all, if not specified.",
).Default(ConfigDefaults.CollectorsEnabled).String(),
}
}
func (c *collector) GetName() string {
func (c *Collector) GetName() string {
return Name
}
func (c *collector) SetLogger(logger log.Logger) {
func (c *Collector) SetLogger(logger log.Logger) {
c.logger = log.With(logger, "collector", Name)
}
func (c *collector) GetPerfCounter() ([]string, error) {
func (c *Collector) GetPerfCounter() ([]string, error) {
return []string{
"SMB Server Shares",
}, nil
}
func (c *collector) Build() error {
func (c *Collector) Close() error {
return nil
}
func (c *Collector) Build() error {
// desc creates a new prometheus description
desc := func(metricName string, description string, labels ...string) *prometheus.Desc {
return prometheus.NewDesc(
@@ -100,8 +102,8 @@ func (c *collector) Build() error {
)
}
c.CurrentOpenFileCount = desc("server_shares_current_open_file_count", "Current total count open files on the SMB Server")
c.TreeConnectCount = desc("server_shares_tree_connect_count", "Count of user connections to the SMB Server")
c.currentOpenFileCount = desc("server_shares_current_open_file_count", "Current total count open files on the SMB Server")
c.treeConnectCount = desc("server_shares_tree_connect_count", "Count of user connections to the SMB Server")
c.enabledCollectors = make([]string, 0, len(smbAllCollectorNames))
@@ -110,10 +112,11 @@ func (c *collector) Build() error {
}
if *c.smbListAllCollectors {
fmt.Printf("%-32s %-32s\n", "Collector Name", "Perflib Object")
fmt.Printf("%-32s %-32s\n", "Collector Name", "Perflib Object") //nolint:forbidigo
for _, cname := range smbAllCollectorNames {
fmt.Printf("%-32s %-32s\n", cname, collectorDesc[cname])
fmt.Printf("%-32s %-32s\n", cname, collectorDesc[cname]) //nolint:forbidigo
}
os.Exit(0)
}
@@ -135,7 +138,7 @@ func (c *collector) Build() error {
}
// Collect collects smb metrics and sends them to prometheus
func (c *collector) Collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
func (c *Collector) Collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
collectorFuncs := map[string]func(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error{
"ServerShares": c.collectServerShares,
}
@@ -157,7 +160,7 @@ type perflibServerShares struct {
TreeConnectCount float64 `perflib:"Tree Connect Count"`
}
func (c *collector) collectServerShares(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
func (c *Collector) collectServerShares(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
var data []perflibServerShares
if err := perflib.UnmarshalObject(ctx.PerfObjects["SMB Server Shares"], &data, c.logger); err != nil {
return err
@@ -169,23 +172,22 @@ func (c *collector) collectServerShares(ctx *types.ScrapeContext, ch chan<- prom
}
ch <- prometheus.MustNewConstMetric(
c.CurrentOpenFileCount,
c.currentOpenFileCount,
prometheus.CounterValue,
instance.CurrentOpenFileCount,
)
ch <- prometheus.MustNewConstMetric(
c.TreeConnectCount,
c.treeConnectCount,
prometheus.CounterValue,
instance.TreeConnectCount,
)
}
return nil
}
// toLabelName converts strings to lowercase and replaces all whitespaces and dots with underscores
func (c *collector) toLabelName(name string) string {
func (c *Collector) toLabelName(name string) string {
s := strings.ReplaceAll(strings.Join(strings.Fields(strings.ToLower(name)), "_"), ".", "_")
s = strings.ReplaceAll(s, "__", "_")
return s