Skip to content

Commit

Permalink
prometheus target config
Browse files Browse the repository at this point in the history
Signed-off-by: MEX7 <mex7.0828@gmail.com>
  • Loading branch information
kl7sn committed Jul 9, 2020
1 parent fc0cd7e commit 287828e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 36 deletions.
37 changes: 1 addition & 36 deletions pkg/proxy/confProxy/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func NewETCDDataSource(prefix string, isWatchPrometheusTargetConfig bool) *DataS
}
xgo.Go(dataSource.watch)
if isWatchPrometheusTargetConfig {
dataSource.PrometheusConfigScanner()
xgo.Go(dataSource.watchPrometheus)
}
return dataSource
Expand Down Expand Up @@ -215,42 +216,6 @@ func (d *DataSource) watch() {
}()
}

func (d *DataSource) watchPrometheus() {
// etcd的key用作配置数据读取
hostKey := strings.Join([]string{"/prometheus", "job"}, "/")
// init watch
watch, err := d.etcdClient.NewWatch(hostKey)

if err != nil {
panic("watch err: " + err.Error())
}
go func() {
for {
select {
case event := <-watch.C():
switch event.Type {
case mvccpb.DELETE:
case mvccpb.PUT:
key, value := string(event.Kv.Key), string(event.Kv.Value)
keyArr := strings.Split(key, "/")
if len(keyArr) != 5 {
fmt.Println("key", key, "value", value)
break
}
content := `
- targets:
- "` + value + `"
labels:
instance: ` + keyArr[4] + `
job: ` + keyArr[3]
util.WriteFile("/etc/prometheus/conf/"+keyArr[3]+".yml", content)
}
}
}
}()
}

// ListenAppConfig listen the app config change
func (d *DataSource) ListenAppConfig(ctx echo.Context, key string) chan *structs.ConfNode {
xlog.Info("confProxy", xlog.String("listenConfig", key))
Expand Down
91 changes: 91 additions & 0 deletions pkg/proxy/confProxy/etcd/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package etcd

import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/douyu/juno-agent/util"
"github.com/douyu/jupiter/pkg/xlog"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/mvcc/mvccpb"
)

func (d *DataSource) watchPrometheus() {
// etcd的key用作配置数据读取
hostKey := strings.Join([]string{"/prometheus", "job"}, "/")
// init watch
watch, err := d.etcdClient.NewWatch(hostKey)

if err != nil {
panic("watch err: " + err.Error())
}
go func() {
for {
select {
case event := <-watch.C():
switch event.Type {
case mvccpb.DELETE:
key, value := string(event.Kv.Key), string(event.Kv.Value)
fmt.Println("key", key, "value", value)

keyArr := strings.Split(key, "/")
if len(keyArr) != 5 {
fmt.Println("key", key, "value", value)
break
}
os.Remove("/tmp/etc/prometheus/conf/" + keyArr[3] + ".yml")
case mvccpb.PUT:
key, value := string(event.Kv.Key), string(event.Kv.Value)
keyArr := strings.Split(key, "/")
if len(keyArr) != 5 {
fmt.Println("key", key, "value", value)
break
}
content := `
- targets:
- "` + value + `"
labels:
instance: ` + keyArr[4] + `
job: ` + keyArr[3]
util.WriteFile("/etc/prometheus/conf/"+keyArr[3]+".yml", content)
}
}
}
}()
}

// PrometheusConfigScanner ..
func (d *DataSource) PrometheusConfigScanner() {

// etcd的key用作配置数据读取
hostKey := strings.Join([]string{"/prometheus", "job"}, "/")

ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
resp, err := d.etcdClient.Get(ctx, hostKey, clientv3.WithPrefix())
if err != nil {
xlog.Error("init get hostKey error", xlog.String("plugin", "confgo"), xlog.String("msg", err.Error()), xlog.String("hostKey", hostKey))
return
}
for _, kv := range resp.Kvs {
key, value := string(kv.Key), string(kv.Value)
keyArr := strings.Split(key, "/")
if len(keyArr) != 5 {
fmt.Println("key", key, "value", value)
break
}
content := `
- targets:
- "` + value + `"
labels:
instance: ` + keyArr[4] + `
job: ` + keyArr[3]
util.WriteFile("/etc/prometheus/conf/"+keyArr[3]+".yml", content)
}
return
}

0 comments on commit 287828e

Please sign in to comment.