Skip to content

Commit

Permalink
Prometheus client: fix lock copy + add traces
Browse files Browse the repository at this point in the history
- Do not copy prom's default round tripper as it contains a lock
- Add trace-level logs on prom queries
  • Loading branch information
jotak committed Feb 2, 2021
1 parent a17911e commit a1426f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 11 additions & 2 deletions prometheus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prometheus
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -84,9 +85,17 @@ func NewClientForConfig(cfg config.PrometheusConfig) (*Client, error) {
}

// make a copy of the prometheus DefaultRoundTripper to avoid race condition (issue #3518)
defaultRoundTripper := *api.DefaultRoundTripper.(*http.Transport)
// Do not copy the struct itself, it contains a lock. Re-create it from scratch instead.
roundTripper := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
}

transportConfig, err := httputil.CreateTransport(&auth, &defaultRoundTripper, httputil.DefaultTimeout)
transportConfig, err := httputil.CreateTransport(&auth, roundTripper, httputil.DefaultTimeout)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions prometheus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func fetchHistogramValues(ctx context.Context, api prom_v1.API, metricName, labe
queries := buildHistogramQueries(metricName, labels, grouping, rateInterval, avg, quantiles)
histogram := make(map[string]model.Vector, len(queries))
for k, query := range queries {
log.Tracef("fetchHistogramValues: %s", query)
result, warnings, err := api.Query(ctx, query, queryTime)
if warnings != nil && len(warnings) > 0 {
log.Warningf("fetchHistogramValues. Prometheus Warnings: [%s]", strings.Join(warnings, ","))
Expand Down Expand Up @@ -93,6 +94,7 @@ func buildHistogramQueries(metricName, labels, grouping, rateInterval string, av
}

func fetchRange(ctx context.Context, api prom_v1.API, query string, bounds prom_v1.Range) Metric {
log.Tracef("fetchRange: %s", query)
result, warnings, err := api.QueryRange(ctx, query, bounds)
if warnings != nil && len(warnings) > 0 {
log.Warningf("fetchRange. Prometheus Warnings: [%s]", strings.Join(warnings, ","))
Expand Down Expand Up @@ -172,6 +174,7 @@ func getItemRequestRates(ctx context.Context, api prom_v1.API, namespace, item,

func getRequestRatesForLabel(ctx context.Context, api prom_v1.API, time time.Time, labels, ratesInterval string) (model.Vector, error) {
query := fmt.Sprintf("rate(istio_requests_total{%s}[%s]) > 0", labels, ratesInterval)
log.Tracef("getRequestRatesForLabel: %s", query)
promtimer := internalmetrics.GetPrometheusProcessingTimePrometheusTimer("Metrics-GetRequestRates")
result, warnings, err := api.Query(ctx, query, time)
if warnings != nil && len(warnings) > 0 {
Expand Down

0 comments on commit a1426f1

Please sign in to comment.