Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions telemetry/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math"
"strings"
"sync"
"time"

"github.com/pborman/uuid"
Expand Down Expand Up @@ -42,6 +43,10 @@ type (
cfg OtelConfig
vi ValidatorInfo
gatherer prometheus.Gatherer
ctx context.Context
cancel context.CancelFunc
mu sync.RWMutex
stopped bool
}
)

Expand All @@ -57,10 +62,13 @@ func NewOtelClient(otelConfig OtelConfig, vi ValidatorInfo, opts ...OtelOption)
if vi.Moniker == "" {
vi.Moniker = "UNKNOWN-" + uuid.NewUUID().String()
}
ctx, cancel := context.WithCancel(context.Background())
client := &OtelClient{
cfg: otelConfig,
vi: vi,
gatherer: prometheus.DefaultGatherer,
ctx: ctx,
cancel: cancel,
}

for _, opt := range opts {
Expand All @@ -77,7 +85,6 @@ func (o *OtelClient) StartExporter(logger log.Logger) error {
return nil
}
logger.Debug("starting otlp exporter")
ctx := context.Background()

opts := []otlpmetrichttp.Option{
otlpmetrichttp.WithEndpoint(cfg.CollectorEndpoint),
Expand All @@ -92,12 +99,12 @@ func (o *OtelClient) StartExporter(logger log.Logger) error {
opts = append(opts, otlpmetrichttp.WithInsecure())
}

exporter, err := otlpmetrichttp.New(ctx, opts...)
exporter, err := otlpmetrichttp.New(o.ctx, opts...)
if err != nil {
return fmt.Errorf("OTLP exporter setup failed: %w", err)
}

res, _ := resource.New(ctx, resource.WithAttributes(
res, _ := resource.New(o.ctx, resource.WithAttributes(
semconv.ServiceName(fmt.Sprintf("%s.%s", serviceName, o.vi.ChainID)),
semconv.ServiceVersion(version.Version),
))
Expand All @@ -113,15 +120,35 @@ func (o *OtelClient) StartExporter(logger log.Logger) error {
gauges := make(map[string]otmetric.Float64Gauge)
histograms := make(map[string]otmetric.Float64Histogram)
ticker := time.NewTicker(cfg.PushInterval)
for range ticker.C {
if err := o.scrapePrometheusMetrics(ctx, logger, meter, gauges, histograms); err != nil {
logger.Debug("error scraping metrics", "error", err)
defer ticker.Stop()

for {
select {
case <-o.ctx.Done():
logger.Debug("stopping otlp metrics scraper")
return
case <-ticker.C:
if err := o.scrapePrometheusMetrics(o.ctx, logger, meter, gauges, histograms); err != nil {
logger.Debug("error scraping metrics", "error", err)
}
}
}
}()
return nil
}

func (o *OtelClient) Stop() {
o.mu.Lock()
defer o.mu.Unlock()

if o.stopped {
return
}

o.cancel()
o.stopped = true
}

func (o *OtelClient) SetValidatorStatus(isVal bool) {
o.vi.IsValidator = isVal
}
Expand Down