From d1cf3a6d9dfa4f7b3a8b243c6ec0332e2db3c4bd Mon Sep 17 00:00:00 2001 From: zianazhao Date: Wed, 23 Nov 2022 20:03:03 +0800 Subject: [PATCH 1/8] =?UTF-8?q?qaap-=E5=86=85=E9=83=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/client/client.go | 13 ++++++ pkg/collector/handler_qaap.go | 11 ++++- pkg/instance/repository_qaap.go | 79 +++++++++++++++++++++++++++++++++ pkg/metric/repository.go | 2 +- 4 files changed, 103 insertions(+), 2 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 0c653d5..857f457 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -1,6 +1,7 @@ package client import ( + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions" "net/http" "net/url" @@ -10,6 +11,7 @@ import ( kafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819" clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" cmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cmq/v20190304" + apiCommon "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" @@ -305,3 +307,14 @@ func NewGAAPClient(cred common.CredentialIface, conf *config.TencentConfig) (*ga } return gaap.NewClient(cred, conf.Credential.Region, cpf) } + +func NewGAAPCommonClient(cred common.CredentialIface, conf *config.TencentConfig) *apiCommon.Client { + cpf := profile.NewClientProfile() + if conf.Credential.IsInternal == true { + cpf.HttpProfile.Endpoint = "gaap.internal.tencentcloudapi.com" + } else { + cpf.HttpProfile.Endpoint = "gaap.tencentcloudapi.com" + } + cpf.HttpProfile.ReqMethod = "POST" + return apiCommon.NewCommonClient(cred, regions.Guangzhou, cpf) +} diff --git a/pkg/collector/handler_qaap.go b/pkg/collector/handler_qaap.go index be6230e..e3989b4 100644 --- a/pkg/collector/handler_qaap.go +++ b/pkg/collector/handler_qaap.go @@ -24,6 +24,7 @@ type QaapHandler struct { baseProductHandler tcpListenersRepo instance.QaapTcInstanceTCPListenersRepository udpListenersRepo instance.QaapTcInstanceUDPListenersRepository + commonQaap instance.CommonQaapTcInstanceRepository } func (h *QaapHandler) IsMetricMetaVaild(meta *metric.TcmMeta) bool { @@ -147,7 +148,11 @@ func (h *QaapHandler) getSeriesByMetricType(m *metric.TcmMetric, ins instance.Tc func (h *QaapHandler) getInstanceSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { var series []*metric.TcmSeries - + commoninfo, err := h.commonQaap.GetCommonQaapInfo(ins.GetInstanceId()) + if err != nil { + return nil, err + } + level.Info(h.logger).Log("commoninfo", commoninfo) ql := map[string]string{ h.monitorQueryKey: ins.GetMonitorQueryKey(), } @@ -162,6 +167,7 @@ func (h *QaapHandler) getInstanceSeries(m *metric.TcmMetric, ins instance.TcInst func (h *QaapHandler) getListenerIdSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { var series []*metric.TcmSeries + tcpListenersInfos, err := h.tcpListenersRepo.GetTCPListenersInfo(ins.GetInstanceId()) if err != nil { return nil, err @@ -219,6 +225,8 @@ func NewQaapHandler(cred common.CredentialIface, c *TcProductCollector, logger l } udpListenersRepoCache := instance.NewTcGaapInstanceeUDPListenersCache(udpListenersRepo, relodInterval, logger) + commonQaap, err := instance.NewCommonQaapTcInstanceRepository(cred, c.Conf, logger) + handler = &QaapHandler{ baseProductHandler: baseProductHandler{ monitorQueryKey: QaapInstanceidKey, @@ -227,6 +235,7 @@ func NewQaapHandler(cred common.CredentialIface, c *TcProductCollector, logger l }, tcpListenersRepo: tcpListenersRepoCache, udpListenersRepo: udpListenersRepoCache, + commonQaap: commonQaap, } return diff --git a/pkg/instance/repository_qaap.go b/pkg/instance/repository_qaap.go index 7de54b9..5c9a606 100644 --- a/pkg/instance/repository_qaap.go +++ b/pkg/instance/repository_qaap.go @@ -1,9 +1,11 @@ package instance import ( + "encoding/json" "fmt" "github.com/go-kit/log" "github.com/go-kit/log/level" + tchttp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http" sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap/v20180529" selfcommon "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" @@ -156,3 +158,80 @@ func NewQaapTcInstanceUDPListenersRepository(cred common.CredentialIface, c *con } return repo, nil } + +// 内部接口 + +type ProxyInstancesRsp struct { + TotalCount int64 + ProxySet []ProxyDetail +} +type ProxyDetail struct { + ProxyId string + ProxyName string + L4ListenerSet []L4ListenerDetail + L7ListenerSet []L7ListenerDetail +} +type L4ListenerDetail struct { + ListenerId string + ListenerName string + Protocol string + RsSet []BoundRsDetail +} +type BoundRsDetail struct { + RsId string + RsInfo string +} +type L7ListenerDetail struct { + ListenerId string + ListenerName string + ForwardProtocol string + RuleSet []RuleDetail +} +type RuleDetail struct { + RsSet []BoundRsDetail + RuleId string +} + +type CommonQaapTcInstanceRepository interface { + GetCommonQaapInfo(instanceId string) (ProxyInstancesRsp, error) +} + +type CommonQaapTcInstanceRepositoryImpl struct { + client *selfcommon.Client + logger log.Logger +} + +func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapInfo(instanceId string) (ProxyInstancesRsp, error) { + var proxyInstancesRsp ProxyInstancesRsp + request := tchttp.NewCommonRequest("gaap", "2018-05-29", "DescribeProxyInstances") + body:=map[string]interface{}{ + "Limit":1, + "Offset":0, + "ProxyIds":[]string{"link-2r1whx05"}, + } + // 设置action所需的请求数据 + err := request.SetActionParameters(body) + if err != nil { + return proxyInstancesRsp, err + } + // 创建common response + response := tchttp.NewCommonResponse() + // 发送请求 + err = repo.client.Send(request, response) + if err != nil { + fmt.Printf("fail to invoke api: %v \n", err) + } + // 获取响应结果 + fmt.Println(string(response.GetBody())) + json.Unmarshal(response.GetBody(), &proxyInstancesRsp) + return proxyInstancesRsp, nil +} + +func NewCommonQaapTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (CommonQaapTcInstanceRepository, error) { + cli := client.NewGAAPCommonClient(cred, c) + repo := &CommonQaapTcInstanceRepositoryImpl{ + client: cli, + logger: logger, + } + return repo, nil +} diff --git a/pkg/metric/repository.go b/pkg/metric/repository.go index 052f3ee..87a8383 100644 --- a/pkg/metric/repository.go +++ b/pkg/metric/repository.go @@ -193,7 +193,7 @@ func (repo *TcmMetricRepositoryImpl) listSampleByBatch( if err != nil { return nil, err } - + level.Info(repo.logger).Log("reqid:",response.Response.RequestId) for _, points := range response.Response.DataPoints { samples, ql, e := repo.buildSamples(m, points) if e != nil { From 03673cdb98f31e85d5f92b0846ce11aa376abd4e Mon Sep 17 00:00:00 2001 From: zianazhao Date: Thu, 24 Nov 2022 16:06:43 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=86=85=E9=83=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/collector/handler_qaap.go | 12 ++++---- pkg/instance/repository_qaap.go | 49 +++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/pkg/collector/handler_qaap.go b/pkg/collector/handler_qaap.go index e3989b4..69a4c58 100644 --- a/pkg/collector/handler_qaap.go +++ b/pkg/collector/handler_qaap.go @@ -51,6 +51,13 @@ func (h *QaapHandler) IsMetricVaild(m *metric.TcmMetric) bool { } func (h *QaapHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { + noneBgpIpListRsp, err := h.commonQaap.GetCommonQaapNoneBgpIpList("") + proxyInstancesRsp, err := h.commonQaap.GetCommonQaapProxyInstances("") + if err != nil { + return nil, err + } + level.Info(h.logger).Log("noneBgpIpListRsp count:",noneBgpIpListRsp.Response.TotalCount) + level.Info(h.logger).Log("proxyInstancesRsp count:",proxyInstancesRsp.Response.TotalCount) if m.Conf.IsIncludeOnlyInstance() { return h.GetSeriesByOnly(m) } @@ -148,11 +155,6 @@ func (h *QaapHandler) getSeriesByMetricType(m *metric.TcmMetric, ins instance.Tc func (h *QaapHandler) getInstanceSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { var series []*metric.TcmSeries - commoninfo, err := h.commonQaap.GetCommonQaapInfo(ins.GetInstanceId()) - if err != nil { - return nil, err - } - level.Info(h.logger).Log("commoninfo", commoninfo) ql := map[string]string{ h.monitorQueryKey: ins.GetMonitorQueryKey(), } diff --git a/pkg/instance/repository_qaap.go b/pkg/instance/repository_qaap.go index 5c9a606..b4c3a58 100644 --- a/pkg/instance/repository_qaap.go +++ b/pkg/instance/repository_qaap.go @@ -161,10 +161,13 @@ func NewQaapTcInstanceUDPListenersRepository(cred common.CredentialIface, c *con // 内部接口 -type ProxyInstancesRsp struct { +type Rsp struct { TotalCount int64 ProxySet []ProxyDetail } +type ProxyInstancesRsp struct { + Response Rsp +} type ProxyDetail struct { ProxyId string ProxyName string @@ -191,9 +194,12 @@ type RuleDetail struct { RsSet []BoundRsDetail RuleId string } - +type NoneBgpIpListRsp struct { + Response Rsp +} type CommonQaapTcInstanceRepository interface { - GetCommonQaapInfo(instanceId string) (ProxyInstancesRsp, error) + GetCommonQaapProxyInstances(instanceId string) (ProxyInstancesRsp, error) + GetCommonQaapNoneBgpIpList(instanceId string) (NoneBgpIpListRsp, error) } type CommonQaapTcInstanceRepositoryImpl struct { @@ -201,13 +207,13 @@ type CommonQaapTcInstanceRepositoryImpl struct { logger log.Logger } -func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapInfo(instanceId string) (ProxyInstancesRsp, error) { +func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapProxyInstances(instanceId string) (ProxyInstancesRsp, error) { var proxyInstancesRsp ProxyInstancesRsp request := tchttp.NewCommonRequest("gaap", "2018-05-29", "DescribeProxyInstances") - body:=map[string]interface{}{ - "Limit":1, - "Offset":0, - "ProxyIds":[]string{"link-2r1whx05"}, + body := map[string]interface{}{ + "Limit": 1, + "Offset": 0, + "ProxyIds": []string{"link-2r1whx05"}, } // 设置action所需的请求数据 err := request.SetActionParameters(body) @@ -222,11 +228,36 @@ func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapInfo(instanceId str fmt.Printf("fail to invoke api: %v \n", err) } // 获取响应结果 - fmt.Println(string(response.GetBody())) + // fmt.Println(string(response.GetBody())) json.Unmarshal(response.GetBody(), &proxyInstancesRsp) return proxyInstancesRsp, nil } +func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapNoneBgpIpList(instanceId string) (NoneBgpIpListRsp, error) { + var noneBgpIpListRsp NoneBgpIpListRsp + request := tchttp.NewCommonRequest("gaap", "2018-05-29", "DescribeNoneBgpIpList") + body := map[string]interface{}{ + "Limit": 1, + "Offset": 0, + } + // 设置action所需的请求数据 + err := request.SetActionParameters(body) + if err != nil { + return noneBgpIpListRsp, err + } + // 创建common response + response := tchttp.NewCommonResponse() + // 发送请求 + err = repo.client.Send(request, response) + if err != nil { + fmt.Printf("fail to invoke api: %v \n", err) + } + // 获取响应结果 + // fmt.Println(string(response.GetBody())) + json.Unmarshal(response.GetBody(), &noneBgpIpListRsp) + return noneBgpIpListRsp, nil +} + func NewCommonQaapTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (CommonQaapTcInstanceRepository, error) { cli := client.NewGAAPCommonClient(cred, c) repo := &CommonQaapTcInstanceRepositoryImpl{ From b2ec4a5f3081d49c8f0027a8577729103d5aed5e Mon Sep 17 00:00:00 2001 From: zianazhao Date: Fri, 25 Nov 2022 15:01:19 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=9B=BD=E9=99=85=E7=AB=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/collector/handler_qaap.go | 8 ++++++-- pkg/config/config.go | 1 + pkg/metric/repository.go | 23 +++++++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pkg/collector/handler_qaap.go b/pkg/collector/handler_qaap.go index 69a4c58..31018d7 100644 --- a/pkg/collector/handler_qaap.go +++ b/pkg/collector/handler_qaap.go @@ -56,8 +56,12 @@ func (h *QaapHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error if err != nil { return nil, err } - level.Info(h.logger).Log("noneBgpIpListRsp count:",noneBgpIpListRsp.Response.TotalCount) - level.Info(h.logger).Log("proxyInstancesRsp count:",proxyInstancesRsp.Response.TotalCount) + level.Info(h.logger).Log("noneBgpIpListRsp count:", noneBgpIpListRsp.Response.TotalCount) + level.Info(h.logger).Log("proxyInstancesRsp count:", proxyInstancesRsp.Response.TotalCount) + if proxyInstancesRsp.Response.TotalCount != 0 { + level.Info(h.logger).Log("proxyInstancesRsp ProxyId:", proxyInstancesRsp.Response.ProxySet[0].ProxyId) + } + if m.Conf.IsIncludeOnlyInstance() { return h.GetSeriesByOnly(m) } diff --git a/pkg/config/config.go b/pkg/config/config.go index ae4c976..f00fa68 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -145,6 +145,7 @@ type TencentConfig struct { MetricQueryBatchSize int `yaml:"metric_query_batch_size"` Filename string `yaml:"filename"` CacheInterval int64 `yaml:"cache_interval"` // 单位 s + IsInternational bool `yaml:"is_international"` // true 表示是国际站 } func NewConfig() *TencentConfig { diff --git a/pkg/metric/repository.go b/pkg/metric/repository.go index 87a8383..b60bc1d 100644 --- a/pkg/metric/repository.go +++ b/pkg/metric/repository.go @@ -39,8 +39,10 @@ type TcmMetricRepositoryImpl struct { credential common.CredentialIface monitorClient *monitor.Client monitorClientInGuangzhou *monitor.Client + monitorClientInSinapore *monitor.Client limiter *rate.Limiter // 限速 ctx context.Context + IsInternational bool queryMetricBatchSize int @@ -133,7 +135,9 @@ func (repo *TcmMetricRepositoryImpl) GetSamples(s *TcmSeries, st int64, et int64 } response := &v20180724.GetMonitorDataResponse{} - if util.IsStrInList(config.QcloudNamespace, s.Metric.Meta.ProductName) { + if repo.IsInternational && s.Metric.Meta.ProductName == "QAAP" { + response, err = repo.monitorClientInSinapore.GetMonitorData(request) + } else if util.IsStrInList(config.QcloudNamespace, s.Metric.Meta.ProductName) { response, err = repo.monitorClientInGuangzhou.GetMonitorData(request) } else { response, err = repo.monitorClient.GetMonitorData(request) @@ -185,15 +189,15 @@ func (repo *TcmMetricRepositoryImpl) listSampleByBatch( request := repo.buildGetMonitorDataRequest(m, seriesList, st, et) response := &v20180724.GetMonitorDataResponse{} - if util.IsStrInList(config.QcloudNamespace, m.Meta.ProductName) { - response, err = repo.monitorClientInGuangzhou.GetMonitorData(request) - } else { + if repo.IsInternational && m.Meta.ProductName == "QAAP" { + response, err = repo.monitorClientInSinapore.GetMonitorData(request) + } else if util.IsStrInList(config.QcloudNamespace, m.Meta.ProductName) { response, err = repo.monitorClient.GetMonitorData(request) } if err != nil { return nil, err } - level.Info(repo.logger).Log("reqid:",response.Response.RequestId) + level.Info(repo.logger).Log("reqid:", response.Response.RequestId) for _, points := range response.Response.DataPoints { samples, ql, e := repo.buildSamples(m, points) if e != nil { @@ -282,13 +286,20 @@ func NewTcmMetricRepository(cred common.CredentialIface, conf *config.TencentCon if err != nil { return } - + var monitorClientInSingapore *monitor.Client + if conf.IsInternational { + if monitorClientInSingapore, err = client.NewMonitorClient(cred, conf, "ap-singapore"); err != nil { + return + } + } repo = &TcmMetricRepositoryImpl{ credential: cred, monitorClient: monitorClient, monitorClientInGuangzhou: monitorClientInGuangzhou, + monitorClientInSinapore: monitorClientInSingapore, limiter: rate.NewLimiter(rate.Limit(conf.RateLimit), 1), ctx: context.Background(), + IsInternational: conf.IsInternational, queryMetricBatchSize: conf.MetricQueryBatchSize, logger: logger, } From 98bb0cb2a656196db0d7e13a512836896657ded3 Mon Sep 17 00:00:00 2001 From: zianazhao Date: Tue, 29 Nov 2022 20:16:21 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E5=86=85=E9=83=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/collector/handler_qaap.go | 43 +++++++++++++++++++++++++++++++-- pkg/instance/repository_qaap.go | 37 +++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/pkg/collector/handler_qaap.go b/pkg/collector/handler_qaap.go index 31018d7..c7f0c5c 100644 --- a/pkg/collector/handler_qaap.go +++ b/pkg/collector/handler_qaap.go @@ -16,6 +16,24 @@ const ( QaapInstanceidKey = "channelId" ) +var ( + QaapDetail2GroupidMetricNames = []string{ + "GroupInFlow", "GroupOutFlow", "GroupInbandwidth", "GroupOutbandwidth", + } + QaapIpDetailMetricNames = []string{ + "IpConnum", "IpInbandwidth", "IpInpacket", "IpLatency", "IpOutbandwidth", "IpOutpacket", "IpPacketLoss", + } + QaapListenerStatMetricNames = []string{ + "ListenerConnum", "ListenerOutbandwidth", "ListenerInpacket", "ListenerOutpacket", "ListenerInbandwidth", + } + QaapListenerRsMetricNames = []string{ + "ListenerRsStatus", + } + QaapRuleRsMetricNames = []string{ + "RuleRsStatus", "RuleRsStatusTest", + } +) + func init() { registerHandler(QaapNamespace, defaultHandlerEnabled, NewQaapHandler) } @@ -149,8 +167,15 @@ func (h *QaapHandler) getSeriesByMetricType(m *metric.TcmMetric, ins instance.Tc for _, v := range m.Meta.SupportDimensions { dimensions = append(dimensions, v) } - - if util.IsStrInList(dimensions, "listenerId") { + if util.IsStrInList(QaapDetail2GroupidMetricNames, m.Meta.MetricName) { + return h.getQaapDetail2GroupidSeries(m, ins) + } else if util.IsStrInList(QaapIpDetailMetricNames, m.Meta.MetricName) { + return h.getListenerIdSeries(m, ins) + } else if util.IsStrInList(QaapListenerStatMetricNames, m.Meta.MetricName) { + return h.getListenerIdSeries(m, ins) + } else if util.IsStrInList(QaapListenerRsMetricNames, m.Meta.MetricName) { + return h.getListenerIdSeries(m, ins) + } else if util.IsStrInList(QaapRuleRsMetricNames, m.Meta.MetricName) { return h.getListenerIdSeries(m, ins) } else { return h.getInstanceSeries(m, ins) @@ -217,6 +242,20 @@ func (h *QaapHandler) getListenerIdSeries(m *metric.TcmMetric, ins instance.TcIn return series, nil } +func (h *QaapHandler) getQaapDetail2GroupidSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { + var series []*metric.TcmSeries + ql := map[string]string{ + "GroupId": ins.GetMonitorQueryKey(), + } + s, err := metric.NewTcmSeries(m, ql, ins) + if err != nil { + return nil, err + } + series = append(series, s) + + return series, nil +} + func NewQaapHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { tcpListenersRepo, err := instance.NewQaapTcInstanceTCPListenersRepository(cred, c.Conf, logger) if err != nil { diff --git a/pkg/instance/repository_qaap.go b/pkg/instance/repository_qaap.go index b4c3a58..d6b33b7 100644 --- a/pkg/instance/repository_qaap.go +++ b/pkg/instance/repository_qaap.go @@ -159,8 +159,39 @@ func NewQaapTcInstanceUDPListenersRepository(cred common.CredentialIface, c *con return repo, nil } -// 内部接口 +// DescribeProxyGroupList +type QaapTcInstanceProxyGroupListRepository interface { + GetProxyGroupList(instanceId string) (*sdk.DescribeProxyGroupListResponse, error) +} + +type QaapTcInstanceProxyGroupListRepositoryImpl struct { + client *sdk.Client + logger log.Logger +} + +func (repo *QaapTcInstanceProxyGroupListRepositoryImpl) GetProxyGroupList(instanceId string) (*sdk.DescribeProxyGroupListResponse, error) { + req := sdk.NewDescribeProxyGroupListRequest() + var offset int64 = 0 + var limit int64 = 100 + req.Limit = &limit + req.Offset = &offset + // req. = selfcommon.StringPtr(instanceId) + return repo.client.DescribeProxyGroupList(req) +} + +func NewQaapTcInstanceProxyGroupListRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (QaapTcInstanceUDPListenersRepository, error) { + cli, err := client.NewGAAPClient(cred, c) + if err != nil { + return nil, err + } + repo := &QaapTcInstanceUDPListenersRepositoryImpl{ + client: cli, + logger: logger, + } + return repo, nil +} +// 内部接口 type Rsp struct { TotalCount int64 ProxySet []ProxyDetail @@ -237,8 +268,8 @@ func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapNoneBgpIpList(insta var noneBgpIpListRsp NoneBgpIpListRsp request := tchttp.NewCommonRequest("gaap", "2018-05-29", "DescribeNoneBgpIpList") body := map[string]interface{}{ - "Limit": 1, - "Offset": 0, + "Limit": 1, + "Offset": 0, } // 设置action所需的请求数据 err := request.SetActionParameters(body) From 344ef9e76ea9bb60924c6be775d47904009276ab Mon Sep 17 00:00:00 2001 From: zianazhao Date: Fri, 2 Dec 2022 17:53:07 +0800 Subject: [PATCH 5/8] gaap --- pkg/collector/handler_qaap.go | 150 ++++++++++++++++++++++++-------- pkg/instance/cache.go | 116 ++++++++++++++++++------ pkg/instance/repository_qaap.go | 112 +++++++++--------------- 3 files changed, 241 insertions(+), 137 deletions(-) diff --git a/pkg/collector/handler_qaap.go b/pkg/collector/handler_qaap.go index c7f0c5c..40d7cc3 100644 --- a/pkg/collector/handler_qaap.go +++ b/pkg/collector/handler_qaap.go @@ -8,6 +8,7 @@ import ( "github.com/tencentyun/tencentcloud-exporter/pkg/instance" "github.com/tencentyun/tencentcloud-exporter/pkg/metric" "github.com/tencentyun/tencentcloud-exporter/pkg/util" + "strings" "time" ) @@ -30,7 +31,7 @@ var ( "ListenerRsStatus", } QaapRuleRsMetricNames = []string{ - "RuleRsStatus", "RuleRsStatusTest", + "RuleRsStatus", } ) @@ -40,9 +41,8 @@ func init() { type QaapHandler struct { baseProductHandler - tcpListenersRepo instance.QaapTcInstanceTCPListenersRepository - udpListenersRepo instance.QaapTcInstanceUDPListenersRepository - commonQaap instance.CommonQaapTcInstanceRepository + commonQaapInstanceInfoRepo instance.CommonQaapTcInstanceRepository + qaapInstanceInfoRepo instance.QaapTcInstanceInfoRepository } func (h *QaapHandler) IsMetricMetaVaild(meta *metric.TcmMeta) bool { @@ -69,17 +69,6 @@ func (h *QaapHandler) IsMetricVaild(m *metric.TcmMetric) bool { } func (h *QaapHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { - noneBgpIpListRsp, err := h.commonQaap.GetCommonQaapNoneBgpIpList("") - proxyInstancesRsp, err := h.commonQaap.GetCommonQaapProxyInstances("") - if err != nil { - return nil, err - } - level.Info(h.logger).Log("noneBgpIpListRsp count:", noneBgpIpListRsp.Response.TotalCount) - level.Info(h.logger).Log("proxyInstancesRsp count:", proxyInstancesRsp.Response.TotalCount) - if proxyInstancesRsp.Response.TotalCount != 0 { - level.Info(h.logger).Log("proxyInstancesRsp ProxyId:", proxyInstancesRsp.Response.ProxySet[0].ProxyId) - } - if m.Conf.IsIncludeOnlyInstance() { return h.GetSeriesByOnly(m) } @@ -170,13 +159,13 @@ func (h *QaapHandler) getSeriesByMetricType(m *metric.TcmMetric, ins instance.Tc if util.IsStrInList(QaapDetail2GroupidMetricNames, m.Meta.MetricName) { return h.getQaapDetail2GroupidSeries(m, ins) } else if util.IsStrInList(QaapIpDetailMetricNames, m.Meta.MetricName) { - return h.getListenerIdSeries(m, ins) + return h.getQaapIpDetailSeries(m, ins) } else if util.IsStrInList(QaapListenerStatMetricNames, m.Meta.MetricName) { - return h.getListenerIdSeries(m, ins) + return h.getQaapListenerStatSeries(m, ins) } else if util.IsStrInList(QaapListenerRsMetricNames, m.Meta.MetricName) { - return h.getListenerIdSeries(m, ins) + return h.getQaapListenerRsSeries(m, ins) } else if util.IsStrInList(QaapRuleRsMetricNames, m.Meta.MetricName) { - return h.getListenerIdSeries(m, ins) + return h.getRuleRsSeries(m, ins) } else { return h.getInstanceSeries(m, ins) } @@ -196,10 +185,10 @@ func (h *QaapHandler) getInstanceSeries(m *metric.TcmMetric, ins instance.TcInst return series, nil } -func (h *QaapHandler) getListenerIdSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { +func (h *QaapHandler) getQaapListenerRsSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { var series []*metric.TcmSeries - tcpListenersInfos, err := h.tcpListenersRepo.GetTCPListenersInfo(ins.GetInstanceId()) + tcpListenersInfos, err := h.qaapInstanceInfoRepo.GetTCPListenersInfo(ins.GetInstanceId()) if err != nil { return nil, err } @@ -219,7 +208,7 @@ func (h *QaapHandler) getListenerIdSeries(m *metric.TcmMetric, ins instance.TcIn series = append(series, s) } } - udpListenersInfos, err := h.udpListenersRepo.GetUDPListenersInfo(ins.GetInstanceId()) + udpListenersInfos, err := h.qaapInstanceInfoRepo.GetUDPListenersInfo(ins.GetInstanceId()) if err != nil { return nil, err } @@ -244,33 +233,123 @@ func (h *QaapHandler) getListenerIdSeries(m *metric.TcmMetric, ins instance.TcIn func (h *QaapHandler) getQaapDetail2GroupidSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { var series []*metric.TcmSeries - ql := map[string]string{ - "GroupId": ins.GetMonitorQueryKey(), + proxyGroupLists, err := h.qaapInstanceInfoRepo.GetProxyGroupList(ins.GetInstanceId()) + if err != nil { + return nil, err } - s, err := metric.NewTcmSeries(m, ql, ins) + for _, proxyGroupList := range proxyGroupLists.Response.ProxyGroupList { + ql := map[string]string{ + "GroupId": *proxyGroupList.GroupId, + } + s, err := metric.NewTcmSeries(m, ql, ins) + if err != nil { + return nil, err + } + series = append(series, s) + } + return series, nil +} + +func (h *QaapHandler) getQaapIpDetailSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { + var series []*metric.TcmSeries + noneBgpIpLists, err := h.commonQaapInstanceInfoRepo.GetCommonQaapNoneBgpIpList(ins.GetInstanceId()) if err != nil { return nil, err } - series = append(series, s) + for _, instanceSet := range noneBgpIpLists.Response.InstanceSet { + ql := map[string]string{ + "ip": instanceSet.IP, + "proxyid": instanceSet.ProxyId, + "groupid": instanceSet.GroupId, + "isp": strings.ToLower(instanceSet.Isp), + } + s, err := metric.NewTcmSeries(m, ql, ins) + if err != nil { + return nil, err + } + series = append(series, s) + } + return series, nil +} +func (h *QaapHandler) getQaapListenerStatSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { + var series []*metric.TcmSeries + ProxyInstances, err := h.commonQaapInstanceInfoRepo.GetCommonQaapProxyInstances(ins.GetInstanceId()) + if err != nil { + return nil, err + } + for _, proxySet := range ProxyInstances.Response.ProxySet { + for _, l4Listener := range proxySet.L4ListenerSet { + ql := map[string]string{ + "instanceid": proxySet.ProxyId, + "listenerid": l4Listener.ListenerId, + } + s, err := metric.NewTcmSeries(m, ql, ins) + if err != nil { + return nil, err + } + series = append(series, s) + } + for _, l7Listener := range proxySet.L7ListenerSet { + ql := map[string]string{ + "instanceid": proxySet.ProxyId, + "listenerid": l7Listener.ListenerId, + } + s, err := metric.NewTcmSeries(m, ql, ins) + if err != nil { + return nil, err + } + series = append(series, s) + } + + } + return series, nil +} +func (h *QaapHandler) getRuleRsSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { + var series []*metric.TcmSeries + ProxyInstances, err := h.commonQaapInstanceInfoRepo.GetCommonQaapProxyInstances(ins.GetInstanceId()) + if err != nil { + return nil, err + } + for _, proxySet := range ProxyInstances.Response.ProxySet { + for _, l7Listener := range proxySet.L7ListenerSet { + for _, rule := range l7Listener.RuleSet { + for _, rs := range rule.RsInfo { + fmt.Print(proxySet.ProxyId, "-", l7Listener.ListenerId, "") + ql := map[string]string{ + "instanceid": proxySet.ProxyId, + "listenerid": l7Listener.ListenerId, + "ruleid": rule.RuleId, + "rs_ip": rs.RsInfo, + } + s, err := metric.NewTcmSeries(m, ql, ins) + if err != nil { + return nil, err + } + series = append(series, s) + } + + } + + } + + } return series, nil } func NewQaapHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { - tcpListenersRepo, err := instance.NewQaapTcInstanceTCPListenersRepository(cred, c.Conf, logger) + qaapInstanceInfo, err := instance.NewQaapTcInstanceInfoRepository(cred, c.Conf, logger) if err != nil { return nil, err } - relodInterval := time.Duration(c.ProductConf.RelodIntervalMinutes * int64(time.Minute)) - tcpListenersRepoCache := instance.NewTcGaapInstanceeTCPListenersCache(tcpListenersRepo, relodInterval, logger) + reloadInterval := time.Duration(c.ProductConf.RelodIntervalMinutes * int64(time.Minute)) + qaapInstanceInfoCache := instance.NewTcGaapInstanceeInfosCache(qaapInstanceInfo, reloadInterval, logger) - udpListenersRepo, err := instance.NewQaapTcInstanceUDPListenersRepository(cred, c.Conf, logger) + commonQaapInstanceInfoRepo, err := instance.NewCommonQaapTcInstanceRepository(cred, c.Conf, logger) if err != nil { return nil, err } - udpListenersRepoCache := instance.NewTcGaapInstanceeUDPListenersCache(udpListenersRepo, relodInterval, logger) - - commonQaap, err := instance.NewCommonQaapTcInstanceRepository(cred, c.Conf, logger) + commonQaapInstanceInfoCache := instance.NewTcCommonGaapInstanceeInfosCache(commonQaapInstanceInfoRepo, reloadInterval, logger) handler = &QaapHandler{ baseProductHandler: baseProductHandler{ @@ -278,9 +357,8 @@ func NewQaapHandler(cred common.CredentialIface, c *TcProductCollector, logger l collector: c, logger: logger, }, - tcpListenersRepo: tcpListenersRepoCache, - udpListenersRepo: udpListenersRepoCache, - commonQaap: commonQaap, + commonQaapInstanceInfoRepo: commonQaapInstanceInfoCache, + qaapInstanceInfoRepo: qaapInstanceInfoCache, } return diff --git a/pkg/instance/cache.go b/pkg/instance/cache.go index fc36ed4..8887deb 100644 --- a/pkg/instance/cache.go +++ b/pkg/instance/cache.go @@ -528,9 +528,11 @@ func NewVbcTcInstanceDRegionRepositoryCache(repo VbcTcInstanceDRegionRepository, } // gaap -type TcGaapInstanceeTCPListenersCache struct { - Raw QaapTcInstanceTCPListenersRepository - cache map[string]*gaap.DescribeTCPListenersResponse +type TcGaapInstanceeInfosCache struct { + Raw QaapTcInstanceInfoRepository + cache1 map[string]*gaap.DescribeTCPListenersResponse + cache2 map[string]*gaap.DescribeUDPListenersResponse + cache3 map[string]*gaap.DescribeProxyGroupListResponse lastReloadTime map[string]time.Time reloadInterval time.Duration mu sync.Mutex @@ -538,10 +540,10 @@ type TcGaapInstanceeTCPListenersCache struct { logger log.Logger } -func (c *TcGaapInstanceeTCPListenersCache) GetTCPListenersInfo(instanceId string) (*gaap.DescribeTCPListenersResponse, error) { +func (c *TcGaapInstanceeInfosCache) GetTCPListenersInfo(instanceId string) (*gaap.DescribeTCPListenersResponse, error) { lrtime, exists := c.lastReloadTime[instanceId] if exists && time.Now().Sub(lrtime) < c.reloadInterval { - namespace, ok := c.cache[instanceId] + namespace, ok := c.cache1[instanceId] if ok { return namespace, nil } @@ -551,16 +553,54 @@ func (c *TcGaapInstanceeTCPListenersCache) GetTCPListenersInfo(instanceId string if err != nil { return nil, err } - c.cache[instanceId] = tcpListeners + c.cache1[instanceId] = tcpListeners c.lastReloadTime[instanceId] = time.Now() level.Debug(c.logger).Log("msg", "Get gaap Namespaces info from api", "instanceId", instanceId) return tcpListeners, nil } +func (c *TcGaapInstanceeInfosCache) GetUDPListenersInfo(instanceId string) (*gaap.DescribeUDPListenersResponse, error) { + lrtime, exists := c.lastReloadTime[instanceId] + if exists && time.Now().Sub(lrtime) < c.reloadInterval { + namespace, ok := c.cache2[instanceId] + if ok { + return namespace, nil + } + } -func NewTcGaapInstanceeTCPListenersCache(repo QaapTcInstanceTCPListenersRepository, reloadInterval time.Duration, logger log.Logger) QaapTcInstanceTCPListenersRepository { - cache := &TcGaapInstanceeTCPListenersCache{ + udpListeners, err := c.Raw.GetUDPListenersInfo(instanceId) + if err != nil { + return nil, err + } + c.cache2[instanceId] = udpListeners + c.lastReloadTime[instanceId] = time.Now() + level.Debug(c.logger).Log("msg", "Get gaap Namespaces info from api", "instanceId", instanceId) + return udpListeners, nil +} +func (c *TcGaapInstanceeInfosCache) GetProxyGroupList(instanceId string) (*gaap.DescribeProxyGroupListResponse, error) { + lrtime, exists := c.lastReloadTime[instanceId] + if exists && time.Now().Sub(lrtime) < c.reloadInterval { + namespace, ok := c.cache3[instanceId] + if ok { + return namespace, nil + } + } + + proxyGroupList, err := c.Raw.GetProxyGroupList(instanceId) + if err != nil { + return nil, err + } + c.cache3[instanceId] = proxyGroupList + c.lastReloadTime[instanceId] = time.Now() + level.Debug(c.logger).Log("msg", "Get gaap Namespaces info from api", "instanceId", instanceId) + return proxyGroupList, nil +} + +func NewTcGaapInstanceeInfosCache(repo QaapTcInstanceInfoRepository, reloadInterval time.Duration, logger log.Logger) QaapTcInstanceInfoRepository { + cache := &TcGaapInstanceeInfosCache{ Raw: repo, - cache: map[string]*gaap.DescribeTCPListenersResponse{}, + cache1: map[string]*gaap.DescribeTCPListenersResponse{}, + cache2: map[string]*gaap.DescribeUDPListenersResponse{}, + cache3: map[string]*gaap.DescribeProxyGroupListResponse{}, lastReloadTime: map[string]time.Time{}, reloadInterval: reloadInterval, logger: logger, @@ -568,42 +608,62 @@ func NewTcGaapInstanceeTCPListenersCache(repo QaapTcInstanceTCPListenersReposito return cache } -type TcGaapInstanceeUDPListenersCache struct { - Raw QaapTcInstanceUDPListenersRepository - cache map[string]*gaap.DescribeUDPListenersResponse - lastReloadTime map[string]time.Time - reloadInterval time.Duration - mu sync.Mutex +type TcCommonGaapInstanceeInfosCache struct { + Raw CommonQaapTcInstanceRepository + proxyInstancesCache map[string]ProxyInstancesRsp + noneBgpIpListCache map[string]NoneBgpIpListRsp + lastReloadTime map[string]time.Time + reloadInterval time.Duration + mu sync.Mutex logger log.Logger } -func (c *TcGaapInstanceeUDPListenersCache) GetUDPListenersInfo(instanceId string) (*gaap.DescribeUDPListenersResponse, error) { +func (c *TcCommonGaapInstanceeInfosCache) GetCommonQaapProxyInstances(instanceId string) (ProxyInstancesRsp, error) { lrtime, exists := c.lastReloadTime[instanceId] if exists && time.Now().Sub(lrtime) < c.reloadInterval { - namespace, ok := c.cache[instanceId] + namespace, ok := c.proxyInstancesCache[instanceId] if ok { return namespace, nil } } - tcpListeners, err := c.Raw.GetUDPListenersInfo(instanceId) + proxyGroupList, err := c.Raw.GetCommonQaapProxyInstances(instanceId) if err != nil { - return nil, err + return ProxyInstancesRsp{}, err } - c.cache[instanceId] = tcpListeners + c.proxyInstancesCache[instanceId] = proxyGroupList c.lastReloadTime[instanceId] = time.Now() level.Debug(c.logger).Log("msg", "Get gaap Namespaces info from api", "instanceId", instanceId) - return tcpListeners, nil + return proxyGroupList, nil +} +func (c *TcCommonGaapInstanceeInfosCache) GetCommonQaapNoneBgpIpList(instanceId string) (NoneBgpIpListRsp, error) { + lrtime, exists := c.lastReloadTime[instanceId] + if exists && time.Now().Sub(lrtime) < c.reloadInterval { + namespace, ok := c.noneBgpIpListCache[instanceId] + if ok { + return namespace, nil + } + } + + proxyGroupList, err := c.Raw.GetCommonQaapNoneBgpIpList(instanceId) + if err != nil { + return NoneBgpIpListRsp{}, err + } + c.noneBgpIpListCache[instanceId] = proxyGroupList + c.lastReloadTime[instanceId] = time.Now() + level.Debug(c.logger).Log("msg", "Get gaap Namespaces info from api", "instanceId", instanceId) + return proxyGroupList, nil } -func NewTcGaapInstanceeUDPListenersCache(repo QaapTcInstanceUDPListenersRepository, reloadInterval time.Duration, logger log.Logger) QaapTcInstanceUDPListenersRepository { - cache := &TcGaapInstanceeUDPListenersCache{ - Raw: repo, - cache: map[string]*gaap.DescribeUDPListenersResponse{}, - lastReloadTime: map[string]time.Time{}, - reloadInterval: reloadInterval, - logger: logger, +func NewTcCommonGaapInstanceeInfosCache(repo CommonQaapTcInstanceRepository, reloadInterval time.Duration, logger log.Logger) CommonQaapTcInstanceRepository { + cache := &TcCommonGaapInstanceeInfosCache{ + Raw: repo, + proxyInstancesCache: map[string]ProxyInstancesRsp{}, + noneBgpIpListCache: map[string]NoneBgpIpListRsp{}, + lastReloadTime: map[string]time.Time{}, + reloadInterval: reloadInterval, + logger: logger, } return cache } diff --git a/pkg/instance/repository_qaap.go b/pkg/instance/repository_qaap.go index d6b33b7..36ace2e 100644 --- a/pkg/instance/repository_qaap.go +++ b/pkg/instance/repository_qaap.go @@ -95,49 +95,29 @@ func NewQaapTcInstanceRepository(cred common.CredentialIface, c *config.TencentC return } -// TCPListeners -type QaapTcInstanceTCPListenersRepository interface { +// DescribeProxyGroupList +type QaapTcInstanceInfoRepository interface { + GetProxyGroupList(instanceId string) (*sdk.DescribeProxyGroupListResponse, error) + GetUDPListenersInfo(instanceId string) (*sdk.DescribeUDPListenersResponse, error) GetTCPListenersInfo(instanceId string) (*sdk.DescribeTCPListenersResponse, error) } -type QaapTcInstanceTCPListenersRepositoryImpl struct { +type QaapTcInstanceInfoRepositoryImpl struct { client *sdk.Client logger log.Logger } -func (repo *QaapTcInstanceTCPListenersRepositoryImpl) GetTCPListenersInfo(instanceId string) (*sdk.DescribeTCPListenersResponse, error) { - req := sdk.NewDescribeTCPListenersRequest() - var offset uint64 = 0 - var limit uint64 = 100 +func (repo *QaapTcInstanceInfoRepositoryImpl) GetProxyGroupList(instanceId string) (*sdk.DescribeProxyGroupListResponse, error) { + req := sdk.NewDescribeProxyGroupListRequest() + var offset int64 = 0 + var limit int64 = 100 + var projectId int64 = -1 req.Limit = &limit req.Offset = &offset - req.ProxyId = selfcommon.StringPtr(instanceId) - return repo.client.DescribeTCPListeners(req) -} - -func NewQaapTcInstanceTCPListenersRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (QaapTcInstanceTCPListenersRepository, error) { - cli, err := client.NewGAAPClient(cred, c) - if err != nil { - return nil, err - } - repo := &QaapTcInstanceTCPListenersRepositoryImpl{ - client: cli, - logger: logger, - } - return repo, nil -} - -// UDPListeners -type QaapTcInstanceUDPListenersRepository interface { - GetUDPListenersInfo(instanceId string) (*sdk.DescribeUDPListenersResponse, error) -} - -type QaapTcInstanceUDPListenersRepositoryImpl struct { - client *sdk.Client - logger log.Logger + req.ProjectId = &projectId + return repo.client.DescribeProxyGroupList(req) } - -func (repo *QaapTcInstanceUDPListenersRepositoryImpl) GetUDPListenersInfo(instanceId string) (*sdk.DescribeUDPListenersResponse, error) { +func (repo *QaapTcInstanceInfoRepositoryImpl) GetUDPListenersInfo(instanceId string) (*sdk.DescribeUDPListenersResponse, error) { req := sdk.NewDescribeUDPListenersRequest() var offset uint64 = 0 var limit uint64 = 100 @@ -146,45 +126,22 @@ func (repo *QaapTcInstanceUDPListenersRepositoryImpl) GetUDPListenersInfo(instan req.ProxyId = selfcommon.StringPtr(instanceId) return repo.client.DescribeUDPListeners(req) } - -func NewQaapTcInstanceUDPListenersRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (QaapTcInstanceUDPListenersRepository, error) { - cli, err := client.NewGAAPClient(cred, c) - if err != nil { - return nil, err - } - repo := &QaapTcInstanceUDPListenersRepositoryImpl{ - client: cli, - logger: logger, - } - return repo, nil -} - -// DescribeProxyGroupList -type QaapTcInstanceProxyGroupListRepository interface { - GetProxyGroupList(instanceId string) (*sdk.DescribeProxyGroupListResponse, error) -} - -type QaapTcInstanceProxyGroupListRepositoryImpl struct { - client *sdk.Client - logger log.Logger -} - -func (repo *QaapTcInstanceProxyGroupListRepositoryImpl) GetProxyGroupList(instanceId string) (*sdk.DescribeProxyGroupListResponse, error) { - req := sdk.NewDescribeProxyGroupListRequest() - var offset int64 = 0 - var limit int64 = 100 +func (repo *QaapTcInstanceInfoRepositoryImpl) GetTCPListenersInfo(instanceId string) (*sdk.DescribeTCPListenersResponse, error) { + req := sdk.NewDescribeTCPListenersRequest() + var offset uint64 = 0 + var limit uint64 = 100 req.Limit = &limit req.Offset = &offset - // req. = selfcommon.StringPtr(instanceId) - return repo.client.DescribeProxyGroupList(req) + req.ProxyId = selfcommon.StringPtr(instanceId) + return repo.client.DescribeTCPListeners(req) } -func NewQaapTcInstanceProxyGroupListRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (QaapTcInstanceUDPListenersRepository, error) { +func NewQaapTcInstanceInfoRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (QaapTcInstanceInfoRepository, error) { cli, err := client.NewGAAPClient(cred, c) if err != nil { return nil, err } - repo := &QaapTcInstanceUDPListenersRepositoryImpl{ + repo := &QaapTcInstanceInfoRepositoryImpl{ client: cli, logger: logger, } @@ -192,12 +149,12 @@ func NewQaapTcInstanceProxyGroupListRepository(cred common.CredentialIface, c *c } // 内部接口 -type Rsp struct { +type ProxyInstancesResponse struct { TotalCount int64 ProxySet []ProxyDetail } type ProxyInstancesRsp struct { - Response Rsp + Response ProxyInstancesResponse } type ProxyDetail struct { ProxyId string @@ -222,11 +179,22 @@ type L7ListenerDetail struct { RuleSet []RuleDetail } type RuleDetail struct { - RsSet []BoundRsDetail + RsInfo []BoundRsDetail RuleId string } + type NoneBgpIpListRsp struct { - Response Rsp + Response NoneBgpIpListResponse +} +type NoneBgpIpListResponse struct { + TotalCount int64 + InstanceSet []InstanceDetail +} +type InstanceDetail struct { + IP string + Isp string + ProxyId string + GroupId string } type CommonQaapTcInstanceRepository interface { GetCommonQaapProxyInstances(instanceId string) (ProxyInstancesRsp, error) @@ -242,9 +210,9 @@ func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapProxyInstances(inst var proxyInstancesRsp ProxyInstancesRsp request := tchttp.NewCommonRequest("gaap", "2018-05-29", "DescribeProxyInstances") body := map[string]interface{}{ - "Limit": 1, - "Offset": 0, - "ProxyIds": []string{"link-2r1whx05"}, + "Limit": 1, + "Offset": 0, + // "ProxyIds": []string{"link-09ai6y0r"}, } // 设置action所需的请求数据 err := request.SetActionParameters(body) @@ -259,7 +227,6 @@ func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapProxyInstances(inst fmt.Printf("fail to invoke api: %v \n", err) } // 获取响应结果 - // fmt.Println(string(response.GetBody())) json.Unmarshal(response.GetBody(), &proxyInstancesRsp) return proxyInstancesRsp, nil } @@ -284,7 +251,6 @@ func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapNoneBgpIpList(insta fmt.Printf("fail to invoke api: %v \n", err) } // 获取响应结果 - // fmt.Println(string(response.GetBody())) json.Unmarshal(response.GetBody(), &noneBgpIpListRsp) return noneBgpIpListRsp, nil } From b0dfff2f42c27c6b6cc143a45186a4c2c70445d5 Mon Sep 17 00:00:00 2001 From: zianazhao Date: Mon, 5 Dec 2022 12:56:16 +0800 Subject: [PATCH 6/8] gaap --- pkg/collector/handler_qaap.go | 5 +++-- pkg/instance/repository_qaap.go | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/collector/handler_qaap.go b/pkg/collector/handler_qaap.go index 40d7cc3..658c3e5 100644 --- a/pkg/collector/handler_qaap.go +++ b/pkg/collector/handler_qaap.go @@ -283,6 +283,7 @@ func (h *QaapHandler) getQaapListenerStatSeries(m *metric.TcmMetric, ins instanc ql := map[string]string{ "instanceid": proxySet.ProxyId, "listenerid": l4Listener.ListenerId, + "protocol":l4Listener.Protocol, } s, err := metric.NewTcmSeries(m, ql, ins) if err != nil { @@ -294,6 +295,7 @@ func (h *QaapHandler) getQaapListenerStatSeries(m *metric.TcmMetric, ins instanc ql := map[string]string{ "instanceid": proxySet.ProxyId, "listenerid": l7Listener.ListenerId, + "protocol":l7Listener.ForwardProtocol, } s, err := metric.NewTcmSeries(m, ql, ins) if err != nil { @@ -314,8 +316,7 @@ func (h *QaapHandler) getRuleRsSeries(m *metric.TcmMetric, ins instance.TcInstan for _, proxySet := range ProxyInstances.Response.ProxySet { for _, l7Listener := range proxySet.L7ListenerSet { for _, rule := range l7Listener.RuleSet { - for _, rs := range rule.RsInfo { - fmt.Print(proxySet.ProxyId, "-", l7Listener.ListenerId, "") + for _, rs := range rule.RsSet { ql := map[string]string{ "instanceid": proxySet.ProxyId, "listenerid": l7Listener.ListenerId, diff --git a/pkg/instance/repository_qaap.go b/pkg/instance/repository_qaap.go index 36ace2e..e2d651b 100644 --- a/pkg/instance/repository_qaap.go +++ b/pkg/instance/repository_qaap.go @@ -179,7 +179,7 @@ type L7ListenerDetail struct { RuleSet []RuleDetail } type RuleDetail struct { - RsInfo []BoundRsDetail + RsSet []BoundRsDetail RuleId string } @@ -210,9 +210,9 @@ func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapProxyInstances(inst var proxyInstancesRsp ProxyInstancesRsp request := tchttp.NewCommonRequest("gaap", "2018-05-29", "DescribeProxyInstances") body := map[string]interface{}{ - "Limit": 1, - "Offset": 0, - // "ProxyIds": []string{"link-09ai6y0r"}, + "Limit": 100, + "Offset": 0, + "ProxyIds": []string{instanceId}, } // 设置action所需的请求数据 err := request.SetActionParameters(body) @@ -235,7 +235,7 @@ func (repo *CommonQaapTcInstanceRepositoryImpl) GetCommonQaapNoneBgpIpList(insta var noneBgpIpListRsp NoneBgpIpListRsp request := tchttp.NewCommonRequest("gaap", "2018-05-29", "DescribeNoneBgpIpList") body := map[string]interface{}{ - "Limit": 1, + "Limit": 100, "Offset": 0, } // 设置action所需的请求数据 From 7e7024cd3c17983e3e1a338995a63bcbbf8a428d Mon Sep 17 00:00:00 2001 From: zianazhao Date: Mon, 5 Dec 2022 13:00:00 +0800 Subject: [PATCH 7/8] fix-gaap --- pkg/collector/handler_qaap.go | 4 ++-- pkg/instance/cache.go | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/collector/handler_qaap.go b/pkg/collector/handler_qaap.go index 658c3e5..68e5c05 100644 --- a/pkg/collector/handler_qaap.go +++ b/pkg/collector/handler_qaap.go @@ -283,7 +283,7 @@ func (h *QaapHandler) getQaapListenerStatSeries(m *metric.TcmMetric, ins instanc ql := map[string]string{ "instanceid": proxySet.ProxyId, "listenerid": l4Listener.ListenerId, - "protocol":l4Listener.Protocol, + "protocol": l4Listener.Protocol, } s, err := metric.NewTcmSeries(m, ql, ins) if err != nil { @@ -295,7 +295,7 @@ func (h *QaapHandler) getQaapListenerStatSeries(m *metric.TcmMetric, ins instanc ql := map[string]string{ "instanceid": proxySet.ProxyId, "listenerid": l7Listener.ListenerId, - "protocol":l7Listener.ForwardProtocol, + "protocol": l7Listener.ForwardProtocol, } s, err := metric.NewTcmSeries(m, ql, ins) if err != nil { diff --git a/pkg/instance/cache.go b/pkg/instance/cache.go index 8887deb..0e5f772 100644 --- a/pkg/instance/cache.go +++ b/pkg/instance/cache.go @@ -530,9 +530,9 @@ func NewVbcTcInstanceDRegionRepositoryCache(repo VbcTcInstanceDRegionRepository, // gaap type TcGaapInstanceeInfosCache struct { Raw QaapTcInstanceInfoRepository - cache1 map[string]*gaap.DescribeTCPListenersResponse - cache2 map[string]*gaap.DescribeUDPListenersResponse - cache3 map[string]*gaap.DescribeProxyGroupListResponse + tcpCache map[string]*gaap.DescribeTCPListenersResponse + udpCache map[string]*gaap.DescribeUDPListenersResponse + groupCache map[string]*gaap.DescribeProxyGroupListResponse lastReloadTime map[string]time.Time reloadInterval time.Duration mu sync.Mutex @@ -543,7 +543,7 @@ type TcGaapInstanceeInfosCache struct { func (c *TcGaapInstanceeInfosCache) GetTCPListenersInfo(instanceId string) (*gaap.DescribeTCPListenersResponse, error) { lrtime, exists := c.lastReloadTime[instanceId] if exists && time.Now().Sub(lrtime) < c.reloadInterval { - namespace, ok := c.cache1[instanceId] + namespace, ok := c.tcpCache[instanceId] if ok { return namespace, nil } @@ -553,7 +553,7 @@ func (c *TcGaapInstanceeInfosCache) GetTCPListenersInfo(instanceId string) (*gaa if err != nil { return nil, err } - c.cache1[instanceId] = tcpListeners + c.tcpCache[instanceId] = tcpListeners c.lastReloadTime[instanceId] = time.Now() level.Debug(c.logger).Log("msg", "Get gaap Namespaces info from api", "instanceId", instanceId) return tcpListeners, nil @@ -561,7 +561,7 @@ func (c *TcGaapInstanceeInfosCache) GetTCPListenersInfo(instanceId string) (*gaa func (c *TcGaapInstanceeInfosCache) GetUDPListenersInfo(instanceId string) (*gaap.DescribeUDPListenersResponse, error) { lrtime, exists := c.lastReloadTime[instanceId] if exists && time.Now().Sub(lrtime) < c.reloadInterval { - namespace, ok := c.cache2[instanceId] + namespace, ok := c.udpCache[instanceId] if ok { return namespace, nil } @@ -571,7 +571,7 @@ func (c *TcGaapInstanceeInfosCache) GetUDPListenersInfo(instanceId string) (*gaa if err != nil { return nil, err } - c.cache2[instanceId] = udpListeners + c.udpCache[instanceId] = udpListeners c.lastReloadTime[instanceId] = time.Now() level.Debug(c.logger).Log("msg", "Get gaap Namespaces info from api", "instanceId", instanceId) return udpListeners, nil @@ -579,7 +579,7 @@ func (c *TcGaapInstanceeInfosCache) GetUDPListenersInfo(instanceId string) (*gaa func (c *TcGaapInstanceeInfosCache) GetProxyGroupList(instanceId string) (*gaap.DescribeProxyGroupListResponse, error) { lrtime, exists := c.lastReloadTime[instanceId] if exists && time.Now().Sub(lrtime) < c.reloadInterval { - namespace, ok := c.cache3[instanceId] + namespace, ok := c.groupCache[instanceId] if ok { return namespace, nil } @@ -589,7 +589,7 @@ func (c *TcGaapInstanceeInfosCache) GetProxyGroupList(instanceId string) (*gaap. if err != nil { return nil, err } - c.cache3[instanceId] = proxyGroupList + c.groupCache[instanceId] = proxyGroupList c.lastReloadTime[instanceId] = time.Now() level.Debug(c.logger).Log("msg", "Get gaap Namespaces info from api", "instanceId", instanceId) return proxyGroupList, nil @@ -598,9 +598,9 @@ func (c *TcGaapInstanceeInfosCache) GetProxyGroupList(instanceId string) (*gaap. func NewTcGaapInstanceeInfosCache(repo QaapTcInstanceInfoRepository, reloadInterval time.Duration, logger log.Logger) QaapTcInstanceInfoRepository { cache := &TcGaapInstanceeInfosCache{ Raw: repo, - cache1: map[string]*gaap.DescribeTCPListenersResponse{}, - cache2: map[string]*gaap.DescribeUDPListenersResponse{}, - cache3: map[string]*gaap.DescribeProxyGroupListResponse{}, + tcpCache: map[string]*gaap.DescribeTCPListenersResponse{}, + udpCache: map[string]*gaap.DescribeUDPListenersResponse{}, + groupCache: map[string]*gaap.DescribeProxyGroupListResponse{}, lastReloadTime: map[string]time.Time{}, reloadInterval: reloadInterval, logger: logger, From 00ac9dd8ddd6c6b72c4ea24b12a2082387e35102 Mon Sep 17 00:00:00 2001 From: zianazhao Date: Mon, 5 Dec 2022 14:09:00 +0800 Subject: [PATCH 8/8] gaap --- pkg/metric/repository.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/metric/repository.go b/pkg/metric/repository.go index b60bc1d..363d33b 100644 --- a/pkg/metric/repository.go +++ b/pkg/metric/repository.go @@ -197,7 +197,6 @@ func (repo *TcmMetricRepositoryImpl) listSampleByBatch( if err != nil { return nil, err } - level.Info(repo.logger).Log("reqid:", response.Response.RequestId) for _, points := range response.Response.DataPoints { samples, ql, e := repo.buildSamples(m, points) if e != nil {