-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_client.go
70 lines (56 loc) · 1.91 KB
/
metrics_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
package hcnetcore
import (
"context"
"time"
"github.com/prometheus/client_golang/prometheus"
proto "github.com/shantanu-hashcash/go/protocols/hcnetcore"
"github.com/shantanu-hashcash/go/xdr"
)
var envelopeTypeToLabel = map[xdr.EnvelopeType]string{
xdr.EnvelopeTypeEnvelopeTypeTxV0: "v0",
xdr.EnvelopeTypeEnvelopeTypeTx: "v1",
xdr.EnvelopeTypeEnvelopeTypeTxFeeBump: "fee_bump",
}
type ClientWithMetrics struct {
coreClient Client
// submissionDuration exposes timing metrics about the rate and latency of
// submissions to hcnet-core
submissionDuration *prometheus.SummaryVec
}
func (c ClientWithMetrics) SubmitTx(ctx context.Context, rawTx string) (*proto.TXResponse, error) {
var envelope xdr.TransactionEnvelope
err := xdr.SafeUnmarshalBase64(rawTx, &envelope)
if err != nil {
return &proto.TXResponse{}, err
}
startTime := time.Now()
response, err := c.coreClient.SubmitTransaction(ctx, rawTx)
duration := time.Since(startTime).Seconds()
label := prometheus.Labels{}
if err != nil {
label["status"] = "request_error"
} else if response.IsException() {
label["status"] = "exception"
} else {
label["status"] = response.Status
}
label["envelope_type"] = envelopeTypeToLabel[envelope.Type]
c.submissionDuration.With(label).Observe(duration)
return response, err
}
func NewClientWithMetrics(client Client, registry *prometheus.Registry, prometheusSubsystem string) ClientWithMetrics {
submissionDuration := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: "aurora",
Subsystem: prometheusSubsystem,
Name: "submission_duration_seconds",
Help: "submission durations to Hcnet-Core, sliding window = 10m",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, []string{"status", "envelope_type"})
registry.MustRegister(
submissionDuration,
)
return ClientWithMetrics{
coreClient: client,
submissionDuration: submissionDuration,
}
}