Skip to content

Commit

Permalink
[CAPPL-2] Add Cron Trigger Capability
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkaseman committed Aug 26, 2024
1 parent 6a3d104 commit 2d70038
Show file tree
Hide file tree
Showing 9 changed files with 1,298 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/confluentinc/confluent-kafka-go/v2 v2.3.0
github.com/dominikbraun/graph v0.23.0
github.com/fxamacker/cbor/v2 v2.5.0
github.com/go-co-op/gocron/v2 v2.11.0
github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -76,6 +77,7 @@ require (
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0
github.com/stretchr/objx v0.5.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE=
github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/go-co-op/gocron/v2 v2.11.0 h1:IOowNA6SzwdRFnD4/Ol3Kj6G2xKfsoiiGq2Jhhm9bvE=
github.com/go-co-op/gocron/v2 v2.11.0/go.mod h1:xY7bJxGazKam1cz04EebrlP4S9q4iWdiAylMGP3jY9w=
github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0 h1:ymLjT4f35nQbASLnvxEde4XOBL+Sn7rFuV+FOJqkljg=
github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0/go.mod h1:6daplAwHHGbUGib4990V3Il26O0OC4aRyvewaaAihaA=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -204,6 +206,8 @@ github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwa
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/riferrei/srclient v0.5.4 h1:dfwyR5u23QF7beuVl2WemUY2KXh5+Sc4DHKyPXBNYuc=
github.com/riferrei/srclient v0.5.4/go.mod h1:vbkLmWcgYa7JgfPvuy/+K8fTS0p1bApqadxrxi/S1MI=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
Expand Down
30 changes: 30 additions & 0 deletions pkg/capabilities/triggers/crontrigger/cron_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package crontrigger

import (
"github.com/go-co-op/gocron/v2"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
)

var _ gocron.Logger = (*CronLogger)(nil)

type CronLogger struct {
lggr logger.Logger
}

func NewCronLogger(lggr logger.Logger) gocron.Logger {
return &CronLogger{lggr: lggr}
}

func (cl CronLogger) Debug(msg string, args ...any) {
cl.lggr.Debugw(msg, "args", args)
}
func (cl CronLogger) Error(msg string, args ...any) {
cl.lggr.Errorw(msg, "args", args)
}
func (cl CronLogger) Info(msg string, args ...any) {
cl.lggr.Infow(msg, "args", args)
}
func (cl CronLogger) Warn(msg string, args ...any) {
cl.lggr.Warnw(msg, "args", args)
}
55 changes: 55 additions & 0 deletions pkg/capabilities/triggers/crontrigger/cron_monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package crontrigger

import (
"time"

"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

type CronMonitor struct{}

var (
PromRunningServices = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "capability_trigger_cron_running_services",
Help: "Metric representing the number of cron trigger services that are actively scheduling triggers",
},
)
PromTotalTriggersCount = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "capability_trigger_cron_total_triggers_count",
Help: "Metric representing the number of currently active triggers",
},
)
PromExecutionTimeMS = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "capability_trigger_cron_execution_time_ms",
Help: "Metric representing the execution time in milliseconds, by TriggerID",
},
[]string{"trigger_id"},
)
PromTaskRunsCount = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "capability_trigger_cron_runs_count",
Help: "Metric representing the number of runs completed with status, by TriggerID",
},
[]string{"trigger_id", "status"},
)
)

func NewCronMonitor() gocron.Monitor {
return &CronMonitor{}
}

// Hooks into gocron after the job has finished a run. Proceeds RecordJobTiming.
func (cm *CronMonitor) IncrementJob(_ uuid.UUID, name string, _ []string, status gocron.JobStatus) {
PromTaskRunsCount.WithLabelValues(name, string(status)).Inc()
}

// Hooks into gocron after the job has finished a run
func (cm *CronMonitor) RecordJobTiming(startTime, endTime time.Time, _ uuid.UUID, name string, _ []string) {
PromExecutionTimeMS.WithLabelValues(name).Observe(float64(endTime.Sub(startTime).Milliseconds()))
}
Loading

0 comments on commit 2d70038

Please sign in to comment.