Skip to content

Commit 9a7773f

Browse files
Merge pull request #9 from williamchanrico/fix_pkgs
Re-use http.Transport Scrape() calls
2 parents 8835946 + dc7d12a commit 9a7773f

File tree

3 files changed

+86
-23
lines changed

3 files changed

+86
-23
lines changed

collector/task/darkstat/darkstat.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ package darkstat
1616

1717
import (
1818
"context"
19+
"crypto/tls"
1920
"fmt"
2021
"net"
22+
"net/http"
2123
"planet-exporter/collector/task/inventory"
2224
"planet-exporter/pkg/network"
2325
"planet-exporter/pkg/prometheus"
@@ -31,8 +33,9 @@ import (
3133

3234
// task that queries darkstat metrics and aggregates them into usable planet metrics
3335
type task struct {
34-
enabled bool
35-
darkstatAddr string
36+
enabled bool
37+
darkstatAddr string
38+
prometheusClient *prometheus.Client
3639

3740
hosts []Metric
3841
mu sync.Mutex
@@ -44,10 +47,24 @@ var (
4447
)
4548

4649
func init() {
50+
httpTransport := &http.Transport{
51+
DialContext: (&net.Dialer{
52+
Timeout: 30 * time.Second,
53+
KeepAlive: 30 * time.Second,
54+
}).DialContext,
55+
ForceAttemptHTTP2: true,
56+
MaxIdleConns: 100,
57+
IdleConnTimeout: 90 * time.Second,
58+
TLSHandshakeTimeout: 10 * time.Second,
59+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
60+
ExpectContinueTimeout: 1 * time.Second,
61+
}
62+
4763
singleton = task{
48-
enabled: false,
49-
hosts: []Metric{},
50-
mu: sync.Mutex{},
64+
enabled: false,
65+
hosts: []Metric{},
66+
mu: sync.Mutex{},
67+
prometheusClient: prometheus.New(httpTransport),
5168
}
5269
}
5370

@@ -91,6 +108,9 @@ func Collect(ctx context.Context) error {
91108

92109
startTime := time.Now()
93110

111+
ctxCollect, ctxCollectCancel := context.WithCancel(ctx)
112+
defer ctxCollectCancel()
113+
94114
inventoryHosts := inventory.Get()
95115

96116
localAddr, err := network.DefaultLocalAddr()
@@ -109,7 +129,7 @@ func Collect(ctx context.Context) error {
109129

110130
// Scrape darkstat prometheus endpoint for host_bytes_total
111131
var darkstatHostBytesTotal *prom2json.Family
112-
darkstatScrape, err := prometheus.Scrape(singleton.darkstatAddr)
132+
darkstatScrape, err := singleton.prometheusClient.Scrape(ctxCollect, singleton.darkstatAddr)
113133
if err != nil {
114134
return err
115135
}

collector/task/ebpf/tcptop.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package ebpf
1818

1919
import (
2020
"context"
21+
"crypto/tls"
2122
"fmt"
2223
"net"
24+
"net/http"
2325
"planet-exporter/collector/task/inventory"
2426
"planet-exporter/pkg/network"
2527
"planet-exporter/pkg/prometheus"
@@ -33,8 +35,9 @@ import (
3335

3436
// task that queries ebpf metrics and aggregates them into usable planet metrics.
3537
type task struct {
36-
enabled bool
37-
ebpfAddr string
38+
enabled bool
39+
ebpfAddr string
40+
prometheusClient *prometheus.Client
3841

3942
hosts []Metric
4043
mu sync.Mutex
@@ -53,10 +56,24 @@ const (
5356
)
5457

5558
func init() {
59+
httpTransport := &http.Transport{
60+
DialContext: (&net.Dialer{
61+
Timeout: 30 * time.Second,
62+
KeepAlive: 30 * time.Second,
63+
}).DialContext,
64+
ForceAttemptHTTP2: true,
65+
MaxIdleConns: 100,
66+
IdleConnTimeout: 90 * time.Second,
67+
TLSHandshakeTimeout: 10 * time.Second,
68+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
69+
ExpectContinueTimeout: 1 * time.Second,
70+
}
71+
5672
singleton = task{
57-
enabled: false,
58-
hosts: []Metric{},
59-
mu: sync.Mutex{},
73+
enabled: false,
74+
hosts: []Metric{},
75+
mu: sync.Mutex{},
76+
prometheusClient: prometheus.New(httpTransport),
6077
}
6178
}
6279

@@ -100,8 +117,11 @@ func Collect(ctx context.Context) error {
100117

101118
startTime := time.Now()
102119

120+
ctxCollect, ctxCollectCancel := context.WithCancel(ctx)
121+
defer ctxCollectCancel()
122+
103123
// Scrape ebpf prometheus endpoint for send_bytes_metric and recv_bytes_metric.
104-
ebpfScrape, err := prometheus.Scrape(singleton.ebpfAddr)
124+
ebpfScrape, err := singleton.prometheusClient.Scrape(ctxCollect, singleton.ebpfAddr)
105125
if err != nil {
106126
return err
107127
}

pkg/prometheus/prometheus.go

+34-11
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package prometheus
1616

1717
import (
18+
"context"
1819
"crypto/tls"
20+
"net"
1921
"net/http"
22+
"time"
2023

2124
dto "github.com/prometheus/client_model/go"
2225
"github.com/prometheus/prom2json"
@@ -25,17 +28,44 @@ import (
2528
// TODO: Complete package
2629
// e.g. abstract prom2json data structures and maybe share http client
2730

28-
// Scrape metrics from a prometheus HTTP endpoint
29-
func Scrape(url string) ([]*prom2json.Family, error) {
31+
// Client for Prometheus endpoints
32+
type Client struct {
33+
httpTransport *http.Transport
34+
}
35+
36+
// New Prometheus client used to consume Prometheus metrics endpoints
37+
func New(httpTransport *http.Transport) *Client {
38+
if httpTransport == nil {
39+
// Use sane defaults from http.DefaultTransport
40+
httpTransport = &http.Transport{
41+
DialContext: (&net.Dialer{
42+
Timeout: 30 * time.Second,
43+
KeepAlive: 30 * time.Second,
44+
}).DialContext,
45+
ForceAttemptHTTP2: true,
46+
MaxIdleConns: 100,
47+
IdleConnTimeout: 90 * time.Second,
48+
TLSHandshakeTimeout: 10 * time.Second,
49+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
50+
ExpectContinueTimeout: 1 * time.Second,
51+
}
52+
}
53+
54+
return &Client{
55+
httpTransport: httpTransport,
56+
}
57+
}
58+
59+
// Scrape metrics from a Prometheus HTTP endpoint
60+
func (c *Client) Scrape(ctx context.Context, url string) ([]*prom2json.Family, error) {
3061
var err error
3162

3263
mfChan := make(chan *dto.MetricFamily, 1024)
3364

34-
transport, err := makeTransport()
3565
if err != nil {
3666
return nil, err
3767
}
38-
err = prom2json.FetchMetricFamilies(url, mfChan, transport)
68+
err = prom2json.FetchMetricFamilies(url, mfChan, c.httpTransport)
3969
if err != nil {
4070
return nil, err
4171
}
@@ -47,10 +77,3 @@ func Scrape(url string) ([]*prom2json.Family, error) {
4777

4878
return result, nil
4979
}
50-
51-
func makeTransport() (*http.Transport, error) {
52-
transport := &http.Transport{
53-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
54-
}
55-
return transport, nil
56-
}

0 commit comments

Comments
 (0)