github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/metrics/logging/reporter.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/logging/reporter.go
    14  
    15  package logging
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/tickoalcantara12/micro/v3/service/logger"
    21  	"github.com/tickoalcantara12/micro/v3/service/metrics"
    22  )
    23  
    24  const (
    25  	defaultLoggingLevel = logger.TraceLevel
    26  )
    27  
    28  // Reporter is an implementation of metrics.Reporter:
    29  type Reporter struct {
    30  	options metrics.Options
    31  }
    32  
    33  // New returns a configured logging reporter:
    34  func New(opts ...metrics.Option) *Reporter {
    35  	logger.Logf(logger.InfoLevel, "Metrics/Logging - metrics will be logged (at %s level)", defaultLoggingLevel.String())
    36  
    37  	return &Reporter{
    38  		options: metrics.NewOptions(opts...),
    39  	}
    40  }
    41  
    42  // Count implements the metrics.Reporter interface Count method:
    43  func (r *Reporter) Count(metricName string, value int64, tags metrics.Tags) error {
    44  	logger.Logf(defaultLoggingLevel, "Count metric: (%s: %d) %s", metricName, value, tags)
    45  	return nil
    46  }
    47  
    48  // Gauge implements the metrics.Reporter interface Gauge method:
    49  func (r *Reporter) Gauge(metricName string, value float64, tags metrics.Tags) error {
    50  	logger.Logf(defaultLoggingLevel, "Gauge metric: (%s: %f) %s", metricName, value, tags)
    51  	return nil
    52  }
    53  
    54  // Timing implements the metrics.Reporter interface Timing method:
    55  func (r *Reporter) Timing(metricName string, value time.Duration, tags metrics.Tags) error {
    56  	logger.Logf(defaultLoggingLevel, "Timing metric: (%s: %s) %s", metricName, value.String(), tags)
    57  	return nil
    58  }
    59  
    60  // convertTags turns Tags into prometheus labels:
    61  func convertTags(tags metrics.Tags) map[string]interface{} {
    62  	labels := make(map[string]interface{})
    63  	for key, value := range tags {
    64  		labels[key] = value
    65  	}
    66  	return labels
    67  }