Skip to content

Commit 02a4979

Browse files
authored
Merge pull request #72 from zianazhao/master
lable-tag
2 parents 5318702 + 32789cc commit 02a4979

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

.github/workflows/docker-publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ jobs:
3939
# https://github.com/sigstore/cosign-installer
4040
- name: Install cosign
4141
if: github.event_name != 'pull_request'
42-
uses: sigstore/cosign-installer@1e95c1de343b5b0c23352d6417ee3e48d5bcd422
42+
uses: sigstore/cosign-installer@14d43345ff50608baaa37893f4822c406ed470a9
4343
with:
44-
cosign-release: 'v1.4.0'
44+
cosign-release: 'v1.11.1'
4545

4646

4747
# Workaround: https://github.com/docker/build-push-action/issues/461
@@ -70,7 +70,7 @@ jobs:
7070
# https://github.com/docker/build-push-action
7171
- name: Build and push Docker image
7272
id: build-and-push
73-
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
73+
uses: docker/build-push-action@965c6a410d446a30e95d35052c67d6eded60dad6
7474
with:
7575
context: .
7676
push: ${{ github.event_name != 'pull_request' }}

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ products:
8181
```yaml
8282
credential:
8383
access_key: <YOUR_ACCESS_KEY> // 必须, 云API的SecretId
84-
access_secret: <YOUR_ACCESS_SECRET> // 必须, 云API的SecretKey
85-
role: <YOUR_SERVICE_ROLE> // 可选,可在云上CVM/TKE中使用,没有access_key和access_secret时会使用role申请临时密钥
84+
secret_key: <YOUR_ACCESS_SECRET> // 必须, 云API的SecretKey
8685
region: <REGION> // 必须, 实例所在区域信息
8786

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

@@ -173,4 +171,3 @@ export TENCENTCLOUD_REGION="REGION"
173171

174172

175173

176-

pkg/instance/instance.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package instance
22

33
import (
44
"fmt"
5+
"github.com/tencentyun/tencentcloud-exporter/pkg/util"
56
"reflect"
67
)
78

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

20+
// 根据字段名称获取该字段的值, 由各个产品接口具体实现
21+
GetFieldValuesByName(string) (map[string][]string, error)
22+
1923
// 获取实例raw元数据, 每个实例类型不一样
2024
GetMeta() interface{}
2125
}
@@ -36,7 +40,7 @@ func (ins *baseTcInstance) GetMonitorQueryKey() string {
3640
func (ins *baseTcInstance) GetFieldValueByName(name string) (val string, err error) {
3741
defer func() {
3842
if err := recover(); err != nil {
39-
//nothing ignore err
43+
// nothing ignore err
4044
}
4145
}()
4246
v := ins.value.FieldByName(name)
@@ -45,3 +49,44 @@ func (ins *baseTcInstance) GetFieldValueByName(name string) (val string, err err
4549
}
4650
return fmt.Sprintf("%v", v.Interface()), nil
4751
}
52+
53+
func (ins *baseTcInstance) GetFieldValuesByName(name string) (val map[string][]string, err error) {
54+
defer func() {
55+
if err := recover(); err != nil {
56+
// nothing ignore err
57+
}
58+
}()
59+
v := ins.value.FieldByName(name)
60+
if v.Kind() == reflect.Ptr {
61+
v = reflect.Indirect(v)
62+
}
63+
valueMap := make(map[string][]string)
64+
if v.Kind() == reflect.Slice {
65+
for i := 0; i < v.Len(); i++ {
66+
if v.Index(i).Elem().Kind() == reflect.String {
67+
valueMap[name] = append(val[name], fmt.Sprintf("%v", v.Index(i).Elem().Interface()))
68+
} else if v.Index(i).Elem().Kind() == reflect.Struct {
69+
var tagKey, tagValue reflect.Value
70+
if v.Index(i).Elem().FieldByName("TagKey").IsValid() && v.Index(i).Elem().FieldByName("TagValue").IsValid() {
71+
tagKey = v.Index(i).Elem().FieldByName("TagKey")
72+
tagValue = v.Index(i).Elem().FieldByName("TagValue")
73+
} else if v.Index(i).Elem().FieldByName("Key").IsValid() && v.Index(i).Elem().FieldByName("Value").IsValid() {
74+
tagKey = v.Index(i).Elem().FieldByName("Key")
75+
tagValue = v.Index(i).Elem().FieldByName("Value")
76+
}
77+
if tagKey.Kind() == reflect.Ptr {
78+
tagKey = reflect.Indirect(tagKey)
79+
}
80+
if tagValue.Kind() == reflect.Ptr {
81+
tagValue = reflect.Indirect(tagValue)
82+
}
83+
if util.IsValidTagKey(tagKey.String()) {
84+
valueMap[tagKey.String()] = append(val[tagKey.String()], tagValue.String())
85+
}
86+
}
87+
}
88+
} else {
89+
valueMap[name] = append(val[name], fmt.Sprintf("%v", v.Interface()))
90+
}
91+
return valueMap, nil
92+
}

pkg/instance/repository_tdmq.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ func init() {
1818
registerRepository("QCE/TDMQ", NewTdmqTcInstanceRepository)
1919
}
2020

21+
var includeVip = "includeVip"
22+
var includeVipTrue = "true"
23+
2124
type TdmqTcInstanceRepository struct {
2225
client *sdk.Client
2326
logger log.Logger
@@ -29,6 +32,10 @@ func (repo *TdmqTcInstanceRepository) GetInstanceKey() string {
2932

3033
func (repo *TdmqTcInstanceRepository) Get(id string) (instance TcInstance, err error) {
3134
req := sdk.NewDescribeRocketMQClustersRequest()
35+
req.Filters = []*sdk.Filter{{
36+
Name: &includeVip,
37+
Values: []*string{&includeVipTrue},
38+
}}
3239
req.ClusterIdList = []*string{&id}
3340
resp, err := repo.client.DescribeRocketMQClusters(req)
3441
if err != nil {
@@ -57,7 +64,10 @@ func (repo *TdmqTcInstanceRepository) ListByFilters(filters map[string]string) (
5764

5865
req.Offset = &offset
5966
req.Limit = &limit
60-
67+
req.Filters = []*sdk.Filter{{
68+
Name: &includeVip,
69+
Values: []*string{&includeVipTrue},
70+
}}
6171
getMoreInstances:
6272
resp, err := repo.client.DescribeRocketMQClusters(req)
6373
if err != nil {

pkg/metric/label.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ func (l *TcmLabels) GetValues(filters map[string]string, ins instance.TcInstance
4444
}
4545
}
4646
for _, name := range l.instanceLabelNames {
47-
v, e := ins.GetFieldValueByName(name)
48-
if e == nil && v != "" {
49-
nameValues[name] = v
47+
vMap, e := ins.GetFieldValuesByName(name)
48+
if e == nil && vMap != nil {
49+
for vName, values := range vMap {
50+
for _, value := range values {
51+
nameValues[vName] = value
52+
}
53+
54+
}
5055
}
5156
}
5257
for name, value := range l.constLabels {

pkg/util/label.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package util
2+
3+
import (
4+
"regexp"
5+
"unicode"
6+
)
7+
8+
func IsValidTagKey(str string) bool {
9+
for _, r := range str {
10+
if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
11+
return false
12+
}
13+
}
14+
if !regexp.MustCompile(`^[A-Za-z0-9_]+$`).MatchString(str) {
15+
return false
16+
}
17+
return true
18+
}

0 commit comments

Comments
 (0)