2019-05-22 23:16:55 +02:00
|
|
|
package httpserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-11-03 13:10:42 +01:00
|
|
|
"regexp"
|
2019-05-22 23:16:55 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
|
2020-09-16 21:34:01 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2020-11-03 13:10:42 +01:00
|
|
|
var versionRe = regexp.MustCompile(`v\d+\.\d+\.\d+`)
|
|
|
|
|
2020-08-07 09:56:50 +02:00
|
|
|
// WritePrometheusMetrics writes all the registered metrics to w in Prometheus exposition format.
|
|
|
|
func WritePrometheusMetrics(w io.Writer) {
|
2019-05-22 23:16:55 +02:00
|
|
|
metrics.WritePrometheus(w, true)
|
2021-02-04 15:33:07 +01:00
|
|
|
metrics.WriteFDMetrics(w)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2020-11-03 13:10:42 +01: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())
|
|
|
|
|
|
|
|
// 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()
|
2020-09-16 21:34:01 +02:00
|
|
|
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()
|