Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: tcloud api metric #1146

Draft
wants to merge 1 commit into
base: v1.6.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions pkg/adaptor/metric/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available.
* Copyright (C) 2024 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.
*
* We undertake not to change the open source license (MIT license) applicable
*
* to the current version of the project delivered to anyone in the future.
*/

package metric

import (
"net/http"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"

"hcm/pkg/criteria/enumor"
"hcm/pkg/metrics"
)

// restMetric is used to collect cloud api metrics.
var cloudApiMetric *metric

func init() {
m := new(metric)
labels := prometheus.Labels{}

m.lagMS = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: metrics.Namespace,
Subsystem: metrics.CloudApiSubSys,
Name: "lag_seconds",
Help: "the lag seconds to request the cloud API",
ConstLabels: labels,
Buckets: []float64{0.01, 0.025, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5, 0.07,
1, 2, 3, 4, 5, 10, 20, 30},
}, []string{"vendor", "http_code", "api_name", "region", "endpoint"})
metrics.Register().MustRegister(m.lagMS)

m.errCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metrics.Namespace,
Subsystem: metrics.RestfulSubSys,
Name: "total_err_count",
Help: "the total error count to request the restful API",
ConstLabels: labels,
}, []string{"vendor", "http_code", "api_name", "region", "endpoint"})
metrics.Register().MustRegister(m.errCounter)

cloudApiMetric = m
}

type metric struct {
// lagMS record the cost time of request cloud API.
lagMS *prometheus.HistogramVec

// errCounter record the total error count request cloud API.
errCounter *prometheus.CounterVec
}

// GetTCloudRecordRoundTripper get record round tripper for tcloud
func GetTCloudRecordRoundTripper(next http.RoundTripper) promhttp.RoundTripperFunc {
if next == nil {
next = http.DefaultTransport
}
return func(req *http.Request) (*http.Response, error) {
action := strings.Join(req.Header["X-TC-Action"], ",")
region := strings.Join(req.Header["X-TC-Region"], ",")
start := time.Now()
code := "nil"
ret, err := next.RoundTrip(req)
if ret != nil {
code = ret.Status
}
cost := time.Since(start).Seconds()
cloudApiMetric.lagMS.With(
prometheus.Labels{
"vendor": string(enumor.TCloud),
"endpoint": req.Host,
"region": region,
"api_name": action,
"http_code": code,
}).Observe(cost)
return ret, err
}
}
11 changes: 8 additions & 3 deletions pkg/adaptor/tcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package tcloud
import (
"time"

"hcm/pkg/adaptor/metric"
"hcm/pkg/adaptor/types"
"hcm/pkg/criteria/constant"
"hcm/pkg/tools/rand"
Expand Down Expand Up @@ -88,7 +89,7 @@ func (c *clientSet) CamServiceClient(region string) (*cam.Client, error) {
if err != nil {
return nil, err
}

client.WithHttpTransport(metric.GetTCloudRecordRoundTripper(nil))
return client, nil
}

Expand All @@ -98,7 +99,7 @@ func (c *clientSet) CvmClient(region string) (*cvm.Client, error) {
if err != nil {
return nil, err
}

client.WithHttpTransport(metric.GetTCloudRecordRoundTripper(nil))
return client, nil
}

Expand All @@ -108,7 +109,7 @@ func (c *clientSet) CbsClient(region string) (*cbs.Client, error) {
if err != nil {
return nil, err
}

client.WithHttpTransport(metric.GetTCloudRecordRoundTripper(nil))
return client, nil
}

Expand All @@ -118,6 +119,7 @@ func (c *clientSet) VpcClient(region string) (*vpc.Client, error) {
if err != nil {
return nil, err
}
client.WithHttpTransport(metric.GetTCloudRecordRoundTripper(nil))

return client, nil
}
Expand All @@ -128,6 +130,7 @@ func (c *clientSet) BillClient() (*billing.Client, error) {
if err != nil {
return nil, err
}
client.WithHttpTransport(metric.GetTCloudRecordRoundTripper(nil))

return client, nil
}
Expand All @@ -138,6 +141,7 @@ func (c *clientSet) ClbClient(region string) (*clb.Client, error) {
if err != nil {
return nil, err
}
client.WithHttpTransport(metric.GetTCloudRecordRoundTripper(nil))

return client, nil
}
Expand All @@ -148,6 +152,7 @@ func (c *clientSet) CertClient() (*ssl.Client, error) {
if err != nil {
return nil, err
}
client.WithHttpTransport(metric.GetTCloudRecordRoundTripper(nil))

return client, nil
}
3 changes: 3 additions & 0 deletions pkg/metrics/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ const (

// OrmCmdSubSys defines all the orm command related sub system.
OrmCmdSubSys = "orm"

// CloudApiSubSys defines all cloud api related subsystem
CloudApiSubSys = "cloudapi"
)

// labels
Expand Down