github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/quota/metrics.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package quota
    16  
    17  import (
    18  	"fmt"
    19  	"sync"
    20  
    21  	"github.com/google/trillian/monitoring"
    22  )
    23  
    24  var (
    25  
    26  	// Metrics groups all quota-related metrics.
    27  	// The metrics represented here are not meant to be maintained by the quota subsystem
    28  	// implementation.  Instead, they're meant to be updated by the quota's callers, in order to
    29  	// record their interactions with quotas.
    30  	// The quota implementation is encouraged to define its own metrics to monitor its internal
    31  	// state.
    32  	Metrics     = &m{}
    33  	metricsOnce = sync.Once{}
    34  )
    35  
    36  type m struct {
    37  	AcquiredTokens    monitoring.Counter
    38  	ReturnedTokens    monitoring.Counter
    39  	ReplenishedTokens monitoring.Counter
    40  }
    41  
    42  // IncAcquired increments the AcquiredTokens metric.
    43  func (m *m) IncAcquired(tokens int, specs []Spec, success bool) {
    44  	m.add(m.AcquiredTokens, tokens, specs, success)
    45  }
    46  
    47  // IncReturned increments the ReturnedTokens metric.
    48  func (m *m) IncReturned(tokens int, specs []Spec, success bool) {
    49  	m.add(m.ReturnedTokens, tokens, specs, success)
    50  }
    51  
    52  // IncReplenished increments the ReplenishedTokens metric.
    53  func (m *m) IncReplenished(tokens int, specs []Spec, success bool) {
    54  	m.add(m.ReplenishedTokens, tokens, specs, success)
    55  }
    56  
    57  func (m *m) add(c monitoring.Counter, tokens int, specs []Spec, success bool) {
    58  	if c == nil {
    59  		return
    60  	}
    61  	for _, spec := range specs {
    62  		c.Add(float64(tokens), spec.Name(), fmt.Sprint(success))
    63  	}
    64  }
    65  
    66  // InitMetrics initializes Metrics using mf to create the monitoring objects.
    67  // May be called multiple times. If so, the first call is the one that counts.
    68  func InitMetrics(mf monitoring.MetricFactory) {
    69  	metricsOnce.Do(func() {
    70  		Metrics.AcquiredTokens = mf.NewCounter("quota_acquired_tokens", "Number of acquired quota tokens", "spec", "success")
    71  		Metrics.ReturnedTokens = mf.NewCounter("quota_returned_tokens", "Number of quota tokens returned due to overcharging (bad requests, duplicates, etc)", "spec", "success")
    72  		Metrics.ReplenishedTokens = mf.NewCounter("quota_replenished_tokens", "Number of quota tokens replenished due to sequencer progress", "spec", "success")
    73  	})
    74  }