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

Method asPrometheus

coderd/prometheusmetrics/aggregator.go:442–475  ·  view source on GitHub ↗

asPrometheus returns the annotatedMetric as a prometheus.Metric, it preallocates/fills by index, uses the aggregators metric description cache, and a small stack buffer for values in order to reduce memory allocations.

(am *annotatedMetric)

Source from the content-addressed store, hash-verified

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.
442func (ma *MetricsAggregator) asPrometheus(am *annotatedMetric) (prometheus.Metric, error) {
443 baseLabelNames := am.aggregateByLabels
444 extraLabels := am.Labels
445
446 nBase := len(baseLabelNames)
447 nExtra := len(extraLabels)
448 nTotal := nBase + nExtra
449
450 var scratch [16]string
451 var labelValues []string
452 if nTotal <= len(scratch) {
453 labelValues = scratch[:nTotal]
454 } else {
455 labelValues = make([]string, nTotal)
456 }
457
458 for i, label := range baseLabelNames {
459 val, err := am.getFieldByLabel(label)
460 if err != nil {
461 return nil, err
462 }
463 labelValues[i] = val
464 }
465 for i, l := range extraLabels {
466 labelValues[nBase+i] = l.Value
467 }
468
469 desc := ma.getOrCreateDesc(am.Name, metricHelpForAgent, baseLabelNames, extraLabels)
470 valueType, err := asPrometheusValueType(am.Type)
471 if err != nil {
472 return nil, err
473 }
474 return prometheus.MustNewConstMetric(desc, valueType, am.Value, labelValues...), nil
475}
476
477var defaultAgentMetricsLabels = []string{agentmetrics.LabelUsername, agentmetrics.LabelWorkspaceName, agentmetrics.LabelAgentName, agentmetrics.LabelTemplateName}
478

Callers 3

benchAsPrometheusFunction · 0.95
RunMethod · 0.95
TestDescCache_DescExpireFunction · 0.95

Calls 3

getOrCreateDescMethod · 0.95
asPrometheusValueTypeFunction · 0.85
getFieldByLabelMethod · 0.80

Tested by 2

benchAsPrometheusFunction · 0.76
TestDescCache_DescExpireFunction · 0.76