VictoriaMetrics/lib/httpserver/metrics.go

73 lines
2.1 KiB
Go
Raw Normal View History

2019-05-22 23:16:55 +02:00
package httpserver
import (
"flag"
"fmt"
"io"
"regexp"
2019-05-22 23:16:55 +02:00
"strings"
"sync"
"sync/atomic"
2019-05-22 23:16:55 +02:00
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
2019-05-22 23:16:55 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
"github.com/VictoriaMetrics/metrics"
)
var versionRe = regexp.MustCompile(`v\d+\.\d+\.\d+`)
// WritePrometheusMetrics writes all the registered metrics to w in Prometheus exposition format.
func WritePrometheusMetrics(w io.Writer) {
currentTime := time.Now()
metricsCacheLock.Lock()
if currentTime.Sub(metricsCacheLastUpdateTime) > time.Second {
var bb bytesutil.ByteBuffer
writePrometheusMetrics(&bb)
metricsCache.Store(&bb)
metricsCacheLastUpdateTime = currentTime
}
metricsCacheLock.Unlock()
bb := metricsCache.Load().(*bytesutil.ByteBuffer)
2021-02-16 21:05:00 +01:00
_, _ = w.Write(bb.B)
}
var (
metricsCacheLock sync.Mutex
metricsCacheLastUpdateTime time.Time
metricsCache atomic.Value
)
func writePrometheusMetrics(w io.Writer) {
2019-05-22 23:16:55 +02:00
metrics.WritePrometheus(w, true)
metrics.WriteFDMetrics(w)
2019-05-22 23:16:55 +02:00
fmt.Fprintf(w, "vm_app_version{version=%q, short_version=%q} 1\n", buildinfo.Version,
versionRe.FindString(buildinfo.Version))
2019-05-22 23:16:55 +02:00
fmt.Fprintf(w, "vm_allowed_memory_bytes %d\n", memory.Allowed())
fmt.Fprintf(w, "vm_available_memory_bytes %d\n", memory.Allowed()+memory.Remaining())
fmt.Fprintf(w, "vm_available_cpu_cores %d\n", cgroup.AvailableCPUs())
fmt.Fprintf(w, "vm_gogc %d\n", cgroup.GetGOGC())
2019-05-22 23:16:55 +02:00
// Export start time and uptime in seconds
fmt.Fprintf(w, "vm_app_start_timestamp %d\n", startTime.Unix())
fmt.Fprintf(w, "vm_app_uptime_seconds %d\n", int(time.Since(startTime).Seconds()))
// Export flags as metrics.
flag.VisitAll(func(f *flag.Flag) {
lname := strings.ToLower(f.Name)
value := f.Value.String()
if flagutil.IsSecretFlag(lname) {
2019-05-22 23:16:55 +02:00
// Do not expose passwords and keys to prometheus.
value = "secret"
}
fmt.Fprintf(w, "flag{name=%q, value=%q} 1\n", f.Name, value)
})
}
var startTime = time.Now()