mirror of
https://github.com/Chipazawra/v8-1c-cluster-pde.git
synced 2024-11-21 16:46:33 +02:00
add pusher mode
This commit is contained in:
parent
ec481afd43
commit
9dcf16743a
@ -14,6 +14,16 @@
|
||||
websocket
|
||||
}
|
||||
|
||||
errors stderr
|
||||
tls off
|
||||
}
|
||||
|
||||
:9091 {
|
||||
basicauth / {$ADMIN_USER} {$ADMIN_PASSWORD}
|
||||
proxy / pushgateway:9091 {
|
||||
transparent
|
||||
}
|
||||
|
||||
errors stderr
|
||||
tls off
|
||||
}
|
@ -8,7 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
|
||||
err := app.Run()
|
||||
err := app.RunPusher()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -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:
|
||||
|
@ -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())
|
||||
}
|
||||
|
82
internal/pusher/pusher.go
Normal file
82
internal/pusher/pusher.go
Normal file
@ -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
|
||||
}
|
@ -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']
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user