add prometheus plugin skelton

This commit is contained in:
Ulric Qin 2022-04-28 15:20:57 +08:00
parent 7b70e7ae0b
commit 316c4a75e7
3 changed files with 163 additions and 0 deletions

View File

@ -32,6 +32,7 @@ import (
_ "flashcat.cloud/categraf/inputs/ping"
_ "flashcat.cloud/categraf/inputs/processes"
_ "flashcat.cloud/categraf/inputs/procstat"
_ "flashcat.cloud/categraf/inputs/prometheus"
_ "flashcat.cloud/categraf/inputs/redis"
_ "flashcat.cloud/categraf/inputs/system"
_ "flashcat.cloud/categraf/inputs/tomcat"

View File

@ -0,0 +1,33 @@
# # collect interval
# interval = 15
[[instances]]
urls = [
"http://localhost:9104/metrics"
]
# bearer_token = ""
# # basic auth
# username = ""
# password = ""
# # interval = global.interval * interval_times
# interval_times = 1
# labels = {}
# support glob
ignore_metrics = [ "go_*" ]
# timeout for every url
timeout = "3s"
## Optional TLS Config
# use_tls = false
# tls_min_version = "1.2"
# tls_ca = "/etc/categraf/ca.pem"
# tls_cert = "/etc/categraf/cert.pem"
# tls_key = "/etc/categraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = true

View File

@ -0,0 +1,129 @@
package prometheus
import (
"errors"
"net/http"
"sync"
"sync/atomic"
"time"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/pkg/tls"
"flashcat.cloud/categraf/types"
"github.com/toolkits/pkg/container/list"
)
const inputName = "prometheus"
type Instance struct {
URLs []string `toml:"urls"`
Labels map[string]string `toml:"labels"`
IntervalTimes int64 `toml:"interval_times"`
BearerToken string `toml:"bearer_token"`
Username string `toml:"username"`
Password string `toml:"password"`
IgnoreMetrics []string `toml:"ignore_metrics"`
Timeout config.Duration `toml:"timeout"`
tls.ClientConfig
client *http.Client
}
func (ins *Instance) Init() error {
if len(ins.URLs) == 0 {
return errors.New("urls is empty")
}
if ins.Timeout <= 0 {
ins.Timeout = config.Duration(time.Second * 3)
}
client, err := ins.createHTTPClient()
if err != nil {
return err
}
ins.client = client
return nil
}
func (ins *Instance) createHTTPClient() (*http.Client, error) {
trans := &http.Transport{}
if ins.UseTLS {
tlsConfig, err := ins.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
trans.TLSClientConfig = tlsConfig
}
client := &http.Client{
Transport: trans,
Timeout: time.Duration(ins.Timeout),
}
return client, nil
}
type Prometheus struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
}
func init() {
inputs.Add(inputName, func() inputs.Input {
return &Prometheus{}
})
}
func (p *Prometheus) GetInputName() string {
return ""
}
func (p *Prometheus) GetInterval() config.Duration {
return p.Interval
}
func (p *Prometheus) Init() error {
if len(p.Instances) == 0 {
return types.ErrInstancesEmpty
}
for i := 0; i < len(p.Instances); i++ {
if err := p.Instances[i].Init(); err != nil {
return err
}
}
return nil
}
func (p *Prometheus) Drop() {}
func (p *Prometheus) Gather(slist *list.SafeList) {
atomic.AddUint64(&p.Counter, 1)
for i := range p.Instances {
ins := p.Instances[i]
p.wg.Add(1)
go p.gatherOnce(slist, ins)
}
p.wg.Wait()
}
func (p *Prometheus) gatherOnce(slist *list.SafeList, ins *Instance) {
defer p.wg.Done()
if ins.IntervalTimes > 0 {
counter := atomic.LoadUint64(&p.Counter)
if counter%uint64(ins.IntervalTimes) != 0 {
return
}
}
// TODO
}