-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CAPPL-2] Add Cron Trigger Capability
- Loading branch information
1 parent
6a3d104
commit 2d70038
Showing
9 changed files
with
1,298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
Oops, something went wrong.