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

fix: 修复 prometheus 指标上报为兼容 token 问题 --bug=130652727 #532

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func MetricFlowBuckets(b []float64) MetricConfigOption {

type MetricDimensionsHandler struct {
dataId string
token string

ctx context.Context
mu sync.Mutex
Expand Down Expand Up @@ -102,7 +103,7 @@ func (m *MetricDimensionsHandler) cleanUpAndReport(c MetricCollector) {
writeReq := c.Collect()
metrics.RecordApmPreCalcOperateStorageCount(m.dataId, metrics.StoragePrometheus, metrics.OperateSave)
metrics.RecordApmPreCalcSaveStorageTotal(m.dataId, metrics.StoragePrometheus, len(writeReq.Timeseries))
if err := m.promClient.WriteBatch(context.Background(), "", writeReq); err != nil {
if err := m.promClient.WriteBatch(context.Background(), m.token, writeReq); err != nil {
metrics.RecordApmPreCalcOperateStorageFailedTotal(m.dataId, metrics.SavePrometheusFailed)
m.logger.Errorf("[TraceMetricsReport] DataId: %s report to prometheus failed, error: %s", m.dataId, err)
}
Expand Down Expand Up @@ -145,7 +146,8 @@ func NewMetricDimensionHandler(ctx context.Context, dataId string,

h := &MetricDimensionsHandler{
dataId: dataId,
promClient: remote.NewPrometheusWriterClient(token, config.Url, config.Headers),
token: token,
promClient: remote.NewPrometheusWriterClient(config.Url, config.Headers),
relationMetricDimensions: newRelationMetricCollector(metricsConfig.relationMetricMemDuration),
flowMetricCollector: newFlowMetricCollector(metricsConfig.flowMetricBuckets, metricsConfig.flowMetricMemDuration),
ctx: ctx,
Expand Down
23 changes: 3 additions & 20 deletions pkg/bk-monitor-worker/utils/remote/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/prometheus/prompb"
"go.uber.org/zap"
"golang.org/x/exp/maps"

monitorLogger "github.com/TencentBlueKing/bkmonitor-datalink/pkg/utils/logger"
)
Expand Down Expand Up @@ -55,13 +54,12 @@ type PrometheusWriter struct {
headers map[string]string

client *http.Client
isValid bool
logger monitorLogger.Logger
responseHook func(bool)
}

func (p *PrometheusWriter) WriteBatch(ctx context.Context, token string, writeReq prompb.WriteRequest) error {
if !p.isValid || len(writeReq.Timeseries) == 0 {
if len(writeReq.Timeseries) == 0 {
return nil
}

Expand Down Expand Up @@ -105,7 +103,7 @@ func (p *PrometheusWriter) WriteBatch(ctx context.Context, token string, writeRe
return nil
}

func NewPrometheusWriterClient(token, url string, headers map[string]string) *PrometheusWriter {
func NewPrometheusWriterClient(url string, headers map[string]string) *PrometheusWriter {
client := &http.Client{
Transport: &http.Transport{
MaxIdleConns: 10,
Expand All @@ -114,25 +112,10 @@ func NewPrometheusWriterClient(token, url string, headers map[string]string) *Pr
Timeout: 10 * time.Second,
}

h := make(map[string]string, len(headers))
maps.Copy(h, headers)
if _, exist := h["x-bk-token"]; !exist {
if _, oExist := h[tokenKey]; !oExist {
h[tokenKey] = token
}
} else {
h[tokenKey] = h["x-bk-token"]
}
isValid := false
if v, _ := h[tokenKey]; v != "" {
isValid = true
}

return &PrometheusWriter{
url: url,
headers: h,
headers: headers,
client: client,
isValid: isValid,
logger: monitorLogger.With(zap.String("name", "prometheus")),
}
}
2 changes: 1 addition & 1 deletion pkg/bk-monitor-worker/utils/remote/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestPrometheusWriter_WriteBatch(t *testing.T) {
mocker.InitTestDBConfig("../../dist/bmw.yaml")

metric := fmt.Sprintf("prometheus_%s", time.Now().Format("2006010215"))
prometheusWriter := NewPrometheusWriterClient("", config.PromRemoteWriteUrl, config.PromRemoteWriteHeaders)
prometheusWriter := NewPrometheusWriterClient(config.PromRemoteWriteUrl, config.PromRemoteWriteHeaders)
ts := append([]prompb.TimeSeries{

{
Expand Down
2 changes: 1 addition & 1 deletion pkg/bk-monitor-worker/utils/remote/space_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewSpaceReporter(key string, writerUrl string) (Reporter, error) {
return nil, fmt.Errorf("failed to create redis client")
}

writer := NewPrometheusWriterClient("", writerUrl, map[string]string{})
writer := NewPrometheusWriterClient(writerUrl, map[string]string{})

report := &reporter{
client: inst.Client,
Expand Down