forked from ChainSafe/chainbridge-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opentelemetry.go
53 lines (43 loc) · 1.41 KB
/
opentelemetry.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
package opentelemetry
import (
"context"
"net/url"
"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
)
type OpenTelemetry struct {
metrics *ChainbridgeMetrics
}
// NewOpenTelemetry initializes OpenTelementry metrics
func NewOpenTelemetry(collectorRawURL string) (*OpenTelemetry, error) {
collectorURL, err := url.Parse(collectorRawURL)
if err != nil {
return &OpenTelemetry{}, err
}
metricOptions := []otlpmetrichttp.Option{
otlpmetrichttp.WithURLPath(collectorURL.Path),
otlpmetrichttp.WithEndpoint(collectorURL.Host),
}
if collectorURL.Scheme == "http" {
metricOptions = append(metricOptions, otlpmetrichttp.WithInsecure())
}
metrics, err := initOpenTelemetryMetrics(metricOptions...)
if err != nil {
return &OpenTelemetry{}, err
}
return &OpenTelemetry{
metrics: metrics,
}, nil
}
// TrackDepositMessage extracts metrics from deposit message and sends
// them to OpenTelemetry collector
func (t *OpenTelemetry) TrackDepositMessage(m *message.Message) {
t.metrics.DepositEventCount.Add(context.Background(), 1)
}
// ConsoleTelemetry is telemetry that logs metrics and should be used
// when metrics sending to OpenTelemetry should be disabled
type ConsoleTelemetry struct{}
func (t *ConsoleTelemetry) TrackDepositMessage(m *message.Message) {
log.Info().Msgf("Deposit message: %v", m.String())
}