github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_metric.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 (
    10  	"github.com/gogf/gf/v2/encoding/gjson"
    11  	"github.com/gogf/gf/v2/errors/gcode"
    12  	"github.com/gogf/gf/v2/errors/gerror"
    13  	"github.com/gogf/gf/v2/text/gregex"
    14  )
    15  
    16  // localMetric implements interface Metric.
    17  type localMetric struct {
    18  	MetricInfo
    19  }
    20  
    21  // newMetric creates and returns an object that implements interface Metric.
    22  func (meter *localMeter) newMetric(
    23  	metricType MetricType, metricName string, metricOption MetricOption,
    24  ) (Metric, error) {
    25  	if metricName == "" {
    26  		return nil, gerror.NewCodef(
    27  			gcode.CodeInvalidParameter,
    28  			`error creating %s metric while given name is empty, option: %s`,
    29  			metricType, gjson.MustEncodeString(metricOption),
    30  		)
    31  	}
    32  	if !gregex.IsMatchString(MetricNamePattern, metricName) {
    33  		return nil, gerror.NewCodef(
    34  			gcode.CodeInvalidParameter,
    35  			`invalid metric name "%s", should match regular expression pattern "%s"`,
    36  			metricName, MetricNamePattern,
    37  		)
    38  	}
    39  	return &localMetric{
    40  		MetricInfo: meter.newMetricInfo(metricType, metricName, metricOption),
    41  	}, nil
    42  }
    43  
    44  // Info returns the basic information of a Metric.
    45  func (l *localMetric) Info() MetricInfo {
    46  	return l.MetricInfo
    47  }