forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
73 lines (62 loc) · 1.75 KB
/
metrics.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
package p2p
import (
"context"
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
var meter = otel.Meter("shrex/eds")
type status string
const (
StatusBadRequest status = "bad_request"
StatusSendRespErr status = "send_resp_err"
StatusSendReqErr status = "send_req_err"
StatusReadRespErr status = "read_resp_err"
StatusInternalErr status = "internal_err"
StatusNotFound status = "not_found"
StatusTimeout status = "timeout"
StatusSuccess status = "success"
StatusRateLimited status = "rate_limited"
)
type Metrics struct {
totalRequestCounter metric.Int64Counter
}
// ObserveRequests increments the total number of requests sent with the given status as an
// attribute.
func (m *Metrics) ObserveRequests(ctx context.Context, count int64, status status) {
if m == nil {
return
}
if ctx.Err() != nil {
ctx = context.Background()
}
m.totalRequestCounter.Add(ctx, count,
metric.WithAttributes(
attribute.String("status", string(status)),
))
}
func InitClientMetrics(protocol string) (*Metrics, error) {
totalRequestCounter, err := meter.Int64Counter(
fmt.Sprintf("shrex_%s_client_total_requests", protocol),
metric.WithDescription(fmt.Sprintf("Total count of sent shrex/%s requests", protocol)),
)
if err != nil {
return nil, err
}
return &Metrics{
totalRequestCounter: totalRequestCounter,
}, nil
}
func InitServerMetrics(protocol string) (*Metrics, error) {
totalRequestCounter, err := meter.Int64Counter(
fmt.Sprintf("shrex_%s_server_total_responses", protocol),
metric.WithDescription(fmt.Sprintf("Total count of sent shrex/%s responses", protocol)),
)
if err != nil {
return nil, err
}
return &Metrics{
totalRequestCounter: totalRequestCounter,
}, nil
}