/
instrumented_client.go
executable file
·109 lines (92 loc) · 3.58 KB
/
instrumented_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package coingecko
// Code generated by gowrap. DO NOT EDIT.
// template: ../../../.prom-gowrap.tmpl
// gowrap: http://github.com/hexdigest/gowrap
//go:generate gowrap gen -p github.com/brave-intl/bat-go/libs/clients/-i Client -t ../../../.prom-gowrap.tmpl -o instrumented_client.go -l ""
import (
"context"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// ClientWithPrometheus implements Client interface with all methods wrapped
// with Prometheus metrics
type ClientWithPrometheus struct {
base Client
instanceName string
}
var clientDurationSummaryVec = promauto.NewSummaryVec(
prometheus.SummaryOpts{
Name: "coingecko_client_duration_seconds",
Help: "client runtime duration and result",
MaxAge: time.Minute,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"instance_name", "method", "result"})
// NewClientWithPrometheus returns an instance of the Client decorated with prometheus summary metric
func NewClientWithPrometheus(base Client, instanceName string) ClientWithPrometheus {
return ClientWithPrometheus{
base: base,
instanceName: instanceName,
}
}
// FetchCoinList implements Client
func (_d ClientWithPrometheus) FetchCoinList(ctx context.Context, includePlatform bool) (cp1 *CoinListResponse, err error) {
_since := time.Now()
defer func() {
result := "ok"
if err != nil {
result = "error"
}
clientDurationSummaryVec.WithLabelValues(_d.instanceName, "FetchCoinList", result).Observe(time.Since(_since).Seconds())
}()
return _d.base.FetchCoinList(ctx, includePlatform)
}
// FetchCoinMarkets implements Client
func (_d ClientWithPrometheus) FetchCoinMarkets(ctx context.Context, vsCurrency string, limit int) (cp1 *CoinMarketResponse, t1 time.Time, err error) {
_since := time.Now()
defer func() {
result := "ok"
if err != nil {
result = "error"
}
clientDurationSummaryVec.WithLabelValues(_d.instanceName, "FetchCoinMarkets", result).Observe(time.Since(_since).Seconds())
}()
return _d.base.FetchCoinMarkets(ctx, vsCurrency, limit)
}
// FetchMarketChart implements Client
func (_d ClientWithPrometheus) FetchMarketChart(ctx context.Context, id string, vsCurrency string, days float32, cacheDurationSeconds int) (mp1 *MarketChartResponse, t1 time.Time, err error) {
_since := time.Now()
defer func() {
result := "ok"
if err != nil {
result = "error"
}
clientDurationSummaryVec.WithLabelValues(_d.instanceName, "FetchMarketChart", result).Observe(time.Since(_since).Seconds())
}()
return _d.base.FetchMarketChart(ctx, id, vsCurrency, days, cacheDurationSeconds)
}
// FetchSimplePrice implements Client
func (_d ClientWithPrometheus) FetchSimplePrice(ctx context.Context, ids string, vsCurrencies string, include24hrChange bool) (sp1 *SimplePriceResponse, err error) {
_since := time.Now()
defer func() {
result := "ok"
if err != nil {
result = "error"
}
clientDurationSummaryVec.WithLabelValues(_d.instanceName, "FetchSimplePrice", result).Observe(time.Since(_since).Seconds())
}()
return _d.base.FetchSimplePrice(ctx, ids, vsCurrencies, include24hrChange)
}
// FetchSupportedVsCurrencies implements Client
func (_d ClientWithPrometheus) FetchSupportedVsCurrencies(ctx context.Context) (sp1 *SupportedVsCurrenciesResponse, err error) {
_since := time.Now()
defer func() {
result := "ok"
if err != nil {
result = "error"
}
clientDurationSummaryVec.WithLabelValues(_d.instanceName, "FetchSupportedVsCurrencies", result).Observe(time.Since(_since).Seconds())
}()
return _d.base.FetchSupportedVsCurrencies(ctx)
}