mirror of
https://github.com/safing/portbase
synced 2025-04-23 18:59:08 +00:00
Add namespace and labels to go internal metrics
This commit is contained in:
parent
2b73adfc22
commit
11b71d54e9
1 changed files with 66 additions and 2 deletions
|
@ -1,12 +1,17 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
vm "github.com/VictoriaMetrics/metrics"
|
||||
|
||||
"github.com/safing/portbase/api"
|
||||
"github.com/safing/portbase/config"
|
||||
"github.com/safing/portbase/log"
|
||||
)
|
||||
|
||||
func registerRuntimeMetric() error {
|
||||
|
@ -29,6 +34,65 @@ type runtimeMetrics struct {
|
|||
}
|
||||
|
||||
func (r *runtimeMetrics) WritePrometheus(w io.Writer) {
|
||||
// TODO: Add global labels.
|
||||
vm.WriteProcessMetrics(w)
|
||||
// If there nothing to change, just write directly to w.
|
||||
if metricNamespace == "" && len(globalLabels) == 0 {
|
||||
vm.WriteProcessMetrics(w)
|
||||
return
|
||||
}
|
||||
|
||||
// Write metrics to buffer.
|
||||
buf := new(bytes.Buffer)
|
||||
vm.WriteProcessMetrics(buf)
|
||||
|
||||
// Add namespace and label per line.
|
||||
scanner := bufio.NewScanner(buf)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
// Add namespace, if set.
|
||||
if metricNamespace != "" {
|
||||
line = metricNamespace + "_" + line
|
||||
}
|
||||
|
||||
// Add global labels, if set.
|
||||
if len(globalLabels) > 0 {
|
||||
// Find where to insert.
|
||||
mergeWithExisting := true
|
||||
insertAt := strings.Index(line, "{") + 1
|
||||
if insertAt <= 0 {
|
||||
mergeWithExisting = false
|
||||
insertAt = strings.Index(line, " ")
|
||||
if insertAt < 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Write new line directly to w.
|
||||
fmt.Fprint(w, line[:insertAt])
|
||||
if !mergeWithExisting {
|
||||
fmt.Fprint(w, "{")
|
||||
}
|
||||
labelsAdded := 0
|
||||
for labelKey, labelValue := range globalLabels {
|
||||
fmt.Fprintf(w, "%s=%q", labelKey, labelValue)
|
||||
// Add separator if not last label.
|
||||
labelsAdded++
|
||||
if labelsAdded < len(globalLabels) {
|
||||
fmt.Fprint(w, ", ")
|
||||
}
|
||||
}
|
||||
if mergeWithExisting {
|
||||
fmt.Fprint(w, ", ")
|
||||
} else {
|
||||
fmt.Fprint(w, "}")
|
||||
}
|
||||
fmt.Fprintln(w, line[insertAt:])
|
||||
}
|
||||
}
|
||||
|
||||
// Check if there was an error in the scanner.
|
||||
if scanner.Err() != nil {
|
||||
log.Warningf("metrics: failed to scan go process metrics: %s", scanner.Err())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue