From 413733d8e590210d0a215e89ea90ac742a68b4da Mon Sep 17 00:00:00 2001 From: zianazhao Date: Mon, 17 Oct 2022 11:35:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=201=E3=80=81lb-ipv6=202=E3=80=81cdn-qc?= =?UTF-8?q?loud?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/client/client.go | 27 ++++++++------- pkg/config/config.go | 2 ++ pkg/instance/instance_clb.go | 2 ++ pkg/instance/repository_cos.go | 7 ++++ pkg/metric/repository.go | 60 +++++++++++++++++++++++----------- pkg/util/time.go | 15 +++++++++ 6 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 pkg/util/time.go diff --git a/pkg/client/client.go b/pkg/client/client.go index a3b457f..6303486 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -34,14 +34,14 @@ import ( "github.com/tencentyun/tencentcloud-exporter/pkg/config" ) -func NewMonitorClient(cred common.CredentialIface, conf *config.TencentConfig) (*monitor.Client, error) { +func NewMonitorClient(cred common.CredentialIface, conf *config.TencentConfig, region string) (*monitor.Client, error) { cpf := profile.NewClientProfile() if conf.Credential.IsInternal == true { cpf.HttpProfile.Endpoint = "monitor.internal.tencentcloudapi.com" } else { cpf.HttpProfile.Endpoint = "monitor.tencentcloudapi.com" } - return monitor.NewClient(cred, conf.Credential.Region, cpf) + return monitor.NewClient(cred, region, cpf) } func NewMongodbClient(cred common.CredentialIface, conf *config.TencentConfig) (*mongodb.Client, error) { @@ -258,15 +258,20 @@ func NewCosClient(cred common.CredentialIface, conf *config.TencentConfig) (*cos // 用于Get Service 查询, service域名暂时只支持外网 su, _ := url.Parse("http://cos." + conf.Credential.Region + ".myqcloud.com") b := &cos.BaseURL{BucketURL: nil, ServiceURL: su} - // client := cos.NewClient(b, &http.Client{ - // Transport: &cos.AuthorizationTransport{ - // SecretID: conf.Credential.AccessKey, - // SecretKey: conf.Credential.SecretKey, - // }, - // }) - client := cos.NewClient(b, &http.Client{ - Transport: common.NewCredentialTransport(cred.GetRole()), - }) + client := &cos.Client{} + if conf.Credential.Role == "" { + client = cos.NewClient(b, &http.Client{ + Transport: &cos.AuthorizationTransport{ + SecretID: conf.Credential.AccessKey, + SecretKey: conf.Credential.SecretKey, + }, + }) + } else { + client = cos.NewClient(b, &http.Client{ + Transport: common.NewCredentialTransport(cred.GetRole()), + }) + } + return client, nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index 0badafb..9f4847f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -76,6 +76,8 @@ var ( } ) +var QcloudNamespace = []string{"COS", "CDN"} + type TencentCredential struct { AccessKey string `yaml:"access_key"` SecretKey string `yaml:"secret_key"` diff --git a/pkg/instance/instance_clb.go b/pkg/instance/instance_clb.go index 1ecc108..63b3919 100644 --- a/pkg/instance/instance_clb.go +++ b/pkg/instance/instance_clb.go @@ -15,6 +15,8 @@ type ClbInstance struct { func (ins *ClbInstance) GetMonitorQueryKey() string { if len(ins.meta.LoadBalancerVips) == 1 { return *ins.meta.LoadBalancerVips[0] + } else if *ins.meta.AddressIPv6 != "" { + return *ins.meta.AddressIPv6 } else { return "" } diff --git a/pkg/instance/repository_cos.go b/pkg/instance/repository_cos.go index be81c00..3ac554c 100644 --- a/pkg/instance/repository_cos.go +++ b/pkg/instance/repository_cos.go @@ -18,6 +18,7 @@ func init() { } type CosTcInstanceRepository struct { + region string client *sdk.Client logger log.Logger } @@ -62,6 +63,11 @@ func (repo *CosTcInstanceRepository) ListByFilters(filters map[string]string) (i return } for _, meta := range resp.Buckets { + // when region is ap-guangzhou, will get all buckets in every region. + // need to filter by region + if meta.Region != repo.region { + continue + } ins, e := NewCosTcInstance(meta.Name, &meta) if e != nil { 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 return } repo = &CosTcInstanceRepository{ + region: c.Credential.Region, client: cli, logger: logger, } diff --git a/pkg/metric/repository.go b/pkg/metric/repository.go index 0a68bcb..7a4dd88 100644 --- a/pkg/metric/repository.go +++ b/pkg/metric/repository.go @@ -5,6 +5,8 @@ import ( "fmt" "time" + "github.com/tencentyun/tencentcloud-exporter/pkg/util" + "github.com/tencentyun/tencentcloud-exporter/pkg/common" "github.com/go-kit/log" @@ -12,6 +14,7 @@ import ( "golang.org/x/time/rate" monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" + v20180724 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" "github.com/tencentyun/tencentcloud-exporter/pkg/client" "github.com/tencentyun/tencentcloud-exporter/pkg/config" ) @@ -33,10 +36,11 @@ type TcmMetricRepository interface { } type TcmMetricRepositoryImpl struct { - credential common.CredentialIface - monitorClient *monitor.Client - limiter *rate.Limiter // 限速 - ctx context.Context + credential common.CredentialIface + monitorClient *monitor.Client + monitorClientInGuangzhou *monitor.Client + limiter *rate.Limiter // 限速 + ctx context.Context queryMetricBatchSize int @@ -121,14 +125,20 @@ func (repo *TcmMetricRepositoryImpl) GetSamples(s *TcmSeries, st int64, et int64 } request.Instances = []*monitor.Instance{instanceFilters} - stStr := time.Unix(st, 0).Format(timeStampFormat) + stStr := util.FormatTime(time.Unix(st, 0), timeStampFormat) request.StartTime = &stStr if et != 0 { - etStr := time.Unix(et, 0).Format(timeStampFormat) - request.StartTime = &etStr + etStr := util.FormatTime(time.Unix(et, 0), timeStampFormat) + request.EndTime = &etStr } - response, err := repo.monitorClient.GetMonitorData(request) + response := &v20180724.GetMonitorDataResponse{} + if util.IsStrInList(config.QcloudNamespace, s.Metric.Meta.ProductName) { + response, err = repo.monitorClientInGuangzhou.GetMonitorData(request) + } else { + response, err = repo.monitorClient.GetMonitorData(request) + } + level.Info(repo.logger).Log("reqid",response.Response.RequestId) if err != nil { return } @@ -174,7 +184,14 @@ func (repo *TcmMetricRepositoryImpl) listSampleByBatch( } request := repo.buildGetMonitorDataRequest(m, seriesList, st, et) - response, err := repo.monitorClient.GetMonitorData(request) + + response := &v20180724.GetMonitorDataResponse{} + if util.IsStrInList(config.QcloudNamespace, m.Meta.ProductName) { + response, err = repo.monitorClientInGuangzhou.GetMonitorData(request) + } else { + response, err = repo.monitorClient.GetMonitorData(request) + } + level.Info(repo.logger).Log("reqid",response.Response.RequestId) if err != nil { return nil, err } @@ -217,11 +234,11 @@ func (repo *TcmMetricRepositoryImpl) buildGetMonitorDataRequest( request.Instances = append(request.Instances, ifilters) } - stStr := time.Unix(st, 0).Format(timeStampFormat) + stStr := util.FormatTime(time.Unix(st, 0), timeStampFormat) request.StartTime = &stStr if et != 0 { - etStr := time.Unix(et, 0).Format(timeStampFormat) - request.StartTime = &etStr + etStr := util.FormatTime(time.Unix(et, 0), timeStampFormat) + request.EndTime = &etStr } return request } @@ -259,18 +276,23 @@ func (repo *TcmMetricRepositoryImpl) buildSamples( } func NewTcmMetricRepository(cred common.CredentialIface, conf *config.TencentConfig, logger log.Logger) (repo TcmMetricRepository, err error) { - monitorClient, err := client.NewMonitorClient(cred, conf) + monitorClient, err := client.NewMonitorClient(cred, conf, conf.Credential.Region) + if err != nil { + return + } + monitorClientInGuangzhou, err := client.NewMonitorClient(cred, conf, "ap-guangzhou") if err != nil { return } repo = &TcmMetricRepositoryImpl{ - credential: cred, - monitorClient: monitorClient, - limiter: rate.NewLimiter(rate.Limit(conf.RateLimit), 1), - ctx: context.Background(), - queryMetricBatchSize: conf.MetricQueryBatchSize, - logger: logger, + credential: cred, + monitorClient: monitorClient, + monitorClientInGuangzhou: monitorClientInGuangzhou, + limiter: rate.NewLimiter(rate.Limit(conf.RateLimit), 1), + ctx: context.Background(), + queryMetricBatchSize: conf.MetricQueryBatchSize, + logger: logger, } return diff --git a/pkg/util/time.go b/pkg/util/time.go new file mode 100644 index 0000000..6c7b31f --- /dev/null +++ b/pkg/util/time.go @@ -0,0 +1,15 @@ +package util + +import "time" + +// FormatTime formats time by zone. +func FormatTime(t time.Time, format string) string { + var local time.Time + _, offset := t.Zone() + if offset == 0 { + local = t.Add(8 * time.Hour) + } else { + local = t + } + return local.Format(format) +} From 2353fa5b5bd9c085fae362871e437d7383cf816b Mon Sep 17 00:00:00 2001 From: zianazhao Date: Tue, 18 Oct 2022 11:57:12 +0800 Subject: [PATCH 2/2] lb-ipv6 --- pkg/instance/repository_clb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/instance/repository_clb.go b/pkg/instance/repository_clb.go index 10cdaae..52db153 100644 --- a/pkg/instance/repository_clb.go +++ b/pkg/instance/repository_clb.go @@ -88,7 +88,7 @@ getMoreInstances: level.Error(repo.logger).Log("msg", "Create clb instance fail", "id", *meta.LoadBalancerId) continue } - if meta.LoadBalancerVips == nil || len(meta.LoadBalancerVips) == 0 { + if (meta.LoadBalancerVips == nil || len(meta.LoadBalancerVips) == 0) && meta.AddressIPv6 == nil { level.Warn(repo.logger).Log("msg", "clb instance no include vip", "id", *meta.LoadBalancerId) continue }