Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
feat(BUX-567): track metrics of cronJobs
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Feb 12, 2024
1 parent 25fd0c4 commit 1b286c1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
61 changes: 39 additions & 22 deletions cron_job_declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,50 @@ type cronJobHandler func(ctx context.Context, client *Client) error

// here is where we define all the cron jobs for the client
func (c *Client) cronJobs() taskmanager.CronJobs {
// handler adds the client pointer to the cronJobTask by using a closure
handler := func(cronJobTask cronJobHandler) taskmanager.CronJobHandler {
return func(ctx context.Context) error {
return cronJobTask(ctx, c)
jobs := taskmanager.CronJobs{}

addJob := func(name string, period time.Duration, task cronJobHandler) {
// handler adds the client pointer to the cronJobTask by using a closure
handler := func(ctx context.Context) (err error) {
if metrics, enabled := c.Metrics(); enabled {
end := metrics.TrackCron(name)
defer func() {
success := err == nil
end(success)
}()
}
err = task(ctx, c)
return
}
}

jobs := taskmanager.CronJobs{
CronJobNameDraftTransactionCleanUp: {
Period: 60 * time.Second,
Handler: handler(taskCleanupDraftTransactions),
},
CronJobNameSyncTransactionBroadcast: {
Period: 2 * time.Minute,
Handler: handler(taskBroadcastTransactions),
},
CronJobNameSyncTransactionSync: {
Period: 5 * time.Minute,
Handler: handler(taskSyncTransactions),
},
jobs[name] = taskmanager.CronJob{
Handler: handler,
Period: period,
}
}

addJob(
CronJobNameDraftTransactionCleanUp,
60*time.Second,
taskCleanupDraftTransactions,
)
addJob(
CronJobNameSyncTransactionBroadcast,
2*time.Minute,
taskBroadcastTransactions,
)
addJob(
CronJobNameSyncTransactionSync,
5*time.Minute,
taskSyncTransactions,
)

if _, enabled := c.Metrics(); enabled {
jobs[CronJobNameCalculateMetrics] = taskmanager.CronJob{
Period: 15 * time.Second,
Handler: handler(taskCalculateMetrics),
}
addJob(
CronJobNameCalculateMetrics,
15*time.Second,
taskCalculateMetrics,
)
}

return jobs
Expand Down
5 changes: 5 additions & 0 deletions metrics/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package metrics
// Collector is an interface that is used to register metrics
type Collector interface {
RegisterGauge(name string) GaugeInterface
RegisterGaugeVec(name string, labels ...string) GaugeVecInterface
RegisterHistogramVec(name string, labels ...string) HistogramVecInterface
}

type GaugeVecInterface interface {
WithLabelValues(lvs ...string) GaugeInterface
}

// GaugeInterface is an interface that is used to track gauges of values
type GaugeInterface interface {
Set(value float64)
Expand Down
13 changes: 13 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Metrics struct {
verifyMerkleRoots HistogramVecInterface
recordTransaction HistogramVecInterface
queryTransaction HistogramVecInterface
cronHistogram HistogramVecInterface
cronLastExecution GaugeVecInterface
}

// NewMetrics is a constructor for the Metrics struct
Expand All @@ -22,6 +24,8 @@ func NewMetrics(collector Collector) *Metrics {
verifyMerkleRoots: collector.RegisterHistogramVec(verifyMerkleRootsHistogramName, "classification"),
recordTransaction: collector.RegisterHistogramVec(recordTransactionHistogramName, "classification", "strategy"),
queryTransaction: collector.RegisterHistogramVec(queryTransactionHistogramName, "classification"),
cronHistogram: collector.RegisterHistogramVec(cronHistogramName, "name"),
cronLastExecution: collector.RegisterGaugeVec(cronLastExecutionGaugeName, "name"),
}
}

Expand Down Expand Up @@ -52,6 +56,15 @@ func (m *Metrics) TrackQueryTransaction() EndWithClassification {
}
}

// TrackCron is used to track the time it takes to execute a cron job
func (m *Metrics) TrackCron(name string) EndWithClassification {
start := time.Now()
m.cronLastExecution.WithLabelValues(name).Set(float64(start.Unix()))
return func(success bool) {
m.cronHistogram.WithLabelValues(name).Observe(time.Since(start).Seconds())
}
}

func classify(success bool) string {
if success {
return "success"
Expand Down
5 changes: 5 additions & 0 deletions metrics/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ const (
queryTransactionHistogramName = domainPrefix + "query_transaction_histogram"
)

const (
cronHistogramName = domainPrefix + "cron_histogram"
cronLastExecutionGaugeName = domainPrefix + "cron_last_execution_gauge"
)

const xpubGaugeName = domainPrefix + "xpub_gauge"

0 comments on commit 1b286c1

Please sign in to comment.