github.com/kubewharf/katalyst-core@v0.5.3/pkg/metrics/metrics.go (about) 1 /* 2 Copyright 2022 The Katalyst Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package metrics is the package that contains those implementations to 18 // emit metrics to reflect the running states of current process. 19 package metrics // import "github.com/kubewharf/katalyst-core/pkg/metrics" 20 21 import "context" 22 23 type MetricTypeName string 24 25 const ( 26 // The MetricTypeNameRaw is stateless, the MetricTypeNameCount and MetricTypeNameUpDownCount 27 // are stateful, so when we need store metrics with high dimension tags (such as pod name or uuid), 28 // we must use MetricTypeNameRaw to avoid oom. 29 // MetricTypeNameRaw emit raw metrics which report the last value 30 MetricTypeNameRaw MetricTypeName = "raw" 31 // MetricTypeNameCount emit counter metrics which is monotonic 32 MetricTypeNameCount MetricTypeName = "count" 33 // MetricTypeNameUpDownCount emit up down count metrics which isn't monotonic 34 MetricTypeNameUpDownCount MetricTypeName = "up_down_count" 35 ) 36 37 type MetricTag struct { 38 Key, Val string 39 } 40 41 // MetricEmitter interface defines the action of emitting metrics, 42 // support to use different kinds of metrics emitter if needed 43 type MetricEmitter interface { 44 // StoreInt64 receives the given int64 metrics item and sends it the backend store. 45 StoreInt64(key string, val int64, emitType MetricTypeName, tags ...MetricTag) error 46 // StoreFloat64 receives the given float64 metrics item and sends it the backend store. 47 StoreFloat64(key string, val float64, emitType MetricTypeName, tags ...MetricTag) error 48 // WithTags add unit tag and common tags to emitter. 49 WithTags(unit string, commonTags ...MetricTag) MetricEmitter 50 // Run is ensure the starting logic works, since emitter like 51 // prometheus need to be started to trigger gc logic 52 Run(ctx context.Context) 53 } 54 55 type DummyMetrics struct{} 56 57 func (d DummyMetrics) StoreInt64(_ string, _ int64, _ MetricTypeName, _ ...MetricTag) error { 58 return nil 59 } 60 61 func (d DummyMetrics) StoreFloat64(_ string, _ float64, _ MetricTypeName, _ ...MetricTag) error { 62 return nil 63 } 64 65 func (d DummyMetrics) WithTags(unit string, commonTags ...MetricTag) MetricEmitter { 66 newMetricTagWrapper := &MetricTagWrapper{MetricEmitter: d} 67 return newMetricTagWrapper.WithTags(unit, commonTags...) 68 } 69 func (d DummyMetrics) Run(_ context.Context) {} 70 71 var _ MetricEmitter = DummyMetrics{} 72 73 // ConvertMapToTags only pass map to metrics related function 74 func ConvertMapToTags(tags map[string]string) []MetricTag { 75 res := make([]MetricTag, 0, len(tags)) 76 for k, v := range tags { 77 res = append(res, MetricTag{Key: k, Val: v}) 78 } 79 return res 80 }