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