github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_meter_observable_updown_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 // localObservableUpDownCounter is the local implements for interface ObservableUpDownCounter. 10 type localObservableUpDownCounter struct { 11 Metric 12 MeterOption 13 MetricOption 14 ObservableUpDownCounterPerformer 15 } 16 17 var ( 18 // Check the implements for interface MetricInitializer. 19 _ MetricInitializer = (*localObservableUpDownCounter)(nil) 20 // Check the implements for interface PerformerExporter. 21 _ PerformerExporter = (*localObservableUpDownCounter)(nil) 22 ) 23 24 // ObservableUpDownCounter creates and returns a new ObservableUpDownCounter. 25 func (meter *localMeter) ObservableUpDownCounter(name string, option MetricOption) (ObservableUpDownCounter, error) { 26 m, err := meter.newMetric(MetricTypeObservableUpDownCounter, name, option) 27 if err != nil { 28 return nil, err 29 } 30 observableUpDownCounter := &localObservableUpDownCounter{ 31 Metric: m, 32 MeterOption: meter.MeterOption, 33 MetricOption: option, 34 ObservableUpDownCounterPerformer: newNoopObservableUpDownCounterPerformer(), 35 } 36 if globalProvider != nil { 37 if err = observableUpDownCounter.Init(globalProvider); err != nil { 38 return nil, err 39 } 40 } 41 allMetrics = append(allMetrics, observableUpDownCounter) 42 return observableUpDownCounter, nil 43 } 44 45 // MustObservableUpDownCounter creates and returns a new ObservableUpDownCounter. 46 // It panics if any error occurs. 47 func (meter *localMeter) MustObservableUpDownCounter(name string, option MetricOption) ObservableUpDownCounter { 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 *localObservableUpDownCounter) Init(provider Provider) (err error) { 57 if _, ok := l.ObservableUpDownCounterPerformer.(noopObservableUpDownCounterPerformer); !ok { 58 // already initialized. 59 return 60 } 61 l.ObservableUpDownCounterPerformer, err = provider.MeterPerformer(l.MeterOption).ObservableUpDownCounterPerformer( 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 *localObservableUpDownCounter) Performer() any { 71 return l.ObservableUpDownCounterPerformer 72 }