forked from kubewharf/katalyst-core
/
tag_wrapper_metrics.go
87 lines (71 loc) · 2.59 KB
/
tag_wrapper_metrics.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
/*
Copyright 2022 The Katalyst Authors.
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 metrics
import "context"
// MetricTagWrapper is a wrapped implementation for MetricEmitter
// it contains a standard MetricEmitter implementation along with
// pre-defined common metrics tags
type MetricTagWrapper struct {
// unitTag is a special tag to indicate its owner component unit
// commonTags are those tags that should be added by default
unitTag MetricTag
commonTags []MetricTag
MetricEmitter
}
var _ MetricEmitter = &MetricTagWrapper{}
func (t *MetricTagWrapper) StoreInt64(key string, val int64, emitType MetricTypeName, tags ...MetricTag) error {
tags = append(tags, t.commonTags...)
tags = append(tags, t.unitTag)
return t.MetricEmitter.StoreInt64(key, val, emitType, tags...)
}
func (t *MetricTagWrapper) StoreFloat64(key string, val float64, emitType MetricTypeName, tags ...MetricTag) error {
tags = append(tags, t.commonTags...)
tags = append(tags, t.unitTag)
return t.MetricEmitter.StoreFloat64(key, val, emitType, tags...)
}
func (t *MetricTagWrapper) Run(ctx context.Context) {
t.MetricEmitter.Run(ctx)
}
func (t *MetricTagWrapper) WithTags(unit string, commonTags ...MetricTag) MetricEmitter {
return t.deepCopy().withTags(unit, commonTags...)
}
func (t *MetricTagWrapper) withTags(unit string, commonTags ...MetricTag) MetricEmitter {
t.unitTag = MetricTag{
Key: "emmit_unit",
Val: unit,
}
t.addOrUpdateCommonTags(commonTags)
return t
}
func (t *MetricTagWrapper) deepCopy() *MetricTagWrapper {
newMetricTagWrapper := &MetricTagWrapper{MetricEmitter: t.MetricEmitter}
newMetricTagWrapper.unitTag = t.unitTag
newMetricTagWrapper.commonTags = append(newMetricTagWrapper.commonTags, t.commonTags...)
return newMetricTagWrapper
}
// addOrUpdateCommonTags tries to add a tag to common tags list.
func (t *MetricTagWrapper) addOrUpdateCommonTags(tags []MetricTag) {
for _, tag := range tags {
exist := false
for i := range t.commonTags {
if tag.Key == t.commonTags[i].Key {
t.commonTags[i].Val = tag.Val
exist = true
break
}
}
if !exist {
t.commonTags = append(t.commonTags, tag)
}
}
}