Skip to content

Commit 1ed6a2f

Browse files
author
Cairry
committed
🚀 Optimized probe label rename and datasourve write cache
1 parent 2c6287a commit 1ed6a2f

6 files changed

Lines changed: 59 additions & 27 deletions

File tree

alert/probe/service.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ func (s *ProbeService) GetActiveRules() int {
105105
return len(s.watchCtxMap)
106106
}
107107

108+
func (s *ProbeService) ResetWriteCache(datasourceId string, writer MetricsWriter) {
109+
s.writerCacheMu.Lock()
110+
s.writerCache[datasourceId] = writer
111+
s.writerCacheMu.Unlock()
112+
}
113+
114+
func (s *ProbeService) DeleteWriteCache(datasourceId string) {
115+
s.writerCacheMu.Lock()
116+
delete(s.writerCache, datasourceId)
117+
s.writerCacheMu.Unlock()
118+
}
119+
108120
// runProbing 运行拨测
109121
func (s *ProbeService) runProbing(ctx context.Context, rule models.ProbeRule) {
110122
timer := time.NewTicker(time.Second * time.Duration(rule.ProbingEndpointConfig.Strategy.EvalInterval))

internal/services/datasource.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package services
33
import (
44
"fmt"
55
"time"
6+
"watchAlert/alert"
7+
"watchAlert/alert/probe"
68
"watchAlert/internal/ctx"
79
"watchAlert/internal/models"
810
"watchAlert/internal/types"
@@ -62,6 +64,14 @@ func (ds datasourceService) Create(req interface{}) (interface{}, interface{}) {
6264
return nil, err
6365
}
6466

67+
if dataSource.Type == "Prometheus" || dataSource.Type == "VictoriaMetrics" {
68+
alert.Probe.ResetWriteCache(data.ID, probe.NewWriter(probe.MetricsWriterConfig{
69+
Endpoint: dataSource.Write.URL,
70+
Username: dataSource.Auth.User,
71+
Password: dataSource.Auth.Pass,
72+
}))
73+
}
74+
6575
return nil, nil
6676
}
6777

@@ -97,6 +107,14 @@ func (ds datasourceService) Update(req interface{}) (interface{}, interface{}) {
97107
return nil, err
98108
}
99109

110+
if dataSource.Type == "Prometheus" || dataSource.Type == "VictoriaMetrics" {
111+
alert.Probe.ResetWriteCache(data.ID, probe.NewWriter(probe.MetricsWriterConfig{
112+
Endpoint: dataSource.Write.URL,
113+
Username: dataSource.Auth.User,
114+
Password: dataSource.Auth.Pass,
115+
}))
116+
}
117+
100118
return nil, nil
101119
}
102120

@@ -109,6 +127,8 @@ func (ds datasourceService) Delete(req interface{}) (interface{}, interface{}) {
109127

110128
ds.WithRemoveClientForProviderPools(dataSource.ID)
111129

130+
alert.Probe.DeleteWriteCache(dataSource.ID)
131+
112132
return nil, nil
113133
}
114134

pkg/provider/probe_http.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ func (h HTTPer) PilotWithMetrics(option EndpointOption, ruleInfo ProbeRuleInfo)
3939

4040
// 创建基础标签
4141
baseLabels := map[string]any{
42-
"tenant_id": ruleInfo.TenantID,
43-
"rule_id": ruleInfo.RuleID,
44-
"rule_name": ruleInfo.RuleName,
45-
"rule_type": ruleInfo.RuleType,
46-
"endpoint": ruleInfo.Endpoint,
42+
"tenant_id": ruleInfo.TenantID,
43+
"probe_id": ruleInfo.RuleID,
44+
"probe_name": ruleInfo.RuleName,
45+
"probe_type": ruleInfo.RuleType,
46+
"endpoint": ruleInfo.Endpoint,
4747
}
4848

4949
// 添加HTTP特定标签

pkg/provider/probe_ping.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ func (p Pinger) PilotWithMetrics(option EndpointOption, ruleInfo ProbeRuleInfo)
5656

5757
// 创建基础标签
5858
baseLabels := map[string]any{
59-
"tenant_id": ruleInfo.TenantID,
60-
"rule_id": ruleInfo.RuleID,
61-
"rule_name": ruleInfo.RuleName,
62-
"rule_type": ruleInfo.RuleType,
63-
"endpoint": ruleInfo.Endpoint,
64-
"ip_addr": detail.IPAddr,
59+
"tenant_id": ruleInfo.TenantID,
60+
"rule_id": ruleInfo.RuleID,
61+
"probe_name": ruleInfo.RuleName,
62+
"probe_type": ruleInfo.RuleType,
63+
"endpoint": ruleInfo.Endpoint,
64+
"ip_addr": detail.IPAddr,
6565
}
6666

6767
// 创建ICMP指标
@@ -122,12 +122,12 @@ func (p Pinger) PilotWithMetrics(option EndpointOption, ruleInfo ProbeRuleInfo)
122122
// createFailureMetrics 创建失败时的指标
123123
func (p Pinger) createFailureMetrics(ruleInfo ProbeRuleInfo, timestamp int64, errorMsg string) []ProbeMetric {
124124
baseLabels := map[string]any{
125-
"tenant_id": ruleInfo.TenantID,
126-
"rule_id": ruleInfo.RuleID,
127-
"rule_name": ruleInfo.RuleName,
128-
"rule_type": ruleInfo.RuleType,
129-
"endpoint": ruleInfo.Endpoint,
130-
"error": errorMsg,
125+
"tenant_id": ruleInfo.TenantID,
126+
"probe_id": ruleInfo.RuleID,
127+
"probe_name": ruleInfo.RuleName,
128+
"probe_type": ruleInfo.RuleType,
129+
"endpoint": ruleInfo.Endpoint,
130+
"error": errorMsg,
131131
}
132132

133133
return []ProbeMetric{

pkg/provider/probe_ssl.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ func (p Ssler) PilotWithMetrics(option EndpointOption, ruleInfo ProbeRuleInfo) [
1919

2020
// 创建基础标签
2121
baseLabels := map[string]any{
22-
"tenant_id": ruleInfo.TenantID,
23-
"rule_id": ruleInfo.RuleID,
24-
"rule_name": ruleInfo.RuleName,
25-
"rule_type": ruleInfo.RuleType,
26-
"endpoint": ruleInfo.Endpoint,
22+
"tenant_id": ruleInfo.TenantID,
23+
"probe_id": ruleInfo.RuleID,
24+
"probe_name": ruleInfo.RuleName,
25+
"probe_type": ruleInfo.RuleType,
26+
"endpoint": ruleInfo.Endpoint,
2727
}
2828

2929
// 发起 HTTPS 请求

pkg/provider/probe_tcp.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func (p Tcper) PilotWithMetrics(option EndpointOption, ruleInfo ProbeRuleInfo) [
2323

2424
// 创建基础标签
2525
baseLabels := map[string]any{
26-
"tenant_id": ruleInfo.TenantID,
27-
"rule_id": ruleInfo.RuleID,
28-
"rule_name": ruleInfo.RuleName,
29-
"rule_type": ruleInfo.RuleType,
30-
"endpoint": ruleInfo.Endpoint,
26+
"tenant_id": ruleInfo.TenantID,
27+
"probe_id": ruleInfo.RuleID,
28+
"probe_name": ruleInfo.RuleName,
29+
"probe_type": ruleInfo.RuleType,
30+
"endpoint": ruleInfo.Endpoint,
3131
}
3232

3333
// 确定成功状态

0 commit comments

Comments
 (0)