github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/sysadvisor/plugin/metric-emitter/metric_emitter.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 metric_emitter 18 19 import ( 20 "context" 21 22 "k8s.io/klog/v2" 23 24 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache" 25 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin" 26 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/metric-emitter/syncer" 27 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/metric-emitter/syncer/external" 28 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/metric-emitter/syncer/node" 29 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/metric-emitter/syncer/pod" 30 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/metric-emitter/types" 31 "github.com/kubewharf/katalyst-core/pkg/config" 32 "github.com/kubewharf/katalyst-core/pkg/metaserver" 33 metricspool "github.com/kubewharf/katalyst-core/pkg/metrics/metrics-pool" 34 ) 35 36 func init() { 37 syncer.RegisterMetricSyncInitializers(types.MetricSyncerNamePod, pod.NewMetricSyncerPod) 38 syncer.RegisterMetricSyncInitializers(types.MetricSyncerNameNode, node.NewMetricSyncerNode) 39 syncer.RegisterMetricSyncInitializers(types.MetricSyncerNameExternal, external.NewMetricSyncerExternal) 40 } 41 42 const PluginNameCustomMetricEmitter = "metric-emitter-plugin" 43 44 type CustomMetricEmitter struct { 45 name string 46 syncers []syncer.CustomMetricSyncer 47 } 48 49 func NewCustomMetricEmitter(pluginName string, conf *config.Configuration, extraConf interface{}, emitterPool metricspool.MetricsEmitterPool, 50 metaServer *metaserver.MetaServer, metaCache metacache.MetaCache, 51 ) (plugin.SysAdvisorPlugin, error) { 52 metricEmitter := emitterPool.GetDefaultMetricsEmitter().WithTags("custom-metric") 53 54 var syncers []syncer.CustomMetricSyncer 55 for _, name := range conf.AgentConfiguration.SysAdvisorPluginsConfiguration.MetricSyncers { 56 if f, ok := syncer.GetRegisteredMetricSyncers()[name]; ok { 57 if s, err := f(conf, extraConf, metricEmitter, emitterPool, metaServer, metaCache); err != nil { 58 return plugin.DummySysAdvisorPlugin{}, err 59 } else { 60 syncers = append(syncers, s) 61 } 62 } 63 } 64 65 return &CustomMetricEmitter{ 66 name: pluginName, 67 syncers: syncers, 68 }, nil 69 } 70 71 func (cme *CustomMetricEmitter) Name() string { 72 return cme.name 73 } 74 75 func (cme *CustomMetricEmitter) Init() error { 76 return nil 77 } 78 79 // Run is the main logic to get metric data from multiple sources 80 // and emit through the standard emit interface. 81 func (cme *CustomMetricEmitter) Run(ctx context.Context) { 82 klog.Info("custom metrics emitter stated") 83 84 for _, s := range cme.syncers { 85 s.Run(ctx) 86 klog.Infof("run metric emitter %v successfully", s.Name()) 87 } 88 klog.Infof("run all metric emitters successfully") 89 <-ctx.Done() 90 }