getOrCreateDec checks if we already have a metric description in the aggregators cache for a given combination of base labels and extra labels. If we do not, we create a new description and cache it.
(name string, help string, baseLabelNames []string, extraLabels []*agentproto.Stats_Metric_Label)
| 416 | // getOrCreateDec checks if we already have a metric description in the aggregators cache for a given combination of base |
| 417 | // labels and extra labels. If we do not, we create a new description and cache it. |
| 418 | func (ma *MetricsAggregator) getOrCreateDesc(name string, help string, baseLabelNames []string, extraLabels []*agentproto.Stats_Metric_Label) *prometheus.Desc { |
| 419 | if ma.descCache == nil { |
| 420 | ma.descCache = make(map[string]descCacheEntry) |
| 421 | } |
| 422 | key := cacheKeyForDesc(name, baseLabelNames, extraLabels) |
| 423 | if d, ok := ma.descCache[key]; ok { |
| 424 | d.lastUsed = ma.clock.Now() |
| 425 | ma.descCache[key] = d |
| 426 | return d.desc |
| 427 | } |
| 428 | nBase := len(baseLabelNames) |
| 429 | nExtra := len(extraLabels) |
| 430 | labels := make([]string, nBase+nExtra) |
| 431 | copy(labels, baseLabelNames) |
| 432 | for i, l := range extraLabels { |
| 433 | labels[nBase+i] = l.Name |
| 434 | } |
| 435 | d := prometheus.NewDesc(name, help, labels, nil) |
| 436 | ma.descCache[key] = descCacheEntry{d, ma.clock.Now()} |
| 437 | return d |
| 438 | } |
| 439 | |
| 440 | // asPrometheus returns the annotatedMetric as a prometheus.Metric, it preallocates/fills by index, uses the aggregators |
| 441 | // metric description cache, and a small stack buffer for values in order to reduce memory allocations. |