Skip to content

Commit

Permalink
feat(core): split up metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Nov 7, 2023
1 parent 70ddf18 commit 05b6665
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
6 changes: 3 additions & 3 deletions core/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Exchange struct {
store *eds.Store
construct header.ConstructFn

metrics *metrics
metrics *exchangeMetrics
}

func NewExchange(
Expand All @@ -38,11 +38,11 @@ func NewExchange(
}

var (
metrics *metrics
metrics *exchangeMetrics
err error
)
if p.metrics {
metrics, err = newMetrics()
metrics, err = newExchangeMetrics()
if err != nil {
return nil, err
}
Expand Down
50 changes: 50 additions & 0 deletions core/exchange_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package core

import (
"context"
"time"

"go.opentelemetry.io/otel/metric"
)

type exchangeMetrics struct {
getByHeightDuration metric.Float64Histogram // TODO move this one
}

func newExchangeMetrics() (*exchangeMetrics, error) {
m := new(exchangeMetrics)

var err error
m.getByHeightDuration, err = meter.Float64Histogram(
"core_ex_get_by_height_request_time",
metric.WithDescription("core exchange client getByHeight request time in seconds (per single height)"),
)
if err != nil {
return nil, err
}

return m, nil
}

func (m *exchangeMetrics) observe(ctx context.Context, observeFn func(ctx context.Context)) {
if m == nil {
return
}

if ctx.Err() != nil {
ctx = context.Background()
}

observeFn(ctx)
}

func (m *exchangeMetrics) requestDurationPerHeader(ctx context.Context, duration time.Duration, amount uint64) {
m.observe(ctx, func(ctx context.Context) {
if amount == 0 {
// TODO could this happen?
return
}
durationPerHeader := duration.Seconds() / float64(amount)
m.getByHeightDuration.Record(ctx, durationPerHeader)
})
}
6 changes: 3 additions & 3 deletions core/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Listener struct {

listenerTimeout time.Duration

metrics *metrics
metrics *listenerMetrics

cancel context.CancelFunc
}
Expand All @@ -63,11 +63,11 @@ func NewListener(
}

var (
metrics *metrics
metrics *listenerMetrics
err error
)
if p.metrics {
metrics, err = newMetrics()
metrics, err = newListenerMetrics()
if err != nil {
return nil, err
}
Expand Down
42 changes: 14 additions & 28 deletions core/metrics.go → core/listener_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ import (

var meter = otel.Meter("core")

type metrics struct {
type listenerMetrics struct {
lastTimeSubscriptionStuck time.Time
lastTimeSubscriptionStuckInst metric.Int64Observable
lastTimeSubscriptionStuckInst metric.Int64ObservableGauge
lastTimeSubscriptionStuckReg metric.Registration

subscriptionStuckInst metric.Int64Counter

getByHeightDuration metric.Float64Histogram
}

func newMetrics() (*metrics, error) {
m := new(metrics)
func newListenerMetrics() (*listenerMetrics, error) {
m := new(listenerMetrics)

var err error
m.subscriptionStuckInst, err = meter.Int64Counter(
Expand All @@ -32,17 +30,16 @@ func newMetrics() (*metrics, error) {
return nil, err
}

m.lastTimeSubscriptionStuckReg, err = meter.RegisterCallback(
m.observeLastTimeStuckCallback,
m.lastTimeSubscriptionStuckInst,
m.lastTimeSubscriptionStuckInst, err = meter.Int64ObservableGauge(
"core_listener_last_time_subscription_stuck_timestamp",
metric.WithDescription("last time the listener subscription was stuck"),
)
if err != nil {
return nil, err
}

m.getByHeightDuration, err = meter.Float64Histogram(
"core_ex_get_by_height_request_time",
metric.WithDescription("core exchange client getByHeight request time in seconds (per single height)"),
m.lastTimeSubscriptionStuckReg, err = meter.RegisterCallback(
m.observeLastTimeStuckCallback,
m.lastTimeSubscriptionStuckInst,
)
if err != nil {
return nil, err
Expand All @@ -51,7 +48,7 @@ func newMetrics() (*metrics, error) {
return m, nil
}

func (m *metrics) observe(ctx context.Context, observeFn func(context.Context)) {
func (m *listenerMetrics) observe(ctx context.Context, observeFn func(ctx context.Context)) {
if m == nil {
return
}
Expand All @@ -63,30 +60,19 @@ func (m *metrics) observe(ctx context.Context, observeFn func(context.Context))
observeFn(ctx)
}

func (m *metrics) subscriptionStuck(ctx context.Context) {
func (m *listenerMetrics) subscriptionStuck(ctx context.Context) {
m.observe(ctx, func(ctx context.Context) {
m.subscriptionStuckInst.Add(ctx, 1)
m.lastTimeSubscriptionStuck = time.Now()
})
}

func (m *metrics) observeLastTimeStuckCallback(_ context.Context, obs metric.Observer) error {
func (m *listenerMetrics) observeLastTimeStuckCallback(_ context.Context, obs metric.Observer) error {
obs.ObserveInt64(m.lastTimeSubscriptionStuckInst, m.lastTimeSubscriptionStuck.Unix())
return nil
}

func (m *metrics) requestDurationPerHeader(ctx context.Context, duration time.Duration, amount uint64) {
m.observe(ctx, func(ctx context.Context) {
if amount == 0 {
// TODO could this happen?
return
}
durationPerHeader := duration.Seconds() / float64(amount)
m.getByHeightDuration.Record(ctx, durationPerHeader)
})
}

func (m *metrics) Close() error {
func (m *listenerMetrics) Close() error {
if m == nil {
return nil
}
Expand Down

0 comments on commit 05b6665

Please sign in to comment.