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