github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_meter_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 // localCounter is the local implements for interface Counter. 10 type localCounter struct { 11 Metric 12 MeterOption 13 MetricOption 14 CounterPerformer 15 } 16 17 var ( 18 // Check the implements for interface MetricInitializer. 19 _ MetricInitializer = (*localCounter)(nil) 20 // Check the implements for interface PerformerExporter. 21 _ PerformerExporter = (*localCounter)(nil) 22 ) 23 24 // Counter creates and returns a new Counter. 25 func (meter *localMeter) Counter(name string, option MetricOption) (Counter, error) { 26 m, err := meter.newMetric(MetricTypeCounter, name, option) 27 if err != nil { 28 return nil, err 29 } 30 counter := &localCounter{ 31 Metric: m, 32 MeterOption: meter.MeterOption, 33 MetricOption: option, 34 CounterPerformer: newNoopCounterPerformer(), 35 } 36 if globalProvider != nil { 37 if err = counter.Init(globalProvider); err != nil { 38 return nil, err 39 } 40 } 41 allMetrics = append(allMetrics, counter) 42 return counter, nil 43 } 44 45 // MustCounter creates and returns a new Counter. 46 // It panics if any error occurs. 47 func (meter *localMeter) MustCounter(name string, option MetricOption) Counter { 48 m, err := meter.Counter(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 *localCounter) Init(provider Provider) (err error) { 57 if _, ok := l.CounterPerformer.(noopCounterPerformer); !ok { 58 // already initialized. 59 return 60 } 61 l.CounterPerformer, err = provider.MeterPerformer(l.MeterOption).CounterPerformer( 62 l.Info().Name(), 63 l.MetricOption, 64 ) 65 return 66 } 67 68 // Performer implements interface PerformerExporter, which exports internal Performer of Metric. 69 // This is usually used by metric implements. 70 func (l *localCounter) Performer() any { 71 return l.CounterPerformer 72 }