mirror of
https://github.com/safing/portbase
synced 2025-09-04 19:50:18 +00:00
Merge pull request #161 from safing/fix/namespace-and-labels-for-internal-metrics
Add namespace and labels to go internal metrics
This commit is contained in:
commit
59e88b15f7
1 changed files with 66 additions and 2 deletions
|
@ -1,12 +1,17 @@
|
||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
vm "github.com/VictoriaMetrics/metrics"
|
vm "github.com/VictoriaMetrics/metrics"
|
||||||
|
|
||||||
"github.com/safing/portbase/api"
|
"github.com/safing/portbase/api"
|
||||||
"github.com/safing/portbase/config"
|
"github.com/safing/portbase/config"
|
||||||
|
"github.com/safing/portbase/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func registerRuntimeMetric() error {
|
func registerRuntimeMetric() error {
|
||||||
|
@ -29,6 +34,65 @@ type runtimeMetrics struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *runtimeMetrics) WritePrometheus(w io.Writer) {
|
func (r *runtimeMetrics) WritePrometheus(w io.Writer) {
|
||||||
// TODO: Add global labels.
|
// If there nothing to change, just write directly to w.
|
||||||
vm.WriteProcessMetrics(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