Add config.lambdakiq.metrics_enabled to turn off CloudWatch metrics - #42
Open
jeremiahlukus wants to merge 1 commit into
Open
Add config.lambdakiq.metrics_enabled to turn off CloudWatch metrics#42jeremiahlukus wants to merge 1 commit into
jeremiahlukus wants to merge 1 commit into
Conversation
CloudWatch Embedded Metrics are emitted for every ActiveJob event and dimensioned by AppName, JobEvent and JobName. Since custom metrics bill per unique dimension combination, apps with many job classes or high job volume can run up real CloudWatch cost with no way to opt out. Adds `config.lambdakiq.metrics_enabled`, defaulting to true so behavior is unchanged. The flag is checked in the notification subscriber so that a disabled app does not allocate an Event or build any metric payload, and again in Metrics.log so the class is safe to call directly. Also fixes `metrics_logger=`, which was documented but impossible to set: the railtie's after_initialize hook assigned it unconditionally, clobbering any value the application set in config/application.rb. It now only fills in Rails.logger when the application has not chosen a logger. Tests reset Lambdakiq.config between runs, since it is a process wide OrderedOptions and a leaked value would cascade into unrelated tests. Co-authored-by: fabiensebban <fa.sebban@gmail.com>
Contributor
Author
|
@metaskills Hey do you mind adding fixing up the publish gem workflow? https://github.com/rails-lambda/lambdakiq/actions/runs/26990002969/job/79647930370 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Supersedes #38 by @fabiensebban, whose report and original patch this builds on.
Why
Lambdakiq emits CloudWatch Embedded Metrics for every ActiveJob event, dimensioned by
AppName,JobEvent, andJobName. Custom metrics bill per unique dimension combination, so unique metric count scales as job classes × events × 3 metrics — at 50 job classes and 4 events that's ~600 custom metrics, roughly $180/month, plus per-job log ingestion. There was no way to opt out.As reported in #38, the documented escape hatch didn't work either. Setting
config.lambdakiq.metrics_logger = Logger.new("/dev/null")inconfig/application.rbhad no effect, because the railtie'safter_initializehook reassignedmetrics_loggerunconditionally after application config was applied. Verified before the fix:So
metrics_logger=was documented but unsettable. That's fixed here too.What changed
config.lambdakiq.metrics_enabled, defaulting totrue— no behavior change for existing users.The flag is checked in the notification subscriber rather than deep in the metric writer, so a disabled app skips
ActiveSupport::Notifications::Eventallocation and the entireinstrument!payload build, not just theJSON.dump. It's checked again inMetrics.logso the class is safe called directly. Because the check is at call time rather than at subscribe time, the flag stays togglable at runtime.metrics_loggerboot-order fix —after_initializenow uses||=, so it only fills inRails.loggerwhen the application hasn't chosen one.config.active_job.loggeris deliberately left as a plain assignment; ActiveJob sets its own default, so||=there would silently change behavior.Test isolation —
Lambdakiq.configis a process-wideOrderedOptions, so a test that mutates it leaks into every test after it. On #38's version I confirmed that when its new test fails, the leakedfalsetakes 3 unrelated tests down with it. Config is now reset in the globalbeforeblock.Naming
Named
metrics_enabledrather than #38'ssend_cloud_watch_metricsto match the existingmetrics_namespace/metrics_logger/metrics_app_namefamily. Nothing was released under the old name, so there's no compatibility shim.Docs
Documents
metrics_enabled=, plusmetrics_app_name=which already existed but was undocumented, and adds a section covering both ways to reduce metric cost. It also notes that metric properties include each job's arguments — an independent reason some apps will want this off.Testing
42 runs, 260 assertions, 0 failures, stable across repeated runs.New
test/cases/railtie_test.rbboots a throwaway Rails app in a subprocess, since Rails initializes only once per process and the existing dummy app is already booted — the boot-order bug is otherwise untestable in-suite.I verified each guard is load-bearing by reverting them individually against the new tests:
metrics_logger ||=→does not overwrite a metrics_logger set by the applicationfailsmetrics_enabledguards →does not log cloudwatch embedded metrics when metrics are disabledfails