forked from ChainSafe/chainbridge-core
/
metrics.go
63 lines (52 loc) · 1.85 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
package opentelemetry
import (
"context"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
export "go.opentelemetry.io/otel/sdk/export/metric"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
)
type ChainbridgeMetrics struct {
meter metric.Meter
DepositEventCount metric.Int64Counter
HeadBlocks map[uint8]metric.Int64GaugeObserver
StartBlocks map[uint8]metric.Int64GaugeObserver
}
// NewChainbridgeMetrics creates an instance of ChainbridgeMetrics
// with provided OpenTelemetry meter
func NewChainbridgeMetrics(meter metric.Meter) *ChainbridgeMetrics {
headBlocks := make(map[uint8]metric.Int64GaugeObserver)
startBlocks := make(map[uint8]metric.Int64GaugeObserver)
return &ChainbridgeMetrics{
meter: meter,
DepositEventCount: metric.Must(meter).NewInt64Counter(
"chainbridge.DepositEventCount",
metric.WithDescription("Number of deposit events across all chains"),
),
HeadBlocks: headBlocks,
StartBlocks: startBlocks,
}
}
func initOpenTelemetryMetrics(opts ...otlpmetrichttp.Option) (*ChainbridgeMetrics, error) {
ctx := context.Background()
client := otlpmetrichttp.NewClient(opts...)
exp, err := otlpmetric.New(ctx, client)
if err != nil {
return nil, err
}
selector := simple.NewWithInexpensiveDistribution()
proc := processor.NewFactory(selector, export.CumulativeExportKindSelector())
cont := controller.New(proc, controller.WithExporter(exp))
global.SetMeterProvider(cont)
err = cont.Start(ctx)
if err != nil {
return nil, err
}
meter := cont.Meter("chainbridge")
metrics := NewChainbridgeMetrics(meter)
return metrics, nil
}