|
|
7999a9c |
diff --git a/prometheus/promhttp/instrument_client_1_8_test.go b/prometheus/promhttp/instrument_client_1_8_test.go
|
|
|
7999a9c |
deleted file mode 100644
|
|
|
7999a9c |
index 7e3f522..0000000
|
|
|
7999a9c |
--- a/prometheus/promhttp/instrument_client_1_8_test.go
|
|
|
7999a9c |
+++ /dev/null
|
|
|
7999a9c |
@@ -1,195 +0,0 @@
|
|
|
7999a9c |
-// Copyright 2017 The Prometheus Authors
|
|
|
7999a9c |
-// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
7999a9c |
-// you may not use this file except in compliance with the License.
|
|
|
7999a9c |
-// You may obtain a copy of the License at
|
|
|
7999a9c |
-//
|
|
|
7999a9c |
-// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
7999a9c |
-//
|
|
|
7999a9c |
-// Unless required by applicable law or agreed to in writing, software
|
|
|
7999a9c |
-// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
7999a9c |
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
7999a9c |
-// See the License for the specific language governing permissions and
|
|
|
7999a9c |
-// limitations under the License.
|
|
|
7999a9c |
-
|
|
|
7999a9c |
-// +build go1.8
|
|
|
7999a9c |
-
|
|
|
7999a9c |
-package promhttp
|
|
|
7999a9c |
-
|
|
|
7999a9c |
-import (
|
|
|
7999a9c |
- "log"
|
|
|
7999a9c |
- "net/http"
|
|
|
7999a9c |
- "testing"
|
|
|
7999a9c |
- "time"
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- "github.com/prometheus/client_golang/prometheus"
|
|
|
7999a9c |
-)
|
|
|
7999a9c |
-
|
|
|
7999a9c |
-func TestClientMiddlewareAPI(t *testing.T) {
|
|
|
7999a9c |
- client := http.DefaultClient
|
|
|
7999a9c |
- client.Timeout = 1 * time.Second
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- reg := prometheus.NewRegistry()
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- inFlightGauge := prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
7999a9c |
- Name: "client_in_flight_requests",
|
|
|
7999a9c |
- Help: "A gauge of in-flight requests for the wrapped client.",
|
|
|
7999a9c |
- })
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- counter := prometheus.NewCounterVec(
|
|
|
7999a9c |
- prometheus.CounterOpts{
|
|
|
7999a9c |
- Name: "client_api_requests_total",
|
|
|
7999a9c |
- Help: "A counter for requests from the wrapped client.",
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- []string{"code", "method"},
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- dnsLatencyVec := prometheus.NewHistogramVec(
|
|
|
7999a9c |
- prometheus.HistogramOpts{
|
|
|
7999a9c |
- Name: "dns_duration_seconds",
|
|
|
7999a9c |
- Help: "Trace dns latency histogram.",
|
|
|
7999a9c |
- Buckets: []float64{.005, .01, .025, .05},
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- []string{"event"},
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- tlsLatencyVec := prometheus.NewHistogramVec(
|
|
|
7999a9c |
- prometheus.HistogramOpts{
|
|
|
7999a9c |
- Name: "tls_duration_seconds",
|
|
|
7999a9c |
- Help: "Trace tls latency histogram.",
|
|
|
7999a9c |
- Buckets: []float64{.05, .1, .25, .5},
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- []string{"event"},
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- histVec := prometheus.NewHistogramVec(
|
|
|
7999a9c |
- prometheus.HistogramOpts{
|
|
|
7999a9c |
- Name: "request_duration_seconds",
|
|
|
7999a9c |
- Help: "A histogram of request latencies.",
|
|
|
7999a9c |
- Buckets: prometheus.DefBuckets,
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- []string{"method"},
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- reg.MustRegister(counter, tlsLatencyVec, dnsLatencyVec, histVec, inFlightGauge)
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- trace := &InstrumentTrace{
|
|
|
7999a9c |
- DNSStart: func(t float64) {
|
|
|
7999a9c |
- dnsLatencyVec.WithLabelValues("dns_start")
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- DNSDone: func(t float64) {
|
|
|
7999a9c |
- dnsLatencyVec.WithLabelValues("dns_done")
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- TLSHandshakeStart: func(t float64) {
|
|
|
7999a9c |
- tlsLatencyVec.WithLabelValues("tls_handshake_start")
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- TLSHandshakeDone: func(t float64) {
|
|
|
7999a9c |
- tlsLatencyVec.WithLabelValues("tls_handshake_done")
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- }
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- client.Transport = InstrumentRoundTripperInFlight(inFlightGauge,
|
|
|
7999a9c |
- InstrumentRoundTripperCounter(counter,
|
|
|
7999a9c |
- InstrumentRoundTripperTrace(trace,
|
|
|
7999a9c |
- InstrumentRoundTripperDuration(histVec, http.DefaultTransport),
|
|
|
7999a9c |
- ),
|
|
|
7999a9c |
- ),
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- resp, err := client.Get("http://google.com")
|
|
|
7999a9c |
- if err != nil {
|
|
|
7999a9c |
- t.Fatalf("%v", err)
|
|
|
7999a9c |
- }
|
|
|
7999a9c |
- defer resp.Body.Close()
|
|
|
7999a9c |
-}
|
|
|
7999a9c |
-
|
|
|
7999a9c |
-func ExampleInstrumentRoundTripperDuration() {
|
|
|
7999a9c |
- client := http.DefaultClient
|
|
|
7999a9c |
- client.Timeout = 1 * time.Second
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- inFlightGauge := prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
7999a9c |
- Name: "client_in_flight_requests",
|
|
|
7999a9c |
- Help: "A gauge of in-flight requests for the wrapped client.",
|
|
|
7999a9c |
- })
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- counter := prometheus.NewCounterVec(
|
|
|
7999a9c |
- prometheus.CounterOpts{
|
|
|
7999a9c |
- Name: "client_api_requests_total",
|
|
|
7999a9c |
- Help: "A counter for requests from the wrapped client.",
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- []string{"code", "method"},
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- // dnsLatencyVec uses custom buckets based on expected dns durations.
|
|
|
7999a9c |
- // It has an instance label "event", which is set in the
|
|
|
7999a9c |
- // DNSStart and DNSDonehook functions defined in the
|
|
|
7999a9c |
- // InstrumentTrace struct below.
|
|
|
7999a9c |
- dnsLatencyVec := prometheus.NewHistogramVec(
|
|
|
7999a9c |
- prometheus.HistogramOpts{
|
|
|
7999a9c |
- Name: "dns_duration_seconds",
|
|
|
7999a9c |
- Help: "Trace dns latency histogram.",
|
|
|
7999a9c |
- Buckets: []float64{.005, .01, .025, .05},
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- []string{"event"},
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- // tlsLatencyVec uses custom buckets based on expected tls durations.
|
|
|
7999a9c |
- // It has an instance label "event", which is set in the
|
|
|
7999a9c |
- // TLSHandshakeStart and TLSHandshakeDone hook functions defined in the
|
|
|
7999a9c |
- // InstrumentTrace struct below.
|
|
|
7999a9c |
- tlsLatencyVec := prometheus.NewHistogramVec(
|
|
|
7999a9c |
- prometheus.HistogramOpts{
|
|
|
7999a9c |
- Name: "tls_duration_seconds",
|
|
|
7999a9c |
- Help: "Trace tls latency histogram.",
|
|
|
7999a9c |
- Buckets: []float64{.05, .1, .25, .5},
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- []string{"event"},
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- // histVec has no labels, making it a zero-dimensional ObserverVec.
|
|
|
7999a9c |
- histVec := prometheus.NewHistogramVec(
|
|
|
7999a9c |
- prometheus.HistogramOpts{
|
|
|
7999a9c |
- Name: "request_duration_seconds",
|
|
|
7999a9c |
- Help: "A histogram of request latencies.",
|
|
|
7999a9c |
- Buckets: prometheus.DefBuckets,
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- []string{},
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- // Register all of the metrics in the standard registry.
|
|
|
7999a9c |
- prometheus.MustRegister(counter, tlsLatencyVec, dnsLatencyVec, histVec, inFlightGauge)
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- // Define functions for the available httptrace.ClientTrace hook
|
|
|
7999a9c |
- // functions that we want to instrument.
|
|
|
7999a9c |
- trace := &InstrumentTrace{
|
|
|
7999a9c |
- DNSStart: func(t float64) {
|
|
|
7999a9c |
- dnsLatencyVec.WithLabelValues("dns_start")
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- DNSDone: func(t float64) {
|
|
|
7999a9c |
- dnsLatencyVec.WithLabelValues("dns_done")
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- TLSHandshakeStart: func(t float64) {
|
|
|
7999a9c |
- tlsLatencyVec.WithLabelValues("tls_handshake_start")
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- TLSHandshakeDone: func(t float64) {
|
|
|
7999a9c |
- tlsLatencyVec.WithLabelValues("tls_handshake_done")
|
|
|
7999a9c |
- },
|
|
|
7999a9c |
- }
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- // Wrap the default RoundTripper with middleware.
|
|
|
7999a9c |
- roundTripper := InstrumentRoundTripperInFlight(inFlightGauge,
|
|
|
7999a9c |
- InstrumentRoundTripperCounter(counter,
|
|
|
7999a9c |
- InstrumentRoundTripperTrace(trace,
|
|
|
7999a9c |
- InstrumentRoundTripperDuration(histVec, http.DefaultTransport),
|
|
|
7999a9c |
- ),
|
|
|
7999a9c |
- ),
|
|
|
7999a9c |
- )
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- // Set the RoundTripper on our client.
|
|
|
7999a9c |
- client.Transport = roundTripper
|
|
|
7999a9c |
-
|
|
|
7999a9c |
- resp, err := client.Get("http://google.com")
|
|
|
7999a9c |
- if err != nil {
|
|
|
7999a9c |
- log.Printf("error: %v", err)
|
|
|
7999a9c |
- }
|
|
|
7999a9c |
- defer resp.Body.Close()
|
|
|
7999a9c |
-}
|