Skip to content

Commit

Permalink
Merge pull request #11 from juexiaolin/master
Browse files Browse the repository at this point in the history
v2.1版本发布
  • Loading branch information
juexiaolin authored Sep 10, 2020
2 parents d641677 + 2be2dc2 commit 9cb8ff1
Show file tree
Hide file tree
Showing 28 changed files with 197 additions and 22 deletions.
17 changes: 17 additions & 0 deletions configs/qcloud-clb7-product.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
credential:
access_key: "access_key"
secret_key: "secret_key"
region: "region"

rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014

products:
- namespace: QCE/LOADBALANCE #指标详情: https://cloud.tencent.com/document/product/248/45045
all_metrics: true
all_instances: true
#only_include_metrics: []
#only_include_instances: [lb-xxxxxxxx]
#extra_labels: [InstanceName]
#statistics_types: [last]
#period_seconds: 60
#metric_name_type: 2
1 change: 1 addition & 0 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
collectorState = make(map[string]int)
)

// 总指标采集器, 包含多个产品的采集器
type TcMonitorCollector struct {
Collectors map[string]*TcProductCollector
config *config.TencentConfig
Expand Down
15 changes: 11 additions & 4 deletions pkg/collector/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ var (
handlerFactoryMap = make(map[string]func(*TcProductCollector, log.Logger) (productHandler, error))
)

// 每个产品的指标处理逻辑
type productHandler interface {
// 获取云监控指标namespace
GetNamespace() string
// 对指标元数据做检验和补充
CheckMetricMeta(meta *metric.TcmMeta) bool
// 是否包含该指标, ture=包含, false=不包含
IsIncludeMetric(m *metric.TcmMetric) bool
// 获取该指标下符合条件的所有实例, 并生成所有的series
GetSeries(tcmMetric *metric.TcmMetric) (series []*metric.TcmSeries, err error)
}

// 将对应的产品handler注册到Factory中
func registerHandler(namespace string, isDefaultEnabled bool, factory func(*TcProductCollector, log.Logger) (productHandler, error)) {
handlerFactoryMap[namespace] = factory
}
Expand All @@ -36,7 +43,7 @@ func (h *baseProductHandler) GetSeries(m *metric.TcmMetric) (slist []*metric.Tcm
continue
}
ql := map[string]string{
h.monitorQueryKey: ins.GetInstanceId(),
h.monitorQueryKey: ins.GetMonitorQueryKey(),
}
s, err := metric.NewTcmSeries(m, ql, ins)
if err != nil {
Expand All @@ -52,7 +59,7 @@ func (h *baseProductHandler) GetSeries(m *metric.TcmMetric) (slist []*metric.Tcm
}
for _, ins := range insList {
ql := map[string]string{
h.monitorQueryKey: ins.GetInstanceId(),
h.monitorQueryKey: ins.GetMonitorQueryKey(),
}
s, err := metric.NewTcmSeries(m, ql, ins)
if err != nil {
Expand All @@ -71,13 +78,13 @@ func (h *baseProductHandler) GetSeries(m *metric.TcmMetric) (slist []*metric.Tcm
}
ins, err := h.collector.InstanceRepo.Get(v)
if err != nil {
level.Error(h.logger).Log("msg", "Instance not found", "id", v)
level.Error(h.logger).Log("msg", "Instance not found", "err", err, "id", v)
continue
}

s, err := metric.NewTcmSeries(m, ql, ins)
if err != nil {
level.Error(h.logger).Log("msg", "Create metric series fail", "metric", m.Meta.MetricName, "instacne", ins.GetInstanceId())
level.Error(h.logger).Log("msg", "Create metric series fail", "err", err, "metric", m.Meta.MetricName, "instacne", ins.GetInstanceId())
continue
}
slist = append(slist, s)
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/handler_cdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type cdbHandler struct {
baseProductHandler
}

func (h *cdbHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *cdbHandler) GetNamespace() string {
return CdbNamespace
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/handler_cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type cdnHandler struct {
baseProductHandler
}

func (h *cdnHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *cdnHandler) GetNamespace() string {
return CdnNamespace
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/collector/handler_clb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type clbHandler struct {
baseProductHandler
}

func (h *clbHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
if len(meta.SupportDimensions) == 0 {
meta.SupportDimensions = append(meta.SupportDimensions, "vip")
}
return true
}

func (h *clbHandler) GetNamespace() string {
return ClbNamespace
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/collector/handler_clb7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package collector

import (
"github.com/go-kit/kit/log"
"github.com/tencentyun/tencentcloud-exporter/pkg/metric"
"github.com/tencentyun/tencentcloud-exporter/pkg/util"
"strings"
)

const (
Clb7Namespace = "QCE/LOADBALANCE"
Clb7InstanceidKey = "vip"
)

var (
Clb7ExcludeMetrics = []string{
"outpkgratio",
"intrafficratio",
"inpkgratio",
"qpsratio",
"activeconnratio",
"newactiveconnratio",
"outtrafficratio",
}
)

func init() {
registerHandler(Clb7Namespace, defaultHandlerEnabled, NewClb7Handler)
}

type clb7Handler struct {
baseProductHandler
}

func (h *clb7Handler) CheckMetricMeta(meta *metric.TcmMeta) bool {
meta.SupportDimensions = append(meta.SupportDimensions, "vip")
return true
}

func (h *clb7Handler) GetNamespace() string {
return Clb7Namespace
}

func (h *clb7Handler) IsIncludeMetric(m *metric.TcmMetric) bool {
if util.IsStrInList(Clb7ExcludeMetrics, strings.ToLower(m.Meta.MetricName)) {
return false
}
return true
}

func NewClb7Handler(c *TcProductCollector, logger log.Logger) (handler productHandler, err error) {
handler = &clb7Handler{
baseProductHandler{
monitorQueryKey: Clb7InstanceidKey,
collector: c,
logger: logger,
},
}
return
}
4 changes: 4 additions & 0 deletions pkg/collector/handler_cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type cosHandler struct {
baseProductHandler
}

func (h *cosHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *cosHandler) GetNamespace() string {
return CosNamespace
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/handler_cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type cvmHandler struct {
baseProductHandler
}

func (h *cvmHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *cvmHandler) GetNamespace() string {
return CvmNamespace
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/handler_dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type dcHandler struct {
baseProductHandler
}

func (h *dcHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *dcHandler) GetNamespace() string {
return DcNamespace
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/handler_dcx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type dcxHandler struct {
baseProductHandler
}

func (h *dcxHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *dcxHandler) GetNamespace() string {
return DcxNamespace
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/collector/handler_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
)

var (
// TODO: Disk未找到
MongoClusterMetrics = []string{
"inserts", "reads", "updates", "deletes", "counts", "aggregates", "clusterconn", "commands", "connper", "clusterdiskusage",
"qps", "success", "delay10", "delay50", "delay100", "timeouts",
Expand All @@ -39,6 +38,10 @@ type mongoHandler struct {
logger log.Logger
}

func (h *mongoHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *mongoHandler) GetNamespace() string {
return MongoNamespace
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/handler_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type natHandler struct {
baseProductHandler
}

func (h *natHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *natHandler) GetNamespace() string {
return NatNamespace
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/handler_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type redisHandler struct {
baseProductHandler
}

func (h *redisHandler) CheckMetricMeta(meta *metric.TcmMeta) bool {
return true
}

func (h *redisHandler) GetNamespace() string {
return RedisNamespace
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/collector/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
)

// 每个产品的指标采集默认实现, 不同的逻辑通过对应的productHandler实现
type TcProductCollector struct {
Namespace string
MetricRepo metric.TcmMetricRepository
Expand Down Expand Up @@ -118,7 +119,12 @@ func (c *TcProductCollector) loadMetricsByProductConf() (err error) {
for _, mname := range metricNames {
meta, err := c.MetricRepo.GetMeta(c.Namespace, mname)
if err != nil {
level.Error(c.logger).Log("msg", "not found metric meta", "Namespace", c.Namespace, "name", mname)
level.Error(c.logger).Log("msg", "Not found metric meta", "Namespace", c.Namespace, "name", mname)
continue
}
// 指标元数据处理, false=跳过
if !c.handler.CheckMetricMeta(meta) {
level.Error(c.logger).Log("msg", " Metric meta check fail, skip", "Namespace", c.Namespace, "name", meta.MetricName)
continue
}

Expand Down Expand Up @@ -177,6 +183,7 @@ func (c *TcProductCollector) initQuerys() (err error) {
return
}

// 执行所有指标的采集
func (c *TcProductCollector) Collect(ch chan<- prometheus.Metric) (err error) {
wg := sync.WaitGroup{}
wg.Add(len(c.Querys))
Expand All @@ -203,14 +210,16 @@ func (c *TcProductCollector) Collect(ch chan<- prometheus.Metric) (err error) {
return
}

// 创建新的TcProductCollector, 每个产品一个
func NewTcProductCollector(namespace string, metricRepo metric.TcmMetricRepository, conf *config.TencentConfig, logger log.Logger) (*TcProductCollector, error) {
factory, exists := handlerFactoryMap[namespace]
if !exists {
return nil, fmt.Errorf("Product handler not found, Namespace=%s ", namespace)
return nil, fmt.Errorf("product handler not found, Namespace=%s ", namespace)
}

var instanceRepoCache instance.TcInstanceRepository
if !util.IsStrInList(instance.NotSupportInstances, namespace) {
// 支持实例自动发现的产品
instanceRepo, err := instance.NewTcInstanceRepository(namespace, conf, logger)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var (
"dcx": "QCE/DCX",
"lb_public": "QCE/LB_PUBLIC",
"public_clb": "QCE/LB_PUBLIC",
"loadbalance": "QCE/LOADBALANCE",
"7layer_clb": "QCE/LOADBALANCE",
"nat_gateway": "QCE/NAT_GATEWAY",
"nat": "QCE/NAT_GATEWAY",
"cos": "QCE/COS",
Expand Down
1 change: 1 addition & 0 deletions pkg/instance/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"
)

// 可用于产品的实例的缓存, TcInstanceRepository
type TcInstanceCache struct {
Raw TcInstanceRepository
cache map[string]TcInstance
Expand Down
4 changes: 4 additions & 0 deletions pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ var NotSupportInstances = []string{
"QCE/CDN",
}

// 每个产品的实例对象, 可用于配置导出指标的额外label填充, 根据字段名获取值
type TcInstance interface {
// 获取实例的id
GetInstanceId() string

// 用于查询云监控数据的主键字段, 一般是实例id
GetMonitorQueryKey() string

// 根据字段名称获取该字段的值, 由各个产品接口具体实现
GetFieldValueByName(string) (string, error)

// 获取实例raw元数据, 每个实例类型不一样
GetMeta() interface{}
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/instance/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ var (
factoryMap = make(map[string]func(*config.TencentConfig, log.Logger) (TcInstanceRepository, error))
)

// 每个产品的实例对象的Repository
type TcInstanceRepository interface {
// 获取实例id
GetInstanceKey() string
// 根据id, 获取实例对象
Get(id string) (TcInstance, error)
// 根据id列表, 获取所有的实例对象
ListByIds(ids []string) ([]TcInstance, error)
// 根据filters, 获取符合条件的所有实例对象
ListByFilters(filters map[string]string) ([]TcInstance, error)
}

Expand All @@ -25,6 +30,7 @@ func NewTcInstanceRepository(namespace string, conf *config.TencentConfig, logge
return f(conf, logger)
}

// 将TcInstanceRepository注册到factoryMap中
func registerRepository(namespace string, factory func(*config.TencentConfig, log.Logger) (TcInstanceRepository, error)) {
factoryMap[namespace] = factory
}
Loading

0 comments on commit 9cb8ff1

Please sign in to comment.