github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_meter_metric_info.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  import "fmt"
    10  
    11  // localMetricInfo implements interface MetricInfo.
    12  type localMetricInfo struct {
    13  	MetricType
    14  	MetricOption
    15  	InstrumentInfo
    16  	MetricName string
    17  }
    18  
    19  // newMetricInfo creates and returns a MetricInfo.
    20  func (meter *localMeter) newMetricInfo(
    21  	metricType MetricType, metricName string, metricOption MetricOption,
    22  ) MetricInfo {
    23  	return &localMetricInfo{
    24  		MetricName:     metricName,
    25  		MetricType:     metricType,
    26  		MetricOption:   metricOption,
    27  		InstrumentInfo: meter.newInstrumentInfo(),
    28  	}
    29  }
    30  
    31  // Name returns the name of the metric.
    32  func (l *localMetricInfo) Name() string {
    33  	return l.MetricName
    34  }
    35  
    36  // Help returns the help description of the metric.
    37  func (l *localMetricInfo) Help() string {
    38  	return l.MetricOption.Help
    39  }
    40  
    41  // Unit returns the unit name of the metric.
    42  func (l *localMetricInfo) Unit() string {
    43  	return l.MetricOption.Unit
    44  }
    45  
    46  // Type returns the type of the metric.
    47  func (l *localMetricInfo) Type() MetricType {
    48  	return l.MetricType
    49  }
    50  
    51  // Attributes returns the constant attribute slice of the metric.
    52  func (l *localMetricInfo) Attributes() Attributes {
    53  	return l.MetricOption.Attributes
    54  }
    55  
    56  // Instrument returns the instrument info of the metric.
    57  func (l *localMetricInfo) Instrument() InstrumentInfo {
    58  	return l.InstrumentInfo
    59  }
    60  
    61  func (l *localMetricInfo) Key() string {
    62  	if l.Instrument().Name() != "" && l.Instrument().Version() != "" {
    63  		return fmt.Sprintf(
    64  			`%s@%s:%s`,
    65  			l.Instrument().Name(),
    66  			l.Instrument().Version(),
    67  			l.Name(),
    68  		)
    69  	}
    70  	if l.Instrument().Name() != "" && l.Instrument().Version() == "" {
    71  		return fmt.Sprintf(
    72  			`%s:%s`,
    73  			l.Instrument().Name(),
    74  			l.Name(),
    75  		)
    76  	}
    77  	return l.Name()
    78  }