github.com/kubewharf/katalyst-core@v0.5.3/pkg/metrics/metrics-pool/custom_metrics_pool.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_pool
    18  
    19  import (
    20  	"context"
    21  
    22  	"go.uber.org/atomic"
    23  
    24  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    25  )
    26  
    27  // customMetricsEmitterPool registers itself as an implementation
    28  // metrics.MetricEmitter, so that whenever SetDefaultMetricsEmitter is called,
    29  // it will always work the newly set MetricEmitter as default implementation.
    30  type customMetricsEmitterPool struct {
    31  	started                    *atomic.Bool
    32  	emitterPool                MetricsEmitterPool
    33  	customDefaultMetricEmitter metrics.MetricEmitter
    34  }
    35  
    36  var _ MetricsEmitterPool = &customMetricsEmitterPool{}
    37  
    38  func NewCustomMetricsEmitterPool(emitterPool MetricsEmitterPool) MetricsEmitterPool {
    39  	return &customMetricsEmitterPool{
    40  		started:                    atomic.NewBool(false),
    41  		emitterPool:                emitterPool,
    42  		customDefaultMetricEmitter: emitterPool.GetDefaultMetricsEmitter(),
    43  	}
    44  }
    45  
    46  // GetDefaultMetricsEmitter return custom wrapped metric emitter
    47  func (p *customMetricsEmitterPool) GetDefaultMetricsEmitter() metrics.MetricEmitter {
    48  	return p
    49  }
    50  
    51  func (p *customMetricsEmitterPool) SetDefaultMetricsEmitter(metricEmitter metrics.MetricEmitter) {
    52  	if p.started.Load() {
    53  		return
    54  	}
    55  
    56  	p.customDefaultMetricEmitter = metricEmitter
    57  }
    58  
    59  func (p *customMetricsEmitterPool) GetMetricsEmitter(parameters interface{}) (metrics.MetricEmitter, error) {
    60  	return p.emitterPool.GetMetricsEmitter(parameters)
    61  }
    62  
    63  func (p *customMetricsEmitterPool) Run(ctx context.Context) {
    64  	if p.started.Swap(true) {
    65  		return
    66  	}
    67  
    68  	p.customDefaultMetricEmitter.Run(ctx)
    69  	p.emitterPool.Run(ctx)
    70  }
    71  
    72  func (p *customMetricsEmitterPool) StoreInt64(key string, val int64,
    73  	emitType metrics.MetricTypeName, tags ...metrics.MetricTag,
    74  ) error {
    75  	return p.customDefaultMetricEmitter.StoreInt64(key, val,
    76  		emitType, tags...)
    77  }
    78  
    79  func (p *customMetricsEmitterPool) StoreFloat64(key string, val float64,
    80  	emitType metrics.MetricTypeName, tags ...metrics.MetricTag,
    81  ) error {
    82  	return p.customDefaultMetricEmitter.StoreFloat64(key, val,
    83  		emitType, tags...)
    84  }
    85  
    86  func (p *customMetricsEmitterPool) WithTags(unit string,
    87  	commonTags ...metrics.MetricTag,
    88  ) metrics.MetricEmitter {
    89  	return p.customDefaultMetricEmitter.WithTags(unit,
    90  		commonTags...)
    91  }