Skip to content

Commit 51a3d3a

Browse files
author
carldai
committed
fix cos data in guangzhou
1 parent cc148bc commit 51a3d3a

File tree

4 files changed

+40
-590
lines changed

4 files changed

+40
-590
lines changed

pkg/client/client.go

Lines changed: 2 additions & 2 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) {

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: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"golang.org/x/time/rate"
1515

1616
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
17+
v20180724 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
1718
"github.com/tencentyun/tencentcloud-exporter/pkg/client"
1819
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
1920
)
@@ -35,10 +36,11 @@ type TcmMetricRepository interface {
3536
}
3637

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

4345
queryMetricBatchSize int
4446

@@ -130,7 +132,12 @@ func (repo *TcmMetricRepositoryImpl) GetSamples(s *TcmSeries, st int64, et int64
130132
request.EndTime = &etStr
131133
}
132134

133-
response, err := repo.monitorClient.GetMonitorData(request)
135+
response := &v20180724.GetMonitorDataResponse{}
136+
if s.Metric.Meta.ProductName == "COS" {
137+
response, err = repo.monitorClientInGuangzhou.GetMonitorData(request)
138+
} else {
139+
response, err = repo.monitorClient.GetMonitorData(request)
140+
}
134141
if err != nil {
135142
return
136143
}
@@ -175,9 +182,14 @@ func (repo *TcmMetricRepositoryImpl) listSampleByBatch(
175182
return nil, err
176183
}
177184

178-
//level.Info(repo.logger).Log("st", st, "et", et)
179185
request := repo.buildGetMonitorDataRequest(m, seriesList, st, et)
180-
response, err := repo.monitorClient.GetMonitorData(request)
186+
187+
response := &v20180724.GetMonitorDataResponse{}
188+
if m.Meta.ProductName == "COS" {
189+
response, err = repo.monitorClientInGuangzhou.GetMonitorData(request)
190+
} else {
191+
response, err = repo.monitorClient.GetMonitorData(request)
192+
}
181193
if err != nil {
182194
return nil, err
183195
}
@@ -262,18 +274,23 @@ func (repo *TcmMetricRepositoryImpl) buildSamples(
262274
}
263275

264276
func NewTcmMetricRepository(cred common.CredentialIface, conf *config.TencentConfig, logger log.Logger) (repo TcmMetricRepository, err error) {
265-
monitorClient, err := client.NewMonitorClient(cred, conf)
277+
monitorClient, err := client.NewMonitorClient(cred, conf, conf.Credential.Region)
278+
if err != nil {
279+
return
280+
}
281+
monitorClientInGuangzhou, err := client.NewMonitorClient(cred, conf, "ap-guangzhou")
266282
if err != nil {
267283
return
268284
}
269285

270286
repo = &TcmMetricRepositoryImpl{
271-
credential: cred,
272-
monitorClient: monitorClient,
273-
limiter: rate.NewLimiter(rate.Limit(conf.RateLimit), 1),
274-
ctx: context.Background(),
275-
queryMetricBatchSize: conf.MetricQueryBatchSize,
276-
logger: logger,
287+
credential: cred,
288+
monitorClient: monitorClient,
289+
monitorClientInGuangzhou: monitorClientInGuangzhou,
290+
limiter: rate.NewLimiter(rate.Limit(conf.RateLimit), 1),
291+
ctx: context.Background(),
292+
queryMetricBatchSize: conf.MetricQueryBatchSize,
293+
logger: logger,
277294
}
278295

279296
return

0 commit comments

Comments
 (0)