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)
| 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. |
| 442 | func (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 | |
| 477 | var defaultAgentMetricsLabels = []string{agentmetrics.LabelUsername, agentmetrics.LabelWorkspaceName, agentmetrics.LabelAgentName, agentmetrics.LabelTemplateName} |
| 478 |