Skip to content

Commit

Permalink
feat: add new product SqlServer metrics export
Browse files Browse the repository at this point in the history
  • Loading branch information
juexiaolin(林觉霄) authored and juexiaolin committed Jul 5, 2021
1 parent b409a10 commit fc7153f
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 1 deletion.
2 changes: 1 addition & 1 deletion readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ NAT|QCE/NAT_GATEWAY|[指标详情](https://cloud.tencent.com/document/product/24
物理专线|QCE/DC|[指标详情](https://cloud.tencent.com/document/product/248/45102)
专用通道|QCE/DCX|[指标详情](https://cloud.tencent.com/document/product/248/45101)
云硬盘|QCE/CBS|[指标详情](https://cloud.tencent.com/document/product/248/45411)

SqlServer|QCE/SQLSERVER|[指标详情](https://cloud.tencent.com/document/product/248/45146)
`后续会有更多的产品支持`

## 二、快速开始
Expand Down
17 changes: 17 additions & 0 deletions configs/qcloud-sqlserver-product.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
credential:
access_key: "access_key"
secret_key: "secret_key"
region: "region"

rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014

products:
- namespace: QCE/SQLSERVER #指标详情: https://cloud.tencent.com/document/product/248/45146
all_metrics: true
all_instances: true
#only_include_metrics: []
#only_include_instances: [mssql-xxxxxxxx]
#extra_labels: [InstanceId,Name,Region,Vip]
#statistics_types: [last]
period_seconds: 300
#metric_name_type: 2
11 changes: 11 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725"
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412"
sqlserver "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328"
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"

"github.com/tencentyun/tencentcloud-exporter/pkg/config"
Expand Down Expand Up @@ -105,3 +106,13 @@ func NewCbsClient(conf *config.TencentConfig) (*cbs.Client, error) {
cpf.HttpProfile.Endpoint = "cbs.tencentcloudapi.com"
return cbs.NewClient(credential, conf.Credential.Region, cpf)
}

func NewSqlServerClient(conf *config.TencentConfig) (*sqlserver.Client, error) {
credential := common.NewCredential(
conf.Credential.AccessKey,
conf.Credential.SecretKey,
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "sqlserver.tencentcloudapi.com"
return sqlserver.NewClient(credential, conf.Credential.Region, cpf)
}
43 changes: 43 additions & 0 deletions pkg/collector/handler_sqlserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package collector

import (
"github.com/go-kit/kit/log"

"github.com/tencentyun/tencentcloud-exporter/pkg/metric"
)

const (
SqlServerNamespace = "QCE/SQLSERVER"
SqlServerInstanceidKey = "resourceId"
)

func init() {
registerHandler(SqlServerNamespace, defaultHandlerEnabled, NewSqlServerHandler)
}

type sqlServerHandler struct {
baseProductHandler
}

func (h *sqlServerHandler) IsMetricMetaVaild(meta *metric.TcmMeta) bool {
return true
}

func (h *sqlServerHandler) GetNamespace() string {
return SqlServerNamespace
}

func (h *sqlServerHandler) IsMetricVaild(m *metric.TcmMetric) bool {
return true
}

func NewSqlServerHandler(c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) {
handler = &sqlServerHandler{
baseProductHandler{
monitorQueryKey: SqlServerInstanceidKey,
collector: c,
logger: logger,
},
}
return
}
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
"cos": "QCE/COS",
"cdn": "QCE/CDN",
"cbs": "QCE/BLOCK_STORAGE",
"sqlserver": "QCE/SQLSERVER",
}

SupportStatisticsTypes = map[string]bool{
Expand Down
34 changes: 34 additions & 0 deletions pkg/instance/instance_sqlserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package instance

import (
"fmt"
"reflect"

sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328"
)

type SqlServerTcInstance struct {
baseTcInstance
meta *sdk.DBInstance
}

func (ins *SqlServerTcInstance) GetMeta() interface{} {
return ins.meta
}

func NewSqlServerTcInstance(instanceId string, meta *sdk.DBInstance) (ins *SqlServerTcInstance, err error) {
if instanceId == "" {
return nil, fmt.Errorf("instanceId is empty ")
}
if meta == nil {
return nil, fmt.Errorf("meta is empty ")
}
ins = &SqlServerTcInstance{
baseTcInstance: baseTcInstance{
instanceId: instanceId,
value: reflect.ValueOf(*meta),
},
meta: meta,
}
return
}
93 changes: 93 additions & 0 deletions pkg/instance/repository_sqlserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package instance

import (
"fmt"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328"

"github.com/tencentyun/tencentcloud-exporter/pkg/client"
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
)

func init() {
registerRepository("QCE/SQLSERVER", NewSqlServerTcInstanceRepository)
}

type SqlServerTcInstanceRepository struct {
client *sdk.Client
logger log.Logger
}

func (repo *SqlServerTcInstanceRepository) GetInstanceKey() string {
return "InstanceId"
}

func (repo *SqlServerTcInstanceRepository) Get(id string) (instance TcInstance, err error) {
req := sdk.NewDescribeDBInstancesRequest()
req.InstanceIdSet = []*string{&id}
resp, err := repo.client.DescribeDBInstances(req)
if err != nil {
return
}
if len(resp.Response.DBInstances) != 1 {
return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id)
}
meta := resp.Response.DBInstances[0]
instance, err = NewSqlServerTcInstance(id, meta)
if err != nil {
return
}
return
}

func (repo *SqlServerTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) {
return
}

func (repo *SqlServerTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) {
req := sdk.NewDescribeDBInstancesRequest()
var offset int64 = 0
var limit int64 = 100
var total int64 = -1

req.Offset = &offset
req.Limit = &limit

getMoreInstances:
resp, err := repo.client.DescribeDBInstances(req)
if err != nil {
return
}
if total == -1 {
total = *resp.Response.TotalCount
}
for _, meta := range resp.Response.DBInstances {
ins, e := NewSqlServerTcInstance(*meta.InstanceId, meta)
if e != nil {
level.Error(repo.logger).Log("msg", "Create cdb instance fail", "id", *meta.InstanceId)
continue
}
instances = append(instances, ins)
}
offset += limit
if offset < total {
req.Offset = &offset
goto getMoreInstances
}

return
}

func NewSqlServerTcInstanceRepository(c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) {
cli, err := client.NewSqlServerClient(c)
if err != nil {
return
}
repo = &SqlServerTcInstanceRepository{
client: cli,
logger: logger,
}
return
}

0 comments on commit fc7153f

Please sign in to comment.