| 135 | } |
| 136 | |
| 137 | func 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 | |
| 188 | func (b *baseMetricObject) populateCtxOptions(ctxOptions *api.MetricsContextOptions) { |
| 189 | if b.isLocalContext() { |