@@ -6,12 +6,15 @@ import (
6
6
"strconv"
7
7
"sync"
8
8
9
+ enginefactory "github.com/projecteru2/core/engine/factory"
9
10
"github.com/projecteru2/core/log"
10
11
"github.com/projecteru2/core/resource"
11
12
"github.com/projecteru2/core/resource/cobalt"
12
13
plugintypes "github.com/projecteru2/core/resource/plugins/types"
13
14
"github.com/projecteru2/core/types"
14
15
"github.com/projecteru2/core/utils"
16
+ io_prometheus_client "github.com/prometheus/client_model/go"
17
+ "golang.org/x/exp/slices"
15
18
16
19
statsdlib "github.com/CMGS/statsd"
17
20
"github.com/prometheus/client_golang/prometheus"
@@ -85,6 +88,78 @@ func (m *Metrics) SendMetrics(ctx context.Context, metrics ...*plugintypes.Metri
85
88
}
86
89
}
87
90
91
+ func (m * Metrics ) DeleteInactiveNodesWithCache (ctx context.Context , activeNodesMap map [string ]* types.Node ) {
92
+ metricNodeNameMap := m .getNodeNameMapFromMetrics ()
93
+ // 计算差集
94
+ invalidNodes := make ([]string , 0 )
95
+ for nodeName := range metricNodeNameMap {
96
+ if node , exists := activeNodesMap [nodeName ]; ! exists {
97
+ invalidNodes = append (invalidNodes , nodeName )
98
+ enginefactory .RemoveEngineFromCache (ctx , node .Endpoint , node .Ca , node .Cert , node .Key )
99
+ }
100
+ }
101
+ m .RemoveInvalidNodes (invalidNodes )
102
+ }
103
+
104
+ func (m * Metrics ) getNodeNameMapFromMetrics () map [string ]bool {
105
+ metrics , _ := prometheus .DefaultGatherer .Gather ()
106
+ nodeNameMap := make (map [string ]bool , 0 )
107
+ for _ , metric := range metrics {
108
+ for _ , mf := range metric .GetMetric () {
109
+ if len (mf .Label ) == 0 {
110
+ continue
111
+ }
112
+ for _ , label := range mf .Label {
113
+ if label .GetName () == "nodename" {
114
+ nodeNameMap [label .GetValue ()] = true
115
+ break
116
+ }
117
+ }
118
+ }
119
+ }
120
+ return nodeNameMap
121
+ }
122
+
123
+ // RemoveInvalidNodes 清除多余的metric标签值
124
+ func (m * Metrics ) RemoveInvalidNodes (invalidNodes []string ) {
125
+ if len (invalidNodes ) == 0 {
126
+ return
127
+ }
128
+ for _ , collector := range m .Collectors {
129
+ if collector == nil {
130
+ return
131
+ }
132
+ metrics , _ := prometheus .DefaultGatherer .Gather ()
133
+ for _ , metric := range metrics {
134
+ for _ , mf := range metric .GetMetric () {
135
+ if len (mf .Label ) == 0 {
136
+ continue
137
+ }
138
+
139
+ if ! slices .ContainsFunc (mf .Label , func (label * io_prometheus_client.LabelPair ) bool {
140
+ return label .GetName () == "nodename" && slices .ContainsFunc (invalidNodes , func (nodename string ) bool {
141
+ return label .GetValue () == nodename
142
+ })
143
+ }) {
144
+ continue
145
+ }
146
+ labels := prometheus.Labels {}
147
+ for _ , label := range mf .Label {
148
+ labels [label .GetName ()] = label .GetValue ()
149
+ }
150
+ // 删除符合条件的度量标签
151
+ switch c := collector .(type ) {
152
+ case * prometheus.GaugeVec :
153
+ c .Delete (labels )
154
+ case * prometheus.CounterVec :
155
+ c .Delete (labels )
156
+ }
157
+ }
158
+ }
159
+ // 添加更多的条件来处理其他类型的Collector
160
+ }
161
+ }
162
+
88
163
// Lazy connect
89
164
func (m * Metrics ) checkConn (ctx context.Context ) error {
90
165
if m .statsdClient != nil {
0 commit comments