mirror of
https://github.com/kemko/nomad.git
synced 2026-01-05 18:05:42 +03:00
* Upgrade to using hashicorp/go-metrics@v0.5.4 This also requires bumping the dependencies for: * memberlist * serf * raft * raft-boltdb * (and indirectly hashicorp/mdns due to the memberlist or serf update) Unlike some other HashiCorp products, Nomads root module is currently expected to be consumed by others. This means that it needs to be treated more like our libraries and upgrade to hashicorp/go-metrics by utilizing its compat packages. This allows those importing the root module to control the metrics module used via build tags.
54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package hookstats
|
|
|
|
import (
|
|
"time"
|
|
|
|
metrics "github.com/hashicorp/go-metrics/compat"
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
|
)
|
|
|
|
// Handler implements interfaces.HookStatsHandler and is used when the operator
|
|
// has not disabled hook metrics.
|
|
type Handler struct {
|
|
baseLabels []metrics.Label
|
|
runnerType string
|
|
}
|
|
|
|
// NewHandler creates a new hook stats handler to be used for emitting hook
|
|
// stats for operator alerting and performance identification. The base labels
|
|
// should be passed from the client set of labels and the runner type indicates
|
|
// if the hooks are run from the alloc or task runner.
|
|
func NewHandler(base []metrics.Label, runnerType string) interfaces.HookStatsHandler {
|
|
return &Handler{
|
|
baseLabels: base,
|
|
runnerType: runnerType,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) Emit(start time.Time, hookName, hookType string, err error) {
|
|
|
|
// Add the hook name to the base labels array, so we have a complete set to
|
|
// add to the metrics. Operators do not want this as part of the metric
|
|
// name due to cardinality control.
|
|
labels := h.baseLabels
|
|
labels = append(labels, metrics.Label{Name: "hook_name", Value: hookName})
|
|
|
|
metrics.MeasureSinceWithLabels([]string{"client", h.runnerType, hookType, "elapsed"}, start, labels)
|
|
if err != nil {
|
|
metrics.IncrCounterWithLabels([]string{"client", h.runnerType, hookType, "failed"}, 1, labels)
|
|
} else {
|
|
metrics.IncrCounterWithLabels([]string{"client", h.runnerType, hookType, "success"}, 1, labels)
|
|
}
|
|
}
|
|
|
|
// NoOpHandler implements interfaces.HookStatsHandler and is used when the
|
|
// operator has disabled hook metrics.
|
|
type NoOpHandler struct{}
|
|
|
|
func NewNoOpHandler() interfaces.HookStatsHandler { return &NoOpHandler{} }
|
|
|
|
func (n *NoOpHandler) Emit(_ time.Time, _, _ string, _ error) {}
|