generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: introduce cron job metrics #2256
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
277eac8
introduce cron job metrics
jonathanj-square f44c0c7
chore(autofmt): Automated formatting
github-actions[bot] 61dafb6
per-PR feedback:
jonathanj-square 1eb564b
chore(autofmt): Automated formatting
github-actions[bot] 390fe34
Merge branch 'main' into jonathanj/otel/cron
jonathanj-square 820f7d8
Merge branch 'main' into jonathanj/otel/cron
jonathanj-square a949bf1
address merge conflict
jonathanj-square 2f09321
Merge branch 'main' into jonathanj/otel/cron
jonathanj-square e2befca
applying PR feedback
jonathanj-square c4445db
chore(autofmt): Automated formatting
github-actions[bot] 6bf334d
fix typo
jonathanj-square f94a358
naming improvement
jonathanj-square File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,107 @@ | ||
package observability | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/alecthomas/types/optional" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
|
||
"github.com/TBD54566975/ftl/internal/model" | ||
"github.com/TBD54566975/ftl/internal/observability" | ||
) | ||
|
||
const ( | ||
cronMeterName = "ftl.cron" | ||
cronJobFullNameAttribute = "ftl.cron.job.full_name" | ||
|
||
cronJobKilledStatus = "killed" | ||
cronJobFailedStartStatus = "failed_start" | ||
) | ||
|
||
type CronMetrics struct { | ||
jobsActive metric.Int64UpDownCounter | ||
jobsCompleted metric.Int64Counter | ||
jobLatency metric.Int64Histogram | ||
} | ||
|
||
func initCronMetrics() (*CronMetrics, error) { | ||
result := &CronMetrics{} | ||
|
||
var errs error | ||
var err error | ||
|
||
meter := otel.Meter(deploymentMeterName) | ||
|
||
counter := fmt.Sprintf("%s.jobs.completed", cronMeterName) | ||
if result.jobsCompleted, err = meter.Int64Counter( | ||
counter, | ||
metric.WithDescription("the number of cron jobs completed; successful or otherwise")); err != nil { | ||
result.jobsCompleted, errs = handleInt64CounterError(counter, err, errs) | ||
} | ||
|
||
counter = fmt.Sprintf("%s.jobs.active", cronMeterName) | ||
if result.jobsActive, err = meter.Int64UpDownCounter( | ||
counter, | ||
metric.WithDescription("the number of actively executing cron jobs")); err != nil { | ||
result.jobsActive, errs = handleInt64UpDownCounterError(counter, err, errs) | ||
} | ||
|
||
counter = fmt.Sprintf("%s.job.latency", cronMeterName) | ||
if result.jobLatency, err = meter.Int64Histogram( | ||
counter, | ||
metric.WithDescription("the latency between the scheduled execution time of a cron job"), | ||
metric.WithUnit("ms")); err != nil { | ||
result.jobLatency, errs = handleInt64HistogramCounterError(counter, err, errs) | ||
} | ||
|
||
return result, errs | ||
} | ||
|
||
func (m *CronMetrics) JobStarted(ctx context.Context, job model.CronJob) { | ||
m.jobsActive.Add(ctx, 1, cronAttributes(job, optional.None[string]())) | ||
} | ||
|
||
func (m *CronMetrics) JobSuccess(ctx context.Context, job model.CronJob) { | ||
m.jobCompleted(ctx, job, observability.SuccessStatus) | ||
} | ||
|
||
func (m *CronMetrics) JobKilled(ctx context.Context, job model.CronJob) { | ||
m.jobCompleted(ctx, job, cronJobKilledStatus) | ||
} | ||
|
||
func (m *CronMetrics) JobFailedStart(ctx context.Context, job model.CronJob) { | ||
completionAttributes := cronAttributes(job, optional.Some(cronJobFailedStartStatus)) | ||
|
||
elapsed := timeSinceMS(job.NextExecution) | ||
m.jobLatency.Record(ctx, elapsed, completionAttributes) | ||
m.jobsCompleted.Add(ctx, 1, completionAttributes) | ||
} | ||
|
||
func (m *CronMetrics) JobFailed(ctx context.Context, job model.CronJob) { | ||
m.jobCompleted(ctx, job, observability.FailureStatus) | ||
} | ||
|
||
func (m *CronMetrics) jobCompleted(ctx context.Context, job model.CronJob, status string) { | ||
elapsed := timeSinceMS(job.NextExecution) | ||
|
||
m.jobsActive.Add(ctx, -1, cronAttributes(job, optional.None[string]())) | ||
|
||
completionAttributes := cronAttributes(job, optional.Some(status)) | ||
m.jobLatency.Record(ctx, elapsed, completionAttributes) | ||
m.jobsCompleted.Add(ctx, 1, completionAttributes) | ||
} | ||
|
||
func cronAttributes(job model.CronJob, maybeStatus optional.Option[string]) metric.MeasurementOption { | ||
attributes := []attribute.KeyValue{ | ||
attribute.String(observability.ModuleNameAttribute, job.Key.Payload.Module), | ||
attribute.String(cronJobFullNameAttribute, job.Key.String()), | ||
attribute.String(observability.RunnerDeploymentKeyAttribute, job.DeploymentKey.String()), | ||
} | ||
if status, ok := maybeStatus.Get(); ok { | ||
attributes = append(attributes, attribute.String(observability.OutcomeStatusNameAttribute, status)) | ||
} | ||
return metric.WithAttributes(attributes...) | ||
} |
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 |
---|---|---|
@@ -1,7 +1,19 @@ | ||
package observability | ||
|
||
import "go.opentelemetry.io/otel/attribute" | ||
|
||
const ( | ||
ModuleNameAttribute = "ftl.module.name" | ||
StatusSucceededAttribute = "ftl.status.succeeded" | ||
OutcomeStatusNameAttribute = "ftl.outcome.status" | ||
RunnerDeploymentKeyAttribute = "ftl.deployment.key" | ||
|
||
SuccessStatus = "success" | ||
FailureStatus = "failure" | ||
) | ||
|
||
func SuccessOrFailureStatus(succeeded bool) attribute.KeyValue { | ||
if succeeded { | ||
return attribute.String(OutcomeStatusNameAttribute, SuccessStatus) | ||
} | ||
return attribute.String(OutcomeStatusNameAttribute, FailureStatus) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about reprising the pattern you came up with ages ago and adding a helper function here that just returns
attribute.String(observability.OutcomeStatusNameAttribute, observability.SuccessOrFailureStatus(succeeded))
? Since so much of the code that uses this now is basically just duplicating that lineThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm, applied
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
micro-nit: could we rename
SuccessOrFailureStatus
toSuccessOrFailureStatusAttr
since that's what it returns?