Skip to content

Commit

Permalink
feat: 新增slo的上报指标周期任务 (#503)
Browse files Browse the repository at this point in the history
修改之前监控平台数据采集中的slo指标上报,把上报过程改为周期任务集成在bmw中
  • Loading branch information
PlasticTong authored Sep 4, 2024
1 parent 0ebed11 commit 0aa874b
Show file tree
Hide file tree
Showing 18 changed files with 3,625 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/bk-monitor-worker/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ var (
BkApiBcsCcApiUrl string
// BkApiGseApiGwUrl bk-apigw bkgse base url
BkApiGseApiGwUrl string
// SloPushGatewayApi 是否启用监控的apiGateway
BkMonitorApiGatewayEnabled bool
// BkMonitorApiGatewayBaseUrl 监控的apiGateway
BkMonitorApiGatewayBaseUrl string
// BkMonitorApiGatewayStage 监控的apiGateway的环境
BkMonitorApiGatewayStage string

// GoroutineLimit max size of task goroutine
GoroutineLimit map[string]string
Expand Down Expand Up @@ -404,6 +410,13 @@ func initVariables() {
BkApiBcsCcApiUrl = GetValue("taskConfig.common.bkapi.bcsCcApiUrl", "")
BkApiGseApiGwUrl = GetValue("taskConfig.common.bkapi.bkgseApiGwUrl", "")

// SloPushGatewayApi 是否启用监控的apiGateway
BkMonitorApiGatewayEnabled = GetValue("taskConfig.common.bkapi.bkmonitorApiGatewayEnabled", false)
// BkMonitorApiGatewayBaseUrl 监控的apiGateway
BkMonitorApiGatewayBaseUrl = GetValue("taskConfig.common.bkapi.bkmonitorApiGatewayBaseUrl", BkApiUrl)
// BkMonitorApiGatewayStage 监控的apiGateway的环境
BkMonitorApiGatewayStage = GetValue("taskConfig.common.bkapi.bkmonitorApiGatewayStage", "stag")

GoroutineLimit = GetValue("taskConfig.common.goroutineLimit", map[string]string{}, viper.GetStringMapString)

ESClusterMetricReportUrl = GetValue("taskConfig.logSearch.metric.reportUrl", "")
Expand Down
8 changes: 8 additions & 0 deletions pkg/bk-monitor-worker/config/metadata_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ var (
SkipInfluxdbTableIds []string
// 是否可以删除 consul 路径
CanDeleteConsulPath bool

// SloPushGatewayToken slo数据上报Token
SloPushGatewayToken string
// SloPushGatewayEndpoint slo数据上报端点
SloPushGatewayEndpoint string
)

func initMetadataVariables() {
Expand Down Expand Up @@ -181,4 +186,7 @@ func initMetadataVariables() {
BkdataIsAllowAllCmdbLevel = GetValue("taskConfig.metadata.bkdata.isAllowAllCmdbLevel", false)
SkipInfluxdbTableIds = GetValue("taskConfig.metadata.global.skipInfluxdbTableIds", []string{})
CanDeleteConsulPath = GetValue("taskConfig.metadata.global.CanDeleteConsulPath", false)

SloPushGatewayToken = GetValue("taskConfig.metadata.slo.sloPushGatewayToken", "")
SloPushGatewayEndpoint = GetValue("taskConfig.metadata.slo.sloPushGatewayEndpoint", "")
}
38 changes: 38 additions & 0 deletions pkg/bk-monitor-worker/internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/internal/api/cmdb"
apiDefine "github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/internal/api/define"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/internal/api/metadata"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/internal/api/monitor"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/internal/api/nodeman"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/utils/jsonx"
)
Expand All @@ -42,6 +43,7 @@ var (
muForNodemanApi sync.Mutex
muForBkdataApi sync.Mutex
muForMetadataApi sync.Mutex
muForMonitorApi sync.Mutex
)

var (
Expand All @@ -54,6 +56,7 @@ var (
nodemanApi *nodeman.Client
bkdataApi *bkdata.Client
metadataApi *metadata.Client
monitorApi *monitor.Client
)

// GetGseApi 获取GseApi客户端
Expand Down Expand Up @@ -272,6 +275,41 @@ func GetMetadataApi() (*metadata.Client, error) {
return metadataApi, nil
}

// GetMonitorApi 获取metadataApi客户端
func GetMonitorApi() (*monitor.Client, error) {
muForMonitorApi.Lock()
defer muForMonitorApi.Unlock()
if monitorApi != nil {
return monitorApi, nil
}
var config define.ClientConfigProvider
useBkMonitorApigw := cfg.BkMonitorApiGatewayEnabled
if useBkMonitorApigw {
config = bkapi.ClientConfig{
Endpoint: cfg.BkMonitorApiGatewayBaseUrl,
Stage: cfg.BkMonitorApiGatewayStage,
AppCode: cfg.BkApiAppCode,
AppSecret: cfg.BkApiAppSecret,
JsonMarshaler: jsonx.Marshal,
}
} else {
config = bkapi.ClientConfig{
Endpoint: fmt.Sprintf("%s/api/c/compapi/v2/monitor_v3/", cfg.BkApiUrl),
Stage: cfg.BkApiStage,
AppCode: cfg.BkApiAppCode,
AppSecret: cfg.BkApiAppSecret,
JsonMarshaler: jsonx.Marshal,
AuthorizationParams: map[string]string{"bk_username": "admin"},
}
}
var err error
monitorApi, err = monitor.New(useBkMonitorApigw, config, bkapi.OptJsonResultProvider(), bkapi.OptJsonBodyProvider())
if err != nil {
return nil, err
}
return monitorApi, nil
}

// HeaderProvider provide request header.
type HeaderProvider struct {
Header map[string]string
Expand Down
41 changes: 41 additions & 0 deletions pkg/bk-monitor-worker/internal/api/monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台 (BlueKing - Monitor) available.
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

package monitor

import (
"github.com/TencentBlueKing/bk-apigateway-sdks/core/bkapi"
"github.com/TencentBlueKing/bk-apigateway-sdks/core/define"
)

// Client for metadata
type Client struct {
define.BkApiClient
useBkMonitorApigw bool
}

// New monitor client
func New(useBkMonitorApigw bool, configProvider define.ClientConfigProvider, opts ...define.BkApiClientOption) (*Client, error) {
client, err := bkapi.NewBkApiClient("monitor_v3", configProvider, opts...)
if err != nil {
return nil, err
}

return &Client{BkApiClient: client, useBkMonitorApigw: useBkMonitorApigw}, nil
}

// SearchAlert for monitor resource search_alert
func (c *Client) SearchAlert(opts ...define.OperationOption) define.Operation {
path := "search_alert"
return c.BkApiClient.NewOperation(bkapi.OperationConfig{
Name: "search_alert",
Method: "POST",
Path: path,
}, opts...)
}
34 changes: 34 additions & 0 deletions pkg/bk-monitor-worker/internal/api/monitor/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台 (BlueKing - Monitor) available.
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

package monitor

import (
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/internal/api/define"
)

type SearchAlertResp struct {
define.ApiCommonRespMeta
Data SearchAlertData `json:"data"`
}

type SearchAlertData struct {
Total int `json:"total"`
Alerts []SearchAlertDataInfo `json:"alerts"`
}
type SearchAlertDataInfo struct {
BkBizID int32 `json:"bk_biz_id"`
BkBizName string `json:"bk_biz_name"`
StrategyID int32 `json:"strategy_id"`
StrategyName string `json:"strategy_name"`
FirstAnomalyTime int64 `json:"first_anomaly_time"`
LatestTime int64 `json:"latest_time"`
EventID string `json:"event_id"`
Status string `json:"status"`
}
45 changes: 45 additions & 0 deletions pkg/bk-monitor-worker/internal/metadata/apiservice/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台 (BlueKing - Monitor) available.
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

package apiservice

import (
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/internal/api"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/bk-monitor-worker/internal/api/monitor"
"github.com/pkg/errors"
)

var Monitor MonitorService

type MonitorService struct{}

// SearchAlert 获取告警数据
func (MonitorService) SearchAlert(conditions []map[string]interface{}, startTime int64, endTime int64, page int, pageSize int, BkBizID int32) (*monitor.SearchAlertData, error) {
monitorApi, err := api.GetMonitorApi()
if err != nil {
return nil, errors.Wrap(err, "GetMonitorApi failed")
}
var resp monitor.SearchAlertResp
var params = map[string]interface{}{
"bk_biz_ids": []int{int(BkBizID)},
"start_time": startTime,
"end_time": endTime,
"page": page,
"page_size": pageSize,
"conditions": conditions,
}
_, err = monitorApi.SearchAlert().SetBody(params).SetResult(&resp).Request()
if err != nil {
return nil, errors.Wrap(err, "SearchAlert failed")
}
if err := resp.Err(); err != nil {
return nil, errors.Wrap(err, "SearchAlert failed")
}
return &resp.Data, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台 (BlueKing - Monitor) available.
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

package slo

const TableNameAlarmQueryConfigV2 = "alarm_query_config_v2"

// AlarmQueryConfigV2 mapped from table <alarm_query_config_v2>
type AlarmQueryConfigV2 struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
StrategyID int32 `gorm:"column:strategy_id;not null" json:"strategy_id"`
ItemID int32 `gorm:"column:item_id;not null" json:"item_id"`
Alias string `gorm:"column:alias;not null" json:"alias"`
DataSourceLabel string `gorm:"column:data_source_label;not null" json:"data_source_label"`
DataTypeLabel string `gorm:"column:data_type_label;not null" json:"data_type_label"`
MetricID string `gorm:"column:metric_id;not null" json:"metric_id"`
Config string `gorm:"column:config;not null" json:"config"`
}

// TableName AlarmQueryConfigV2's table name
func (*AlarmQueryConfigV2) TableName() string {
return TableNameAlarmQueryConfigV2
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台 (BlueKing - Monitor) available.
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

package slo

import (
"time"
)

const TableNameAlarmStrategyV2 = "alarm_strategy_v2"

//go:generate goqueryset -in alarm_strategy_v2.go -out qs_alarm_strategy_v2_gen.go

// AlarmStrategyV2 mapped from table <alarm_strategy_v2>
type AlarmStrategyV2 struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Name string `gorm:"column:name;not null" json:"name"`
BkBizID int32 `gorm:"column:bk_biz_id;not null" json:"bk_biz_id"`
Source string `gorm:"column:source;not null" json:"source"`
Scenario string `gorm:"column:scenario;not null" json:"scenario"`
Type string `gorm:"column:type;not null" json:"type"`
IsEnabled bool `gorm:"column:is_enabled;not null" json:"is_enabled"`
CreateUser string `gorm:"column:create_user;not null" json:"create_user"`
CreateTime time.Time `gorm:"column:create_time;not null" json:"create_time"`
UpdateUser string `gorm:"column:update_user;not null" json:"update_user"`
UpdateTime time.Time `gorm:"column:update_time;not null" json:"update_time"`
IsInvalid bool `gorm:"column:is_invalid;not null" json:"is_invalid"`
InvalidType string `gorm:"column:invalid_type;not null" json:"invalid_type"`
App string `gorm:"column:app" json:"app"`
Hash string `gorm:"column:hash" json:"hash"`
Path string `gorm:"column:path" json:"path"`
Snippet string `gorm:"column:snippet" json:"snippet"`
Priority int32 `gorm:"column:priority" json:"priority"`
PriorityGroupKey string `gorm:"column:priority_group_key" json:"priority_group_key"`
}

// TableName AlarmStrategyV2's table name
func (*AlarmStrategyV2) TableName() string {
return TableNameAlarmStrategyV2
}
Loading

0 comments on commit 0aa874b

Please sign in to comment.