2018-08-27 19:10:55 +02:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-22 19:41:12 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2018-08-27 19:10:55 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
2018-09-08 07:03:41 +02:00
|
|
|
"github.com/zrepl/zrepl/config"
|
2018-08-27 22:21:45 +02:00
|
|
|
"github.com/zrepl/zrepl/daemon/job"
|
2018-09-08 07:03:41 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2018-12-11 22:01:50 +01:00
|
|
|
"github.com/zrepl/zrepl/rpc/dataconn/frameconn"
|
2018-08-27 19:10:55 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type prometheusJob struct {
|
|
|
|
listen string
|
|
|
|
}
|
|
|
|
|
2018-09-08 07:03:41 +02:00
|
|
|
func newPrometheusJobFromConfig(in *config.PrometheusMonitoring) (*prometheusJob, error) {
|
|
|
|
if _, _, err := net.SplitHostPort(in.Listen); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &prometheusJob{in.Listen}, nil
|
2018-08-27 19:10:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var prom struct {
|
2019-03-22 19:41:12 +01:00
|
|
|
taskLogEntries *prometheus.CounterVec
|
2018-08-27 19:10:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prom.taskLogEntries = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Namespace: "zrepl",
|
|
|
|
Subsystem: "daemon",
|
2018-09-08 07:03:41 +02:00
|
|
|
Name: "log_entries",
|
2018-08-27 19:10:55 +02:00
|
|
|
Help: "number of log entries per job task and level",
|
2018-09-08 07:03:41 +02:00
|
|
|
}, []string{"zrepl_job", "level"})
|
2018-08-27 19:10:55 +02:00
|
|
|
prometheus.MustRegister(prom.taskLogEntries)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *prometheusJob) Name() string { return jobNamePrometheus }
|
|
|
|
|
2018-09-23 21:08:03 +02:00
|
|
|
func (j *prometheusJob) Status() *job.Status { return &job.Status{Type: job.TypeInternal} }
|
2018-08-27 19:10:55 +02:00
|
|
|
|
2019-03-20 23:01:24 +01:00
|
|
|
func (j *prometheusJob) OwnedDatasetSubtreeRoot() (p *zfs.DatasetPath, ok bool) { return nil, false }
|
|
|
|
|
2018-09-08 07:03:41 +02:00
|
|
|
func (j *prometheusJob) RegisterMetrics(registerer prometheus.Registerer) {}
|
|
|
|
|
2018-08-27 19:10:55 +02:00
|
|
|
func (j *prometheusJob) Run(ctx context.Context) {
|
|
|
|
|
|
|
|
if err := zfs.PrometheusRegister(prometheus.DefaultRegisterer); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
if err := frameconn.PrometheusRegister(prometheus.DefaultRegisterer); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-08-27 19:10:55 +02:00
|
|
|
log := job.GetLogger(ctx)
|
|
|
|
|
|
|
|
l, err := net.Listen("tcp", j.listen)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("cannot listen")
|
|
|
|
}
|
|
|
|
go func() {
|
2019-03-22 20:45:27 +01:00
|
|
|
<-ctx.Done()
|
|
|
|
l.Close()
|
2018-08-27 19:10:55 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/metrics", promhttp.Handler())
|
|
|
|
|
|
|
|
err = http.Serve(l, mux)
|
2019-03-22 20:45:27 +01:00
|
|
|
if err != nil && ctx.Err() == nil {
|
2018-08-27 19:10:55 +02:00
|
|
|
log.WithError(err).Error("error while serving")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-08 07:03:41 +02:00
|
|
|
|
|
|
|
type prometheusJobOutlet struct {
|
|
|
|
jobName string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ logger.Outlet = prometheusJobOutlet{}
|
|
|
|
|
|
|
|
func newPrometheusLogOutlet(jobName string) prometheusJobOutlet {
|
|
|
|
return prometheusJobOutlet{jobName}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o prometheusJobOutlet) WriteEntry(entry logger.Entry) error {
|
|
|
|
prom.taskLogEntries.WithLabelValues(o.jobName, entry.Level.String()).Inc()
|
|
|
|
return nil
|
|
|
|
}
|