github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/metrics/metrics.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package metrics provides utilities for registering client metrics to Prometheus.
    18  package metrics
    19  
    20  import (
    21  	"sync"
    22  	"time"
    23  
    24  	"github.com/prometheus/client_golang/prometheus"
    25  )
    26  
    27  const restClientSubsystem = "rest_client"
    28  
    29  var (
    30  	// RequestLatency is a Prometheus Summary metric type partitioned by
    31  	// "verb" and "url" labels. It is used for the rest client latency metrics.
    32  	RequestLatency = prometheus.NewSummaryVec(
    33  		prometheus.SummaryOpts{
    34  			Subsystem: restClientSubsystem,
    35  			Name:      "request_latency_microseconds",
    36  			Help:      "Request latency in microseconds. Broken down by verb and URL",
    37  			MaxAge:    time.Hour,
    38  		},
    39  		[]string{"verb", "url"},
    40  	)
    41  
    42  	RequestResult = prometheus.NewCounterVec(
    43  		prometheus.CounterOpts{
    44  			Subsystem: restClientSubsystem,
    45  			Name:      "request_status_codes",
    46  			Help:      "Number of http requests, partitioned by metadata",
    47  		},
    48  		[]string{"code", "method", "host"},
    49  	)
    50  )
    51  
    52  var registerMetrics sync.Once
    53  
    54  // Register registers all metrics to Prometheus with
    55  // respect to the RequestLatency.
    56  func Register() {
    57  	// Register the metrics.
    58  	registerMetrics.Do(func() {
    59  		prometheus.MustRegister(RequestLatency)
    60  		prometheus.MustRegister(RequestResult)
    61  	})
    62  }
    63  
    64  // Calculates the time since the specified start in microseconds.
    65  func SinceInMicroseconds(start time.Time) float64 {
    66  	return float64(time.Since(start).Nanoseconds() / time.Microsecond.Nanoseconds())
    67  }