diff --git a/caddy/Caddyfile b/caddy/Caddyfile index 1dd45c8..84a960a 100644 --- a/caddy/Caddyfile +++ b/caddy/Caddyfile @@ -14,6 +14,16 @@ websocket } + errors stderr + tls off +} + +:9091 { + basicauth / {$ADMIN_USER} {$ADMIN_PASSWORD} + proxy / pushgateway:9091 { + transparent + } + errors stderr tls off } \ No newline at end of file diff --git a/cmd/app/main.go b/cmd/app/main.go index c716e07..44da2d6 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -8,7 +8,7 @@ import ( func main() { - err := app.Run() + err := app.RunPusher() if err != nil { log.Fatal(err) diff --git a/docker-compose.yaml b/docker-compose.yaml index 26d90e3..0266704 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -75,6 +75,17 @@ services: - ADMIN_USER=${ADMIN_USER} - ADMIN_PASSWORD=${ADMIN_PASSWORD} restart: unless-stopped + networks: + - monitoring + labels: + org.label-schema.group: "monitoring" + + pushgateway: + image: prom/pushgateway:v1.2.0 + container_name: pushgateway + restart: unless-stopped + expose: + - 9091 networks: - monitoring labels: diff --git a/internal/app/app.go b/internal/app/app.go index d7f9647..f955fba 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -1,12 +1,14 @@ package app import ( + "context" "flag" "fmt" "log" "net/http" "os" + pusher "github.com/Chipazawra/v8-1c-cluster-pde/internal/Pusher" "github.com/Chipazawra/v8-1c-cluster-pde/internal/rpHostsCollector" "github.com/caarlos0/env" rascli "github.com/khorevaa/ras-client" @@ -69,3 +71,17 @@ func Run() error { return nil } + +func RunPusher() error { + + rcli := rascli.NewClient(fmt.Sprintf("%s:%s", conf.Host, conf.Port)) + rcli.AuthenticateAgent(conf.User, conf.Pass) + log.Printf("cluster-pde connected to RAS: %v", fmt.Sprintf("%s:%s", conf.Host, conf.Port)) + defer rcli.Close() + + return pusher.New( + rpHostsCollector.New(rcli), + "pushgateway:9091", + pusher.WithInterval(500), + ).Run(context.Background()) +} diff --git a/internal/pusher/pusher.go b/internal/pusher/pusher.go new file mode 100644 index 0000000..06d0eb4 --- /dev/null +++ b/internal/pusher/pusher.go @@ -0,0 +1,82 @@ +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 + } +} + +func New(collector prometheus.Collector, url string, opts ...PusherOption) *Pusher { + + p := &Pusher{ + collector: collector, + intervalMillis: defaultIntervalMillis, + url: url, + jobName: defaultJobname, + } + + for _, opt := range opts { + opt(p) + } + + p.pusher = push.New(url, p.jobName).Collector(collector) + + return p +} + +func (p *Pusher) Run(ctx context.Context) error { + + ticker := time.NewTicker(time.Duration(p.intervalMillis * int(time.Microsecond))) + done := make(chan error) + go func(done chan error) { + Loop: + for { + select { + case <-ticker.C: + err := p.pusher.Push() + if err != nil { + done <- fmt.Errorf("puser: %v", err) + break Loop + } + case <-ctx.Done(): + log.Println("INFO: pusher context complete") + done <- nil + break Loop + } + } + close(done) + }(done) + + return <-done +} diff --git a/prometheus/prometheus.yml b/prometheus/prometheus.yml index 351c978..08b8b0f 100644 --- a/prometheus/prometheus.yml +++ b/prometheus/prometheus.yml @@ -19,3 +19,9 @@ scrape_configs: static_configs: - targets: ['localhost:9090'] + - job_name: 'pushgateway' + scrape_interval: 5s + honor_labels: true + static_configs: + - targets: ['pushgateway:9091'] +