-
Notifications
You must be signed in to change notification settings - Fork 308
/
metric_target.go
100 lines (90 loc) · 2.95 KB
/
metric_target.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 encoder
import (
"sync"
mapset "github.com/deckarep/golang-set/v2"
"google.golang.org/protobuf/proto"
"github.com/deepflowio/deepflow/message/controller"
"github.com/deepflowio/deepflow/server/controller/db/mysql"
"github.com/deepflowio/deepflow/server/controller/prometheus/cache"
"github.com/deepflowio/deepflow/server/controller/prometheus/common"
)
type metricTarget struct {
org *common.ORG
lock sync.Mutex
resourceType string
metricTargets mapset.Set[cache.MetricTargetKey]
targetEncoder *target
}
func newMetricTarget(org *common.ORG, te *target) *metricTarget {
return &metricTarget{
org: org,
resourceType: "metric_target",
metricTargets: mapset.NewSet[cache.MetricTargetKey](),
targetEncoder: te,
}
}
func (mt *metricTarget) refresh(args ...interface{}) error {
var items []*mysql.PrometheusMetricTarget
err := mt.org.DB.Find(&items).Error
if err != nil {
return err
}
for _, item := range items {
mt.metricTargets.Add(cache.NewMetricTargetKey(item.MetricName, item.TargetID))
}
return nil
}
func (mt *metricTarget) encode(toAdd []*controller.PrometheusMetricTargetRequest) ([]*controller.PrometheusMetricTarget, error) {
mt.lock.Lock()
defer mt.lock.Unlock()
resp := make([]*controller.PrometheusMetricTarget, 0)
var dbToAdd []*mysql.PrometheusMetricTarget
for _, item := range toAdd {
mn := item.GetMetricName()
ti := int(item.GetTargetId())
if ti == 0 {
ti, _ = mt.targetEncoder.getID(cache.NewTargetKey(item.GetInstance(), item.GetJob(), int(item.GetEpcId()), int(item.GetPodClusterId())))
}
if ti != 0 {
if ok := mt.metricTargets.Contains(cache.NewMetricTargetKey(mn, ti)); ok {
resp = append(resp, &controller.PrometheusMetricTarget{
MetricName: &mn,
TargetId: proto.Uint32(uint32(ti)),
})
continue
}
dbToAdd = append(dbToAdd, &mysql.PrometheusMetricTarget{
MetricName: mn,
TargetID: ti,
})
}
}
err := addBatch(mt.org.DB, dbToAdd, mt.resourceType)
if err != nil {
log.Error(mt.org.Logf("add %s error: %s", mt.resourceType, err.Error()))
return resp, err
}
for _, item := range dbToAdd {
mt.metricTargets.Add(cache.NewMetricTargetKey(item.MetricName, item.TargetID))
resp = append(resp, &controller.PrometheusMetricTarget{
MetricName: &item.MetricName,
TargetId: proto.Uint32(uint32(item.TargetID)),
})
}
return resp, nil
}