@@ -2,7 +2,9 @@ package metrics
22
33import (
44 "context"
5+ "os"
56 "runtime"
7+ "strings"
68 "sync"
79 "time"
810
@@ -38,23 +40,24 @@ var (
3840)
3941
4042type StatterConfig struct {
41- Addr string // localhost:8125
42- Prefix string // metrics prefix
43- Agent string // telegraf/datadog
44- EnvName string // dev/test/staging/prod
45- HostName string // hostname
46- Version string // version
47- DefaultTags []interface {} // default tags for all metrics
48- StuckFunctionTimeout time.Duration // stuck time
49- MockingThreshold time.Duration // mocking threshold
50- MockingEnabled bool // whether to enable mock statter, which only produce logs
51- Disabled bool // whether to disable metrics completely
52- TracingEnabled bool // whether tracing should be enabled
53- ProfilingEnabled bool // whether Datadog profiling should be enabled
54- MixPanelEnabled bool // whether MixPanel should be enabled
55- MixPanelProjectToken string // MixPanel project token
56- OTELInsecure bool // disable TLS (use for self-hosted SigNoz without TLS)
57- OTELHeaders map [string ]string // extra headers, e.g. {"signoz-access-token": "<token>"} for SigNoz Cloud
43+ Addr string // localhost:8125
44+ Prefix string // metrics prefix
45+ Agent string // telegraf/datadog
46+ EnvName string // dev/test/staging/prod
47+ HostName string // hostname
48+ Version string // version
49+ DefaultTags []interface {} // default tags for all metrics
50+ StuckFunctionTimeout time.Duration // stuck time
51+ MockingThreshold time.Duration // mocking threshold
52+ MockingEnabled bool // whether to enable mock statter, which only produce logs
53+ Disabled bool // whether to disable metrics completely
54+ TracingEnabled bool // whether tracing should be enabled
55+ ProfilingEnabled bool // whether Datadog profiling should be enabled
56+ MixPanelEnabled bool // whether MixPanel should be enabled
57+ MixPanelProjectToken string // MixPanel project token
58+ OTELInsecure bool // disable TLS (use for self-hosted SigNoz without TLS)
59+ OTELHeaders map [string ]string // extra headers, e.g. {"signoz-access-token": "<token>"} for SigNoz Cloud
60+ OTELUseCounterForCount bool // use monotonic OTel counters for Count/Incr instead of UpDownCounters
5861}
5962
6063func (m * StatterConfig ) BaseTags () []string {
@@ -177,6 +180,7 @@ func Init(addr string, prefix string, cfg *StatterConfig) error {
177180 cfg .OTELInsecure ,
178181 cfg .OTELHeaders ,
179182 config .BaseTags (),
183+ cfg .OTELUseCounterForCount ,
180184 )
181185
182186 default :
@@ -229,6 +233,72 @@ func Init(addr string, prefix string, cfg *StatterConfig) error {
229233 return nil
230234}
231235
236+ type ServiceConfig struct {
237+ Disabled bool
238+ ServiceName string
239+ AgentID string
240+ AgentAddress string
241+ MetricsPrefix string
242+ EnvName string
243+ MockingEnabled bool
244+ MockingThreshold time.Duration
245+ MixPanelEnabled bool
246+ MixPanelProjectToken string
247+ OTelInsecure bool
248+ OTelUseCounters bool
249+ TracingEnabled bool
250+ RetryInitialInterval time.Duration
251+ }
252+
253+ func (c ServiceConfig ) normalizePrefix () string {
254+ return strings .TrimRight (c .MetricsPrefix , "." ) + "."
255+ }
256+
257+ func InitService (ctx context.Context , cfg ServiceConfig ) (func (timeout time.Duration ), error ) {
258+ closeFn := func (time.Duration ) {}
259+ if err := cfg .Validate (); err != nil {
260+ return closeFn , err
261+ }
262+ if cfg .Disabled {
263+ return closeFn , nil
264+ }
265+
266+ if cfg .RetryInitialInterval <= 0 {
267+ cfg .RetryInitialInterval = 10 * time .Second
268+ }
269+
270+ for {
271+ hostname , _ := os .Hostname ()
272+ err := Init (cfg .AgentAddress , cfg .normalizePrefix (), & StatterConfig {
273+ Agent : cfg .AgentID ,
274+ EnvName : cfg .EnvName ,
275+ HostName : hostname ,
276+ MockingEnabled : cfg .MockingEnabled ,
277+ MockingThreshold : cfg .MockingThreshold ,
278+ MixPanelEnabled : cfg .MixPanelEnabled ,
279+ MixPanelProjectToken : cfg .MixPanelProjectToken ,
280+ OTELInsecure : cfg .OTelInsecure ,
281+ OTELUseCounterForCount : cfg .OTelUseCounters ,
282+ TracingEnabled : cfg .TracingEnabled ,
283+ DefaultTags : []interface {}{"service.name" , cfg .ServiceName },
284+ })
285+ if err != nil {
286+ log .WithError (err ).Warningf ("metrics init failed, will retry in %s seconds" , cfg .RetryInitialInterval )
287+ select {
288+ case <- ctx .Done ():
289+ return closeFn , nil
290+ case <- time .After (cfg .RetryInitialInterval ):
291+ }
292+ continue
293+ }
294+ log .Debugf ("metrics %s client initialized at %s with prefix %s (mocking %v)" ,
295+ cfg .AgentID , cfg .AgentAddress , cfg .normalizePrefix (), cfg .MockingEnabled )
296+ break
297+ }
298+
299+ return CloseWithTimeout , nil
300+ }
301+
232302func StartMixPanel (projectToken string ) {
233303 clientMux .Lock ()
234304 defer clientMux .Unlock ()
0 commit comments