1
0
mirror of https://github.com/Chipazawra/v8-1c-cluster-pde.git synced 2025-03-19 21:07:45 +02:00

81 lines
1.5 KiB
Go
Raw Normal View History

2021-12-29 12:31:50 +03:00
package pusher
import (
"context"
"fmt"
"log"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
)
var (
defaultIntervalMillis int = 500
defaultJobname string = "v8-1C-cluster-pde"
)
type Pusher struct {
intervalMillis int
collector prometheus.Collector
url string
jobName string
pusher *push.Pusher
}
type PusherOption func(*Pusher)
func WithInterval(millis int) PusherOption {
return func(p *Pusher) {
p.intervalMillis = millis
}
}
func WithJobName(Name string) PusherOption {
return func(p *Pusher) {
p.jobName = Name
}
}
2021-12-30 16:59:13 +03:00
func WithConfig(config PusherConfig) PusherOption {
return func(p *Pusher) {
p.url = fmt.Sprintf("%s:%s", config.PUSH_HOST, config.PUSH_PORT)
p.intervalMillis = config.PUSH_INTERVAL
}
}
func New(collector prometheus.Collector, opts ...PusherOption) *Pusher {
2021-12-29 12:31:50 +03:00
p := &Pusher{
2021-12-30 16:59:13 +03:00
collector: collector,
jobName: defaultJobname,
2021-12-29 12:31:50 +03:00
}
for _, opt := range opts {
opt(p)
}
2021-12-30 16:59:13 +03:00
p.pusher = push.New(p.url, p.jobName).Collector(collector)
2021-12-29 12:31:50 +03:00
return p
}
2021-12-30 14:53:59 +03:00
func (p *Pusher) Run(ctx context.Context, errchan chan<- error) {
2021-12-30 16:59:13 +03:00
log.Printf("v8-1c-cluster-pde: pusher %v", p.url)
2021-12-29 12:31:50 +03:00
ticker := time.NewTicker(time.Duration(p.intervalMillis * int(time.Microsecond)))
2021-12-30 14:53:59 +03:00
Loop:
for {
select {
case <-ticker.C:
err := p.pusher.Push()
if err != nil {
errchan <- fmt.Errorf("puser: %v", err)
2021-12-29 12:31:50 +03:00
break Loop
}
2021-12-30 14:53:59 +03:00
case <-ctx.Done():
log.Println("INFO: pusher context done")
break Loop
2021-12-29 12:31:50 +03:00
}
2021-12-30 14:53:59 +03:00
}
2021-12-29 12:31:50 +03:00
}