2019-05-22 21:16:55 +00:00
|
|
|
package httpserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-11-03 12:10:42 +00:00
|
|
|
"regexp"
|
2019-05-22 21:16:55 +00:00
|
|
|
"strings"
|
2021-02-16 12:54:34 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2019-05-22 21:16:55 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
|
2021-02-16 12:54:34 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2021-03-10 10:01:18 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2020-09-16 19:34:01 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2021-09-21 20:07:56 +00:00
|
|
|
var versionRe = regexp.MustCompile(`v\d+\.\d+\.\d+(?:-enterprise)?(?:-cluster)?`)
|
2020-11-03 12:10:42 +00:00
|
|
|
|
2020-08-07 07:56:50 +00:00
|
|
|
// WritePrometheusMetrics writes all the registered metrics to w in Prometheus exposition format.
|
|
|
|
func WritePrometheusMetrics(w io.Writer) {
|
2021-02-16 12:54:34 +00:00
|
|
|
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 20:05:00 +00:00
|
|
|
_, _ = w.Write(bb.B)
|
2021-02-16 12:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
metricsCacheLock sync.Mutex
|
|
|
|
metricsCacheLastUpdateTime time.Time
|
|
|
|
metricsCache atomic.Value
|
|
|
|
)
|
|
|
|
|
|
|
|
func writePrometheusMetrics(w io.Writer) {
|
2019-05-22 21:16:55 +00:00
|
|
|
metrics.WritePrometheus(w, true)
|
2021-02-04 14:33:07 +00:00
|
|
|
metrics.WriteFDMetrics(w)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2020-11-03 12:10:42 +00:00
|
|
|
fmt.Fprintf(w, "vm_app_version{version=%q, short_version=%q} 1\n", buildinfo.Version,
|
|
|
|
versionRe.FindString(buildinfo.Version))
|
2019-05-22 21:16:55 +00:00
|
|
|
fmt.Fprintf(w, "vm_allowed_memory_bytes %d\n", memory.Allowed())
|
2021-03-10 10:01:18 +00:00
|
|
|
fmt.Fprintf(w, "vm_available_memory_bytes %d\n", memory.Allowed()+memory.Remaining())
|
|
|
|
fmt.Fprintf(w, "vm_available_cpu_cores %d\n", cgroup.AvailableCPUs())
|
2021-07-05 09:31:26 +00:00
|
|
|
fmt.Fprintf(w, "vm_gogc %d\n", cgroup.GetGOGC())
|
2019-05-22 21:16:55 +00: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.
|
2021-07-13 12:07:59 +00:00
|
|
|
isSetMap := make(map[string]bool)
|
|
|
|
flag.Visit(func(f *flag.Flag) {
|
|
|
|
isSetMap[f.Name] = true
|
|
|
|
})
|
2019-05-22 21:16:55 +00:00
|
|
|
flag.VisitAll(func(f *flag.Flag) {
|
|
|
|
lname := strings.ToLower(f.Name)
|
|
|
|
value := f.Value.String()
|
2020-09-16 19:34:01 +00:00
|
|
|
if flagutil.IsSecretFlag(lname) {
|
2019-05-22 21:16:55 +00:00
|
|
|
// Do not expose passwords and keys to prometheus.
|
|
|
|
value = "secret"
|
|
|
|
}
|
2021-07-13 12:07:59 +00:00
|
|
|
isSet := "false"
|
|
|
|
if isSetMap[f.Name] {
|
|
|
|
isSet = "true"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "flag{name=%q, value=%q, is_set=%q} 1\n", f.Name, value, isSet)
|
2019-05-22 21:16:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var startTime = time.Now()
|