MCPcopy
hub / github.com/microsoft/retina / newBaseMetricsObject

Function newBaseMetricsObject

pkg/module/metrics/basemetricsobject.go:137–186  ·  view source on GitHub ↗
(ctxOptions *api.MetricsContextOptions, fl *log.ZapLogger, isLocalContext enrichmentContext, expire expireFn, ttl time.Duration)

Source from the content-addressed store, hash-verified

135}
136
137func newBaseMetricsObject(ctxOptions *api.MetricsContextOptions, fl *log.ZapLogger, isLocalContext enrichmentContext, expire expireFn, ttl time.Duration) *baseMetricObject {
138 expireOrInfiniteTTL := expire
139 if ttl <= 0 {
140 // infinite TTL, so make sure the expiration function is unset
141 expireOrInfiniteTTL = nil
142 }
143
144 b := baseMetricObject{
145 advEnable: ctxOptions.IsAdvanced(),
146 ctxOptions: ctxOptions,
147 l: fl,
148 contextMode: isLocalContext,
149 expireFn: expireOrInfiniteTTL,
150 }
151
152 if expireOrInfiniteTTL != nil {
153 // only initialize these if we have a valid expiration function to save some memory
154 b.RWMutex = &sync.RWMutex{}
155 b.lastUpdated = make(map[string]updated)
156 ctx, cancel := context.WithCancel(context.Background())
157 b.ctx = ctx
158 b.cancelFn = cancel
159 b.l.Info(
160 "Starting metric expiration routine: "+ctxOptions.MetricName,
161 zap.Duration("ttl", ttl),
162 )
163 go func() {
164 ticker := time.NewTicker(ttl)
165 defer ticker.Stop()
166 for {
167 select {
168 case <-ctx.Done():
169 b.l.Info("Stopping metric expiration routine: " + b.ctxOptions.MetricName)
170 return
171 case t := <-ticker.C:
172 b.l.Debug("Expiring metrics: " + b.ctxOptions.MetricName)
173 n := b.expire(ttl)
174 b.l.Debug(
175 "Metric expiration finished: "+b.ctxOptions.MetricName,
176 zap.Time("next_expiration", t.Add(ttl)),
177 zap.Int("expired", n),
178 )
179 }
180 }
181 }()
182 }
183
184 b.populateCtxOptions(ctxOptions)
185 return &b
186}
187
188func (b *baseMetricObject) populateCtxOptions(ctxOptions *api.MetricsContextOptions) {
189 if b.isLocalContext() {

Callers 6

NewDNSMetricsFunction · 0.85
NewForwardCountMetricsFunction · 0.85
TestBaseMetricObjectFunction · 0.85
NewDropCountMetricsFunction · 0.85
NewTCPMetricsFunction · 0.85
NewTCPRetransMetricsFunction · 0.85

Calls 6

expireMethod · 0.95
populateCtxOptionsMethod · 0.95
IsAdvancedMethod · 0.80
InfoMethod · 0.80
StopMethod · 0.65
AddMethod · 0.65

Tested by 1

TestBaseMetricObjectFunction · 0.68