Skip to content

Commit

Permalink
ref!: refactor out info metrics into main/mango, inventory, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhop committed Oct 31, 2023
1 parent 465f7be commit 03a035a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
36 changes: 23 additions & 13 deletions cmd/mango/mango.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ const (
)

var (
metricMangoRuntimeInfoLabels = prometheus.Labels{
"auto_reload": "disabled",
"log_level": "info",
}

metricMangoRuntimeInfo = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "mango_runtime_info",
Help: "A metric with a constant '1' value with labels for inventory auto-reload status, logging level, etc.",
},
[]string{"auto_reload", "log_level"},
)

metricServiceStartSeconds = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "mango_service_start_seconds",
Help: "Unix timestamp of when mango started",
},
)

// TODO: @tjhop move `enrolled` to an inventory pkg metric
// TODO: @tjhop move `manager` to a manager pkg metric
// TODO: @tjhop add labels for: [auto_reload: true|false]
// metricMangoRuntimeInfo = promauto.NewGaugeVec(
// prometheus.GaugeOpts{
// Name: "mango_runtime_info",
// Help: "A metric with a constant '1' value with labels for information about the mango runtime, such as system hostname.",
// },
// []string{"hostname", "enrolled", "manager"},
// )
)

func init() {
Expand All @@ -73,6 +75,7 @@ func init() {
func mango(ctx context.Context, logger *slog.Logger, inventoryPath, hostname string) {
mangoStart := time.Now()
metricServiceStartSeconds.Set(float64(mangoStart.Unix()))
metricMangoRuntimeInfo.With(metricMangoRuntimeInfoLabels).Set(1)

ctx, cancel := context.WithCancel(ctx)
defer func() {
Expand Down Expand Up @@ -164,7 +167,6 @@ func mango(ctx context.Context, logger *slog.Logger, inventoryPath, hostname str
// // TODO(@tjhop): consider reworking this runtime var to be inventory
// // runtime info and move it to inventory pkg?
// metricMangoRuntimeInfo.With(prometheus.Labels{
// // TODO(@tjhop): include system/given hostname, similar to log on l384
// "hostname": hostname,
// "enrolled": strconv.FormatBool(enrolled),
// }).Set(1)
Expand Down Expand Up @@ -324,6 +326,10 @@ func mango(ctx context.Context, logger *slog.Logger, inventoryPath, hostname str
slog.String("interval", dur.String()),
)

// update runtime info metric
metricMangoRuntimeInfoLabels["auto_reload"] = dur.String()
metricMangoRuntimeInfo.With(metricMangoRuntimeInfoLabels).Set(1)

ticker := time.NewTicker(dur)

for {
Expand Down Expand Up @@ -478,11 +484,15 @@ func main() {
slog.LevelError,
"Failed to parse log level from flag",
slog.String("err", "Unsupported log level"),
slog.String("level", logLevelFlagVal),
slog.String("log_level", logLevelFlagVal),
)
os.Exit(1)
}

// update runtime info metric
metricMangoRuntimeInfoLabels["log_level"] = strings.ToLower(logLevel.Level().String())
metricMangoRuntimeInfo.With(metricMangoRuntimeInfoLabels).Set(1)

// parse log output format from flag
logOutputFormat := strings.TrimSpace(strings.ToLower(viper.GetString("logging.output")))
if logOutputFormat == "json" {
Expand Down
24 changes: 24 additions & 0 deletions internal/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log/slog"
"path/filepath"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -13,6 +14,21 @@ var (
commonMetricLabels = []string{"inventory", "component"}

// prometheus metrics
metricMangoInventoryInfoLabels = prometheus.Labels{
"hostname": "unknown",
"enrolled": "false",
"inventory_path": "unknown",
}

metricMangoInventoryInfo = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "mango_inventory_info",
Help: "A metric with a constant '1' value with labels for information about the mango inventory",
},
[]string{"hostname", "enrolled", "inventory_path"},
)

// TODO(@tjhop): move to `_suffix` name?
metricInventory = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "mango_inventory",
Expand All @@ -21,6 +37,7 @@ var (
commonMetricLabels,
)

// TODO(@tjhop): move to `_suffix` name?
metricInventoryApplicable = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "mango_inventory_applicable",
Expand Down Expand Up @@ -132,6 +149,9 @@ func NewInventory(path, name string) *Inventory {
roles: []Role{},
directives: []Directive{},
}
metricMangoInventoryInfoLabels["hostname"] = name
metricMangoInventoryInfoLabels["inventory_path"] = path
metricMangoInventoryInfo.With(metricMangoInventoryInfoLabels).Set(1)

return &i
}
Expand Down Expand Up @@ -193,6 +213,10 @@ func (i *Inventory) Reload(ctx context.Context, logger *slog.Logger) {
slog.String("err", err.Error()),
)
}

// update inventory metrics
metricMangoInventoryInfoLabels["enrolled"] = strconv.FormatBool(i.IsEnrolled())
metricMangoInventoryInfo.With(metricMangoInventoryInfoLabels).Set(1)
}

// IsEnrolled returns if the hostname of the system is defined in the
Expand Down

0 comments on commit 03a035a

Please sign in to comment.