github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/metrics/metrics.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/tickoalcantara12/micro/v3/metrics/go
    14  
    15  // Package metrics is for instrumentation and debugging
    16  package metrics
    17  
    18  import (
    19  	"sync"
    20  	"time"
    21  )
    22  
    23  // Tags is a map of fields to add to a metric:
    24  type Tags map[string]string
    25  
    26  // Reporter is an interface for collecting and instrumenting metrics
    27  type Reporter interface {
    28  	Count(id string, value int64, tags Tags) error
    29  	Gauge(id string, value float64, tags Tags) error
    30  	Timing(id string, value time.Duration, tags Tags) error
    31  }
    32  
    33  var (
    34  	// DefaultMetricsReporter implementation
    35  	DefaultMetricsReporter Reporter
    36  	initialised            bool = false
    37  	initialisedMutex       sync.Mutex
    38  )
    39  
    40  // IsSet lets you know if the DefaultMetricsReporter has been set already:
    41  func IsSet() bool {
    42  	initialisedMutex.Lock()
    43  	defer initialisedMutex.Unlock()
    44  	return initialised
    45  }
    46  
    47  // SetDefaultMetricsReporter allows other packages (such as profiles) to set the DefaultMetricsReporter
    48  // The "initialised" flag prevents this from being overwritten (because other packages may already be using it)
    49  func SetDefaultMetricsReporter(defaultReporter Reporter) {
    50  	initialisedMutex.Lock()
    51  	defer initialisedMutex.Unlock()
    52  	if !initialised {
    53  		DefaultMetricsReporter = defaultReporter
    54  		initialised = true
    55  	}
    56  }
    57  
    58  // Count submits a counter metric using the DefaultMetricsReporter:
    59  func Count(id string, value int64, tags Tags) error {
    60  	return DefaultMetricsReporter.Count(id, value, tags)
    61  }
    62  
    63  // Gauge submits a gauge metric using the DefaultMetricsReporter:
    64  func Gauge(id string, value float64, tags Tags) error {
    65  	return DefaultMetricsReporter.Gauge(id, value, tags)
    66  }
    67  
    68  // Timing submits a timing metric using the DefaultMetricsReporter:
    69  func Timing(id string, value time.Duration, tags Tags) error {
    70  	return DefaultMetricsReporter.Timing(id, value, tags)
    71  }