Skip to content

Commit

Permalink
Merge pull request #72 from zianazhao/master
Browse files Browse the repository at this point in the history
lable-tag
  • Loading branch information
zero3233 authored Aug 31, 2022
2 parents 5318702 + 32789cc commit 02a4979
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ jobs:
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@1e95c1de343b5b0c23352d6417ee3e48d5bcd422
uses: sigstore/cosign-installer@14d43345ff50608baaa37893f4822c406ed470a9
with:
cosign-release: 'v1.4.0'
cosign-release: 'v1.11.1'


# Workaround: https://github.com/docker/build-push-action/issues/461
Expand Down Expand Up @@ -70,7 +70,7 @@ jobs:
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
uses: docker/build-push-action@965c6a410d446a30e95d35052c67d6eded60dad6
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ products:
```yaml
credential:
access_key: <YOUR_ACCESS_KEY> // 必须, 云API的SecretId
access_secret: <YOUR_ACCESS_SECRET> // 必须, 云API的SecretKey
role: <YOUR_SERVICE_ROLE> // 可选,可在云上CVM/TKE中使用,没有access_key和access_secret时会使用role申请临时密钥
secret_key: <YOUR_ACCESS_SECRET> // 必须, 云API的SecretKey
region: <REGION> // 必须, 实例所在区域信息

rate_limit: 15 // 腾讯云监控拉取指标数据限制, 官方默认限制最大20qps
Expand Down Expand Up @@ -139,7 +138,6 @@ metrics:
```bash
export TENCENTCLOUD_SECRET_ID="YOUR_ACCESS_KEY"
export TENCENTCLOUD_SECRET_KEY="YOUR_ACCESS_SECRET"
export TENCENTCLOUD_SERVICE_ROLE = "YOUR_SERVICE_ROLE"
export TENCENTCLOUD_REGION="REGION"
```

Expand Down Expand Up @@ -173,4 +171,3 @@ export TENCENTCLOUD_REGION="REGION"




47 changes: 46 additions & 1 deletion pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package instance

import (
"fmt"
"github.com/tencentyun/tencentcloud-exporter/pkg/util"
"reflect"
)

Expand All @@ -16,6 +17,9 @@ type TcInstance interface {
// 根据字段名称获取该字段的值, 由各个产品接口具体实现
GetFieldValueByName(string) (string, error)

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

// 获取实例raw元数据, 每个实例类型不一样
GetMeta() interface{}
}
Expand All @@ -36,7 +40,7 @@ func (ins *baseTcInstance) GetMonitorQueryKey() string {
func (ins *baseTcInstance) GetFieldValueByName(name string) (val string, err error) {
defer func() {
if err := recover(); err != nil {
//nothing ignore err
// nothing ignore err
}
}()
v := ins.value.FieldByName(name)
Expand All @@ -45,3 +49,44 @@ func (ins *baseTcInstance) GetFieldValueByName(name string) (val string, err err
}
return fmt.Sprintf("%v", v.Interface()), nil
}

func (ins *baseTcInstance) GetFieldValuesByName(name string) (val map[string][]string, err error) {
defer func() {
if err := recover(); err != nil {
// nothing ignore err
}
}()
v := ins.value.FieldByName(name)
if v.Kind() == reflect.Ptr {
v = reflect.Indirect(v)
}
valueMap := make(map[string][]string)
if v.Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
if v.Index(i).Elem().Kind() == reflect.String {
valueMap[name] = append(val[name], fmt.Sprintf("%v", v.Index(i).Elem().Interface()))
} else if v.Index(i).Elem().Kind() == reflect.Struct {
var tagKey, tagValue reflect.Value
if v.Index(i).Elem().FieldByName("TagKey").IsValid() && v.Index(i).Elem().FieldByName("TagValue").IsValid() {
tagKey = v.Index(i).Elem().FieldByName("TagKey")
tagValue = v.Index(i).Elem().FieldByName("TagValue")
} else if v.Index(i).Elem().FieldByName("Key").IsValid() && v.Index(i).Elem().FieldByName("Value").IsValid() {
tagKey = v.Index(i).Elem().FieldByName("Key")
tagValue = v.Index(i).Elem().FieldByName("Value")
}
if tagKey.Kind() == reflect.Ptr {
tagKey = reflect.Indirect(tagKey)
}
if tagValue.Kind() == reflect.Ptr {
tagValue = reflect.Indirect(tagValue)
}
if util.IsValidTagKey(tagKey.String()) {
valueMap[tagKey.String()] = append(val[tagKey.String()], tagValue.String())
}
}
}
} else {
valueMap[name] = append(val[name], fmt.Sprintf("%v", v.Interface()))
}
return valueMap, nil
}
12 changes: 11 additions & 1 deletion pkg/instance/repository_tdmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func init() {
registerRepository("QCE/TDMQ", NewTdmqTcInstanceRepository)
}

var includeVip = "includeVip"
var includeVipTrue = "true"

type TdmqTcInstanceRepository struct {
client *sdk.Client
logger log.Logger
Expand All @@ -29,6 +32,10 @@ func (repo *TdmqTcInstanceRepository) GetInstanceKey() string {

func (repo *TdmqTcInstanceRepository) Get(id string) (instance TcInstance, err error) {
req := sdk.NewDescribeRocketMQClustersRequest()
req.Filters = []*sdk.Filter{{
Name: &includeVip,
Values: []*string{&includeVipTrue},
}}
req.ClusterIdList = []*string{&id}
resp, err := repo.client.DescribeRocketMQClusters(req)
if err != nil {
Expand Down Expand Up @@ -57,7 +64,10 @@ func (repo *TdmqTcInstanceRepository) ListByFilters(filters map[string]string) (

req.Offset = &offset
req.Limit = &limit

req.Filters = []*sdk.Filter{{
Name: &includeVip,
Values: []*string{&includeVipTrue},
}}
getMoreInstances:
resp, err := repo.client.DescribeRocketMQClusters(req)
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions pkg/metric/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ func (l *TcmLabels) GetValues(filters map[string]string, ins instance.TcInstance
}
}
for _, name := range l.instanceLabelNames {
v, e := ins.GetFieldValueByName(name)
if e == nil && v != "" {
nameValues[name] = v
vMap, e := ins.GetFieldValuesByName(name)
if e == nil && vMap != nil {
for vName, values := range vMap {
for _, value := range values {
nameValues[vName] = value
}

}
}
}
for name, value := range l.constLabels {
Expand Down
18 changes: 18 additions & 0 deletions pkg/util/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package util

import (
"regexp"
"unicode"
)

func IsValidTagKey(str string) bool {
for _, r := range str {
if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
return false
}
}
if !regexp.MustCompile(`^[A-Za-z0-9_]+$`).MatchString(str) {
return false
}
return true
}

0 comments on commit 02a4979

Please sign in to comment.