Skip to content

Commit

Permalink
use reg to gen prometheus 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 10, 2020
1 parent cb6bc5c commit 8d5a2fc
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 24 deletions.
5 changes: 3 additions & 2 deletions config/config-live.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
timeout="3s"
secure=false
enable = true
[plugin.regProxy.prometheus]
enable = true
path = "/tmp/etc/prometheus/conf222"
[plugin.confProxy]
# 配置中心地址
env=["dev","live","pre"]
Expand All @@ -18,8 +21,6 @@
secure=false
[plugin.confProxy.etcd]
enable=true
isWatchPrometheusTargetConfig = true
prometheusTargetConfigPath= "/etc/prometheus/conf"
endpoints=["127.0.0.1:2379"]
[plugin.supervisor]
enable = true
Expand Down
5 changes: 3 additions & 2 deletions pkg/core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package core
import (
"context"
"fmt"
"github.com/douyu/juno-agent/pkg/nginx"
"sync"
"time"

"github.com/douyu/juno-agent/pkg/nginx"

"github.com/douyu/juno-agent/pkg/check"
"github.com/douyu/juno-agent/pkg/mbus"
"github.com/douyu/juno-agent/pkg/mbus/rocketmq"
Expand Down Expand Up @@ -116,7 +117,7 @@ func (eng *Engine) startConfProxy() error {

// startRegProxy start app regist proxy plugin
func (eng *Engine) startRegProxy() error {
eng.regProxy = regProxy.NewRegProxy(etcdv3.RawConfig("plugin.regProxy").Build())
eng.regProxy = regProxy.StdConfig("regProxy").Build()
if err := eng.regProxy.Start(); err != nil {
return err
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/proxy/confProxy/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func NewETCDDataSource(prefix string, etcdConfig ConfDataSourceEtcd) *DataSource
prefix: prefix,
}
xgo.Go(dataSource.watch)
if etcdConfig.IsWatchPrometheusTargetConfig {
dataSource.PrometheusConfigScanner(etcdConfig.PrometheusTargetConfigPath)
go dataSource.watchPrometheus(etcdConfig.PrometheusTargetConfigPath)
}
return dataSource
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/proxy/confProxy/etcd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ package etcd
type ConfDataSourceEtcd struct {
Enable bool // 是否开启用该数据源
Secure bool
IsWatchPrometheusTargetConfig bool
PrometheusTargetConfigPath string
EndPoints []string `json:"endpoints"` // 注册中心etcd节点信息
}
2 changes: 0 additions & 2 deletions pkg/proxy/confProxy/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ func DefaultConfig() Config {
},
Etcd: etcd.ConfDataSourceEtcd{
Enable: false,
IsWatchPrometheusTargetConfig: false,
PrometheusTargetConfigPath: "/tmp/etc/prometheus/conf",
Secure: false,
EndPoints: []string{"127.0.0.1:2379"},
},
Expand Down
62 changes: 62 additions & 0 deletions pkg/proxy/regProxy/etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2020 Douyu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package etcd

import (
"container/list"
"errors"

"github.com/douyu/juno-agent/pkg/structs"
"github.com/douyu/jupiter/pkg/client/etcdv3"
"github.com/douyu/jupiter/pkg/util/xgo"
)

var (
// ErrEnvPass ...
ErrEnvPass = errors.New("env pass")
)

// DataSource etcd conf datasource
type DataSource struct {
etcdClient *etcdv3.Client
prefix string
// 用于记录长轮训的应用信息
jm list.List // *job
}

// configNode etcd node chan info
type configNode struct {
key string
ch chan *structs.ConfNode
}

// NewETCDDataSource ...
func NewETCDDataSource(prometheusTargetGenConfig PluginRegProxyPrometheus) *DataSource {
dataSource := &DataSource{
etcdClient: etcdv3.RawConfig("plugin.regProxy").Build(),
}
if prometheusTargetGenConfig.Enable {
dataSource.PrometheusConfigScanner(prometheusTargetGenConfig.Path)
xgo.Go(func() {
dataSource.watchPrometheus(prometheusTargetGenConfig.Path)
})
}
return dataSource
}

// GetClient ..
func (d *DataSource) GetClient() *etcdv3.Client {
return d.etcdClient
}
File renamed without changes.
7 changes: 7 additions & 0 deletions pkg/proxy/regProxy/etcd/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package etcd

// PluginRegProxyPrometheus ETCD dataSource config
type PluginRegProxyPrometheus struct {
Enable bool // 是否开启用该数据源
Path string
}
34 changes: 27 additions & 7 deletions pkg/proxy/regProxy/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,34 @@ import (
"fmt"
"time"

"github.com/douyu/juno-agent/pkg/proxy/regProxy/etcd"
"github.com/douyu/jupiter/pkg/conf"
"github.com/douyu/jupiter/pkg/flag"
"github.com/douyu/jupiter/pkg/util/xtime"
"github.com/douyu/jupiter/pkg/xlog"
)

// Config regConfig
type Config struct {
EndPoints []string `json:"endpoints"`
Timeout time.Duration
Secure bool
Enable bool // Whether to open the open plug-in
EndPoints []string `json:"endpoints"`
Timeout time.Duration
Secure bool
Enable bool // Whether to open the open plug-in
Prometheus etcd.PluginRegProxyPrometheus
}

// StdConfig returns standard configuration information
func StdConfig(key string) Config {
func StdConfig(key string) *Config {
var config = DefaultConfig()
if err := conf.UnmarshalKey(key, &config, conf.TagName("toml")); err != nil {

if err := conf.UnmarshalKey(fmt.Sprintf("plugin.%s", key), &config, conf.TagName("toml")); err != nil {
fmt.Printf("loadRegistryConfig.err:%#v\n", err)
xlog.Error("confProxy", xlog.String("parse config err", err.Error()))
panic(err)
}
return config
flagConfig := flag.Bool("regProxy")
config.Enable = flagConfig || config.Enable
return &config
}

// DefaultConfig return default config
Expand All @@ -47,5 +55,17 @@ func DefaultConfig() Config {
Timeout: xtime.Duration("1s"),
Secure: false,
Enable: false,
Prometheus: etcd.PluginRegProxyPrometheus{
Enable: false,
Path: "/home/www/server/prometheus/conf",
},
}
}

// Build new the instance
func (c *Config) Build() *RegProxy {
if c.Enable {
return NewRegProxy(etcd.NewETCDDataSource(c.Prometheus))
}
return nil
}
11 changes: 6 additions & 5 deletions pkg/proxy/regProxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/douyu/jupiter/pkg/xlog"
"go.etcd.io/etcd/proxy/grpcproxy"
"google.golang.org/grpc/examples/helloworld/helloworld"
"net/url"
"strconv"
"strings"
"sync"

"github.com/douyu/juno-agent/pkg/proxy/regProxy/etcd"
"github.com/douyu/juno-agent/pkg/structs"
"github.com/douyu/jupiter/pkg/client/etcdv3"
"github.com/douyu/jupiter/pkg/util/xdebug"
"github.com/douyu/jupiter/pkg/util/xstring"
"github.com/douyu/jupiter/pkg/xlog"
pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
"go.etcd.io/etcd/proxy/grpcproxy"
"google.golang.org/grpc/examples/helloworld/helloworld"
)

// RegProxy ...
Expand All @@ -47,9 +48,9 @@ type RegProxy struct {
}

// NewRegProxy ...
func NewRegProxy(registryClient *etcdv3.Client) *RegProxy {
func NewRegProxy(confClient *etcd.DataSource) *RegProxy {
proxy := &RegProxy{
Client: registryClient,
Client: confClient.GetClient(),
nodeChan: make(chan *structs.ServiceNode, 100),
}
return proxy
Expand Down

0 comments on commit 8d5a2fc

Please sign in to comment.