70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
|
|
package metric
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/prometheus/client_golang/prometheus"
|
||
|
|
"github.com/prometheus/client_golang/prometheus/push"
|
||
|
|
"os"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
var defaultPusher *push.Pusher
|
||
|
|
|
||
|
|
const (
|
||
|
|
defaultPushIntervalSecond = 5
|
||
|
|
)
|
||
|
|
|
||
|
|
func InitDefaultPusher(gateway string, job string, labels prometheus.Labels, pushIntervalSecond ...int) {
|
||
|
|
defaultPusher = InitPusher(gateway, job, labels)
|
||
|
|
|
||
|
|
// for default pusher , we should add process collector and go collector
|
||
|
|
// then we can monitor all base monitor on our job
|
||
|
|
defaultPusher.
|
||
|
|
Collector(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})).
|
||
|
|
Collector(prometheus.NewGoCollector()).
|
||
|
|
Add()
|
||
|
|
|
||
|
|
// TODO implement init
|
||
|
|
// and push
|
||
|
|
//defaultPusher = InitPusher("")
|
||
|
|
go func() {
|
||
|
|
var pushInterval = defaultPushIntervalSecond * time.Second
|
||
|
|
if len(pushIntervalSecond) > 0 {
|
||
|
|
pushInterval = time.Second * time.Duration(pushIntervalSecond[0])
|
||
|
|
}
|
||
|
|
for {
|
||
|
|
defaultPusher.Push()
|
||
|
|
time.Sleep(pushInterval)
|
||
|
|
}
|
||
|
|
}()
|
||
|
|
}
|
||
|
|
|
||
|
|
// ExitTriggerLastPush
|
||
|
|
// TODO
|
||
|
|
func ExitTriggerLastPush() {
|
||
|
|
|
||
|
|
defaultPusher.Push()
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//var MustLabel prometheus.Labels
|
||
|
|
//
|
||
|
|
//type RequireLabelConf struct {
|
||
|
|
// Service string `json:"service";yaml:`
|
||
|
|
// App string ``
|
||
|
|
// Country string
|
||
|
|
//}
|
||
|
|
|
||
|
|
// Init Pusher
|
||
|
|
// return prometheus pusher
|
||
|
|
// TODO add labels
|
||
|
|
// need lan ,app,country, service labels to implement
|
||
|
|
func InitPusher(gateway string, job string, labels prometheus.Labels) *push.Pusher {
|
||
|
|
host, _ := os.Hostname()
|
||
|
|
|
||
|
|
p := push.New(gateway, job).Grouping("instance", host).Grouping("lang","go")
|
||
|
|
for name, value := range labels {
|
||
|
|
p.Grouping(name, value)
|
||
|
|
}
|
||
|
|
return p
|
||
|
|
}
|