Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

feat(meterreporter): add jitter to meter collection cycles (#11451)

Karan Balanidd3e743b2e66
diff --git a/ee/meterreporter/httpmeterreporter/provider.go b/ee/meterreporter/httpmeterreporter/provider.go
index f77af59cd2..8e775f7634 100644
--- a/ee/meterreporter/httpmeterreporter/provider.go
+++ b/ee/meterreporter/httpmeterreporter/provider.go
@@ -94,17 +94,19 @@ func newProvider(
func (provider *Provider) Start(ctx context.Context) error {
close(provider.healthyC)
- provider.collect(ctx)
+ startDelay := provider.config.NewJitter()
- ticker := time.NewTicker(provider.config.Interval)
- defer ticker.Stop()
+ timer := time.NewTimer(startDelay)
+ defer timer.Stop()
for {
select {
case <-provider.stopC:
return nil
- case <-ticker.C:
+ case <-timer.C:
provider.collect(ctx)
+ next := provider.config.Interval - provider.config.NewJitter()
+ timer.Reset(next)
}
}
}
@@ -257,6 +259,7 @@ func (provider *Provider) report(ctx context.Context, orgID valuer.UUID, license
collectedReadings, err := collector.Collect(ctx, orgID, license, window)
if err != nil {
provider.metrics.collections.Add(ctx, 1, metric.WithAttributes(meterAttr, errors.TypeAttr(err)))
+ provider.settings.Logger().ErrorContext(ctx, "meter collector failed", errors.Attr(err), slog.String("org_id", orgID.StringValue()), slog.String("meter", collector.Name().String()))
continue
}
diff --git a/pkg/meterreporter/config.go b/pkg/meterreporter/config.go
index ad068cf9b4..795e150c08 100644
--- a/pkg/meterreporter/config.go
+++ b/pkg/meterreporter/config.go
@@ -1,6 +1,7 @@
package meterreporter
import (
+ "math/rand/v2"
"time"
"github.com/SigNoz/signoz/pkg/errors"
@@ -15,12 +16,21 @@ type Config struct {
// Backfill enables sealed-day catch-up from the license creation day.
Backfill bool `mapstructure:"backfill"`
+
+ // Jitter is the randomness applied to both the first collect after
+ // Start() and to every subsequent cycle. The first fire happens at a
+ // random time in [0, Jitter); each subsequent cycle takes
+ // Interval - random(0, Jitter). Negative (the default) means "derive
+ // from Interval" via ResolvedJitter, so the value scales with whatever
+ // Interval the user picks.
+ Jitter time.Duration `mapstructure:"jitter"`
}
func newConfig() factory.Config {
return Config{
Interval: 6 * time.Hour,
Backfill: true,
+ Jitter: -1, // Negative sentinel. Resolved at use time unless explicitly set.
}
}
@@ -29,9 +39,27 @@ func NewConfigFactory() factory.ConfigFactory {
}
func (c Config) Validate() error {
- if c.Interval < 5*time.Minute || c.Interval > 24*time.Hour {
- return errors.New(errors.TypeInvalidInput, ErrCodeInvalidInput, "meterreporter::interval must be between 5m and 24h")
+ if c.Interval < 10*time.Minute || c.Interval > 24*time.Hour {
+ return errors.New(errors.TypeInvalidInput, ErrCodeInvalidInput, "meterreporter::interval must be between 10m and 24h")
+ }
+
+ if c.Jitter >= 0 && (c.Jitter < 10*time.Minute || c.Jitter > c.Interval) {
+ return errors.New(errors.TypeInvalidInput, ErrCodeInvalidInput, "meterreporter::jitter must be between 10m and interval")
}
return nil
}
+
+// NewJitter returns a fresh random duration sampled uniformly from
+// [0, jitter), where jitter is the configured Jitter or, if the sentinel
+// default is still in place, min(Interval, 2h).
+func (c Config) NewJitter() time.Duration {
+ defaultJitter := 2 * time.Hour
+
+ cap := c.Jitter
+ if cap < 0 {
+ cap = min(c.Interval, defaultJitter)
+ }
+
+ return time.Duration(rand.Int64N(int64(cap)))
+}