go.temporal.io/server@v1.23.0/common/sdk/metrics_handler.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package sdk
    26  
    27  import (
    28  	"time"
    29  
    30  	sdkclient "go.temporal.io/sdk/client"
    31  
    32  	"go.temporal.io/server/common/metrics"
    33  )
    34  
    35  type (
    36  	MetricsHandler struct {
    37  		provider metrics.Handler
    38  	}
    39  
    40  	metricsCounter struct {
    41  		name     string
    42  		provider metrics.Handler
    43  	}
    44  
    45  	metricsGauge struct {
    46  		name     string
    47  		provider metrics.Handler
    48  	}
    49  
    50  	metricsTimer struct {
    51  		name     string
    52  		provider metrics.Handler
    53  	}
    54  )
    55  
    56  var _ sdkclient.MetricsHandler = &MetricsHandler{}
    57  
    58  func NewMetricsHandler(provider metrics.Handler) *MetricsHandler {
    59  	return &MetricsHandler{provider: provider}
    60  }
    61  
    62  func (m *MetricsHandler) WithTags(tags map[string]string) sdkclient.MetricsHandler {
    63  	t := make([]metrics.Tag, 0, len(tags))
    64  	for k, v := range tags {
    65  		t = append(t, metrics.StringTag(k, v))
    66  	}
    67  
    68  	return NewMetricsHandler(m.provider.WithTags(t...))
    69  }
    70  
    71  func (m *MetricsHandler) Counter(name string) sdkclient.MetricsCounter {
    72  	return &metricsCounter{name: name, provider: m.provider}
    73  }
    74  
    75  func (m *MetricsHandler) Gauge(name string) sdkclient.MetricsGauge {
    76  	return &metricsGauge{name: name, provider: m.provider}
    77  }
    78  
    79  func (m *MetricsHandler) Timer(name string) sdkclient.MetricsTimer {
    80  	return &metricsTimer{name: name, provider: m.provider}
    81  }
    82  
    83  func (m metricsCounter) Inc(i int64) {
    84  	m.provider.Counter(m.name).Record(i)
    85  }
    86  
    87  func (m metricsGauge) Update(f float64) {
    88  	m.provider.Gauge(m.name).Record(f)
    89  }
    90  
    91  func (m metricsTimer) Record(duration time.Duration) {
    92  	m.provider.Timer(m.name).Record(duration)
    93  }