Skip to content

Commit 413733d

Browse files
author
zianazhao
committed
fix:
1、lb-ipv6 2、cdn-qcloud
1 parent 30a1716 commit 413733d

File tree

6 files changed

+83
-30
lines changed

6 files changed

+83
-30
lines changed

pkg/client/client.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ import (
3434
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
3535
)
3636

37-
func NewMonitorClient(cred common.CredentialIface, conf *config.TencentConfig) (*monitor.Client, error) {
37+
func NewMonitorClient(cred common.CredentialIface, conf *config.TencentConfig, region string) (*monitor.Client, error) {
3838
cpf := profile.NewClientProfile()
3939
if conf.Credential.IsInternal == true {
4040
cpf.HttpProfile.Endpoint = "monitor.internal.tencentcloudapi.com"
4141
} else {
4242
cpf.HttpProfile.Endpoint = "monitor.tencentcloudapi.com"
4343
}
44-
return monitor.NewClient(cred, conf.Credential.Region, cpf)
44+
return monitor.NewClient(cred, region, cpf)
4545
}
4646

4747
func NewMongodbClient(cred common.CredentialIface, conf *config.TencentConfig) (*mongodb.Client, error) {
@@ -258,15 +258,20 @@ func NewCosClient(cred common.CredentialIface, conf *config.TencentConfig) (*cos
258258
// 用于Get Service 查询, service域名暂时只支持外网
259259
su, _ := url.Parse("http://cos." + conf.Credential.Region + ".myqcloud.com")
260260
b := &cos.BaseURL{BucketURL: nil, ServiceURL: su}
261-
// client := cos.NewClient(b, &http.Client{
262-
// Transport: &cos.AuthorizationTransport{
263-
// SecretID: conf.Credential.AccessKey,
264-
// SecretKey: conf.Credential.SecretKey,
265-
// },
266-
// })
267-
client := cos.NewClient(b, &http.Client{
268-
Transport: common.NewCredentialTransport(cred.GetRole()),
269-
})
261+
client := &cos.Client{}
262+
if conf.Credential.Role == "" {
263+
client = cos.NewClient(b, &http.Client{
264+
Transport: &cos.AuthorizationTransport{
265+
SecretID: conf.Credential.AccessKey,
266+
SecretKey: conf.Credential.SecretKey,
267+
},
268+
})
269+
} else {
270+
client = cos.NewClient(b, &http.Client{
271+
Transport: common.NewCredentialTransport(cred.GetRole()),
272+
})
273+
}
274+
270275
return client, nil
271276
}
272277

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ var (
7676
}
7777
)
7878

79+
var QcloudNamespace = []string{"COS", "CDN"}
80+
7981
type TencentCredential struct {
8082
AccessKey string `yaml:"access_key"`
8183
SecretKey string `yaml:"secret_key"`

pkg/instance/instance_clb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type ClbInstance struct {
1515
func (ins *ClbInstance) GetMonitorQueryKey() string {
1616
if len(ins.meta.LoadBalancerVips) == 1 {
1717
return *ins.meta.LoadBalancerVips[0]
18+
} else if *ins.meta.AddressIPv6 != "" {
19+
return *ins.meta.AddressIPv6
1820
} else {
1921
return ""
2022
}

pkg/instance/repository_cos.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func init() {
1818
}
1919

2020
type CosTcInstanceRepository struct {
21+
region string
2122
client *sdk.Client
2223
logger log.Logger
2324
}
@@ -62,6 +63,11 @@ func (repo *CosTcInstanceRepository) ListByFilters(filters map[string]string) (i
6263
return
6364
}
6465
for _, meta := range resp.Buckets {
66+
// when region is ap-guangzhou, will get all buckets in every region.
67+
// need to filter by region
68+
if meta.Region != repo.region {
69+
continue
70+
}
6571
ins, e := NewCosTcInstance(meta.Name, &meta)
6672
if e != nil {
6773
level.Error(repo.logger).Log("msg", "Create Cos instance fail", "id", meta.Name)
@@ -84,6 +90,7 @@ func NewCosTcInstanceRepository(cred common.CredentialIface, c *config.TencentCo
8490
return
8591
}
8692
repo = &CosTcInstanceRepository{
93+
region: c.Credential.Region,
8794
client: cli,
8895
logger: logger,
8996
}

pkg/metric/repository.go

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/tencentyun/tencentcloud-exporter/pkg/util"
9+
810
"github.com/tencentyun/tencentcloud-exporter/pkg/common"
911

1012
"github.com/go-kit/log"
1113
"github.com/go-kit/log/level"
1214
"golang.org/x/time/rate"
1315

1416
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
17+
v20180724 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
1518
"github.com/tencentyun/tencentcloud-exporter/pkg/client"
1619
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
1720
)
@@ -33,10 +36,11 @@ type TcmMetricRepository interface {
3336
}
3437

3538
type TcmMetricRepositoryImpl struct {
36-
credential common.CredentialIface
37-
monitorClient *monitor.Client
38-
limiter *rate.Limiter // 限速
39-
ctx context.Context
39+
credential common.CredentialIface
40+
monitorClient *monitor.Client
41+
monitorClientInGuangzhou *monitor.Client
42+
limiter *rate.Limiter // 限速
43+
ctx context.Context
4044

4145
queryMetricBatchSize int
4246

@@ -121,14 +125,20 @@ func (repo *TcmMetricRepositoryImpl) GetSamples(s *TcmSeries, st int64, et int64
121125
}
122126
request.Instances = []*monitor.Instance{instanceFilters}
123127

124-
stStr := time.Unix(st, 0).Format(timeStampFormat)
128+
stStr := util.FormatTime(time.Unix(st, 0), timeStampFormat)
125129
request.StartTime = &stStr
126130
if et != 0 {
127-
etStr := time.Unix(et, 0).Format(timeStampFormat)
128-
request.StartTime = &etStr
131+
etStr := util.FormatTime(time.Unix(et, 0), timeStampFormat)
132+
request.EndTime = &etStr
129133
}
130134

131-
response, err := repo.monitorClient.GetMonitorData(request)
135+
response := &v20180724.GetMonitorDataResponse{}
136+
if util.IsStrInList(config.QcloudNamespace, s.Metric.Meta.ProductName) {
137+
response, err = repo.monitorClientInGuangzhou.GetMonitorData(request)
138+
} else {
139+
response, err = repo.monitorClient.GetMonitorData(request)
140+
}
141+
level.Info(repo.logger).Log("reqid",response.Response.RequestId)
132142
if err != nil {
133143
return
134144
}
@@ -174,7 +184,14 @@ func (repo *TcmMetricRepositoryImpl) listSampleByBatch(
174184
}
175185

176186
request := repo.buildGetMonitorDataRequest(m, seriesList, st, et)
177-
response, err := repo.monitorClient.GetMonitorData(request)
187+
188+
response := &v20180724.GetMonitorDataResponse{}
189+
if util.IsStrInList(config.QcloudNamespace, m.Meta.ProductName) {
190+
response, err = repo.monitorClientInGuangzhou.GetMonitorData(request)
191+
} else {
192+
response, err = repo.monitorClient.GetMonitorData(request)
193+
}
194+
level.Info(repo.logger).Log("reqid",response.Response.RequestId)
178195
if err != nil {
179196
return nil, err
180197
}
@@ -217,11 +234,11 @@ func (repo *TcmMetricRepositoryImpl) buildGetMonitorDataRequest(
217234
request.Instances = append(request.Instances, ifilters)
218235
}
219236

220-
stStr := time.Unix(st, 0).Format(timeStampFormat)
237+
stStr := util.FormatTime(time.Unix(st, 0), timeStampFormat)
221238
request.StartTime = &stStr
222239
if et != 0 {
223-
etStr := time.Unix(et, 0).Format(timeStampFormat)
224-
request.StartTime = &etStr
240+
etStr := util.FormatTime(time.Unix(et, 0), timeStampFormat)
241+
request.EndTime = &etStr
225242
}
226243
return request
227244
}
@@ -259,18 +276,23 @@ func (repo *TcmMetricRepositoryImpl) buildSamples(
259276
}
260277

261278
func NewTcmMetricRepository(cred common.CredentialIface, conf *config.TencentConfig, logger log.Logger) (repo TcmMetricRepository, err error) {
262-
monitorClient, err := client.NewMonitorClient(cred, conf)
279+
monitorClient, err := client.NewMonitorClient(cred, conf, conf.Credential.Region)
280+
if err != nil {
281+
return
282+
}
283+
monitorClientInGuangzhou, err := client.NewMonitorClient(cred, conf, "ap-guangzhou")
263284
if err != nil {
264285
return
265286
}
266287

267288
repo = &TcmMetricRepositoryImpl{
268-
credential: cred,
269-
monitorClient: monitorClient,
270-
limiter: rate.NewLimiter(rate.Limit(conf.RateLimit), 1),
271-
ctx: context.Background(),
272-
queryMetricBatchSize: conf.MetricQueryBatchSize,
273-
logger: logger,
289+
credential: cred,
290+
monitorClient: monitorClient,
291+
monitorClientInGuangzhou: monitorClientInGuangzhou,
292+
limiter: rate.NewLimiter(rate.Limit(conf.RateLimit), 1),
293+
ctx: context.Background(),
294+
queryMetricBatchSize: conf.MetricQueryBatchSize,
295+
logger: logger,
274296
}
275297

276298
return

pkg/util/time.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package util
2+
3+
import "time"
4+
5+
// FormatTime formats time by zone.
6+
func FormatTime(t time.Time, format string) string {
7+
var local time.Time
8+
_, offset := t.Zone()
9+
if offset == 0 {
10+
local = t.Add(8 * time.Hour)
11+
} else {
12+
local = t
13+
}
14+
return local.Format(format)
15+
}

0 commit comments

Comments
 (0)