Co-authored-by: Lucas Francisco López <lucaslopezf@gmail.com> Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
co-authored by
Lucas Francisco López
Julien Robert
parent
f76c659fd2
commit
974a24c58c
@@ -14,6 +14,15 @@ import (
|
||||
"github.com/prometheus/common/expfmt"
|
||||
)
|
||||
|
||||
// globalTelemetryEnabled is a private variable that stores the telemetry enabled state.
|
||||
// It is set on initialization and does not change for the lifetime of the program.
|
||||
var globalTelemetryEnabled bool
|
||||
|
||||
// IsTelemetryEnabled provides controlled access to check if telemetry is enabled.
|
||||
func IsTelemetryEnabled() bool {
|
||||
return globalTelemetryEnabled
|
||||
}
|
||||
|
||||
// globalLabels defines the set of global labels that will be applied to all
|
||||
// metrics emitted using the telemetry package function wrappers.
|
||||
var globalLabels = []metrics.Label{}
|
||||
@@ -95,6 +104,7 @@ type GatherResponse struct {
|
||||
|
||||
// New creates a new instance of Metrics
|
||||
func New(cfg Config) (_ *Metrics, rerr error) {
|
||||
globalTelemetryEnabled = cfg.Enabled
|
||||
if !cfg.Enabled {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -24,6 +24,10 @@ func NewLabel(name, value string) metrics.Label {
|
||||
// metric for a module with a given set of keys. If any global labels are defined,
|
||||
// they will be added to the module label.
|
||||
func ModuleMeasureSince(module string, start time.Time, keys ...string) {
|
||||
if !IsTelemetryEnabled() {
|
||||
return
|
||||
}
|
||||
|
||||
metrics.MeasureSinceWithLabels(
|
||||
keys,
|
||||
start.UTC(),
|
||||
@@ -35,6 +39,10 @@ func ModuleMeasureSince(module string, start time.Time, keys ...string) {
|
||||
// module with a given set of keys. If any global labels are defined, they will
|
||||
// be added to the module label.
|
||||
func ModuleSetGauge(module string, val float32, keys ...string) {
|
||||
if !IsTelemetryEnabled() {
|
||||
return
|
||||
}
|
||||
|
||||
metrics.SetGaugeWithLabels(
|
||||
keys,
|
||||
val,
|
||||
@@ -45,29 +53,58 @@ func ModuleSetGauge(module string, val float32, keys ...string) {
|
||||
// IncrCounter provides a wrapper functionality for emitting a counter metric with
|
||||
// global labels (if any).
|
||||
func IncrCounter(val float32, keys ...string) {
|
||||
if !IsTelemetryEnabled() {
|
||||
return
|
||||
}
|
||||
|
||||
metrics.IncrCounterWithLabels(keys, val, globalLabels)
|
||||
}
|
||||
|
||||
// IncrCounterWithLabels provides a wrapper functionality for emitting a counter
|
||||
// metric with global labels (if any) along with the provided labels.
|
||||
func IncrCounterWithLabels(keys []string, val float32, labels []metrics.Label) {
|
||||
if !IsTelemetryEnabled() {
|
||||
return
|
||||
}
|
||||
|
||||
metrics.IncrCounterWithLabels(keys, val, append(labels, globalLabels...))
|
||||
}
|
||||
|
||||
// SetGauge provides a wrapper functionality for emitting a gauge metric with
|
||||
// global labels (if any).
|
||||
func SetGauge(val float32, keys ...string) {
|
||||
if !IsTelemetryEnabled() {
|
||||
return
|
||||
}
|
||||
|
||||
metrics.SetGaugeWithLabels(keys, val, globalLabels)
|
||||
}
|
||||
|
||||
// SetGaugeWithLabels provides a wrapper functionality for emitting a gauge
|
||||
// metric with global labels (if any) along with the provided labels.
|
||||
func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) {
|
||||
if !IsTelemetryEnabled() {
|
||||
return
|
||||
}
|
||||
|
||||
metrics.SetGaugeWithLabels(keys, val, append(labels, globalLabels...))
|
||||
}
|
||||
|
||||
// MeasureSince provides a wrapper functionality for emitting a a time measure
|
||||
// metric with global labels (if any).
|
||||
func MeasureSince(start time.Time, keys ...string) {
|
||||
if !IsTelemetryEnabled() {
|
||||
return
|
||||
}
|
||||
|
||||
metrics.MeasureSinceWithLabels(keys, start.UTC(), globalLabels)
|
||||
}
|
||||
|
||||
// Now return the current time if telemetry is enabled or a zero time if it's not
|
||||
func Now() time.Time {
|
||||
if !IsTelemetryEnabled() {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var mu sync.Mutex
|
||||
|
||||
func initTelemetry(v bool) {
|
||||
globalTelemetryEnabled = v
|
||||
}
|
||||
|
||||
// Reset the global state to a known disabled state before each test.
|
||||
func setupTest(t *testing.T) {
|
||||
t.Helper()
|
||||
mu.Lock() // Ensure no other test can modify global state at the same time.
|
||||
defer mu.Unlock()
|
||||
initTelemetry(false)
|
||||
}
|
||||
|
||||
// TestNow tests the Now function when telemetry is enabled and disabled.
|
||||
func TestNow(t *testing.T) {
|
||||
setupTest(t) // Locks the mutex to avoid race condition.
|
||||
|
||||
initTelemetry(true)
|
||||
telemetryTime := Now()
|
||||
assert.NotEqual(t, time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled")
|
||||
|
||||
setupTest(t) // Reset the global state and lock the mutex again.
|
||||
|
||||
initTelemetry(false)
|
||||
telemetryTime = Now()
|
||||
assert.Equal(t, time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled")
|
||||
}
|
||||
|
||||
// TestIsTelemetryEnabled tests the IsTelemetryEnabled function.
|
||||
func TestIsTelemetryEnabled(t *testing.T) {
|
||||
setupTest(t) // Locks the mutex to avoid race condition.
|
||||
|
||||
initTelemetry(true)
|
||||
assert.True(t, IsTelemetryEnabled(), "IsTelemetryEnabled() should return true when globalTelemetryEnabled is set to true")
|
||||
|
||||
setupTest(t) // Reset the global state and lock the mutex again.
|
||||
|
||||
initTelemetry(false)
|
||||
assert.False(t, IsTelemetryEnabled(), "IsTelemetryEnabled() should return false when globalTelemetryEnabled is set to false")
|
||||
}
|
||||
Reference in New Issue
Block a user