github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_meter_observable_counter.go (about) 1 // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gmetric 8 9 // localObservableCounter is the local implements for interface ObservableCounter. 10 type localObservableCounter struct { 11 Metric 12 MeterOption 13 MetricOption 14 ObservableCounterPerformer 15 } 16 17 var ( 18 // Check the implements for interface MetricInitializer. 19 _ MetricInitializer = (*localObservableCounter)(nil) 20 // Check the implements for interface PerformerExporter. 21 _ PerformerExporter = (*localObservableCounter)(nil) 22 ) 23 24 // ObservableCounter creates and returns a new ObservableCounter. 25 func (meter *localMeter) ObservableCounter(name string, option MetricOption) (ObservableCounter, error) { 26 m, err := meter.newMetric(MetricTypeObservableCounter, name, option) 27 if err != nil { 28 return nil, err 29 } 30 observableCounter := &localObservableCounter{ 31 Metric: m, 32 MeterOption: meter.MeterOption, 33 MetricOption: option, 34 ObservableCounterPerformer: newNoopObservableCounterPerformer(), 35 } 36 if globalProvider != nil { 37 if err = observableCounter.Init(globalProvider); err != nil { 38 return nil, err 39 } 40 } 41 allMetrics = append(allMetrics, observableCounter) 42 return observableCounter, nil 43 } 44 45 // MustObservableCounter creates and returns a new ObservableCounter. 46 // It panics if any error occurs. 47 func (meter *localMeter) MustObservableCounter(name string, option MetricOption) ObservableCounter { 48 m, err := meter.ObservableCounter(name, option) 49 if err != nil { 50 panic(err) 51 } 52 return m 53 } 54 55 // Init initializes the Metric in Provider creation. 56 func (l *localObservableCounter) Init(provider Provider) (err error) { 57 if _, ok := l.ObservableCounterPerformer.(noopObservableCounterPerformer); !ok { 58 // already initialized. 59 return 60 } 61 l.ObservableCounterPerformer, err = provider.MeterPerformer(l.MeterOption).ObservableCounterPerformer( 62 l.Info().Name(), 63 l.MetricOption, 64 ) 65 return err 66 } 67 68 // Performer implements interface PerformerExporter, which exports internal Performer of Metric. 69 // This is usually used by metric implements. 70 func (l *localObservableCounter) Performer() any { 71 return l.ObservableCounterPerformer 72 }