MCPcopy Index your code
hub / github.com/coder/coder / getOrCreateDesc

Method getOrCreateDesc

coderd/prometheusmetrics/aggregator.go:418–438  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
418func (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.

Callers 2

asPrometheusMethod · 0.95

Calls 1

cacheKeyForDescFunction · 0.85

Tested by 1