### AWS CLI Examples for CloudWatch Alarms Source: https://context7.com/sj26/sidekiq-cloudwatchmetrics/llms.txt These AWS CLI commands illustrate how to create CloudWatch alarms based on Sidekiq metrics. Examples include alarms for high queue latency and queue size, with specified thresholds and actions. ```bash # AWS CLI example: Create alarm for high queue latency aws cloudwatch put-metric-alarm \ --alarm-name "Sidekiq-HighQueueLatency" \ --namespace "Sidekiq" \ --metric-name "DefaultQueueLatency" \ --statistic Average \ --period 60 \ --threshold 30 \ --comparison-operator GreaterThanThreshold \ --evaluation-periods 3 \ --alarm-actions "arn:aws:sns:us-east-1:123456789:alerts" # AWS CLI example: Create alarm for queue size aws cloudwatch put-metric-alarm \ --alarm-name "Sidekiq-QueueBacklog" \ --namespace "Sidekiq" \ --metric-name "QueueSize" \ --dimensions Name=QueueName,Value=critical \ --statistic Sum \ --period 60 \ --threshold 1000 \ --comparison-operator GreaterThanThreshold \ --evaluation-periods 2 \ --alarm-actions "arn:aws:autoscaling:us-east-1:123456789:scalingPolicy:..." ``` -------------------------------- ### Install Sidekiq CloudWatch Metrics Gem Source: https://github.com/sj26/sidekiq-cloudwatchmetrics/blob/main/README.md Add the sidekiq-cloudwatchmetrics gem to your application's Gemfile. This gem integrates with Sidekiq to send metrics to CloudWatch. Ensure you run `bundle install` after adding the gem. ```ruby gem "sidekiq" gem "sidekiq-cloudwatchmetrics" ``` -------------------------------- ### Enable Sidekiq CloudWatch Metrics Reporting Source: https://github.com/sj26/sidekiq-cloudwatchmetrics/blob/main/README.md Enable the Sidekiq CloudWatch Metrics reporting by adding the provided code snippet to your Sidekiq configuration file (e.g., `config/initializers/sidekiq.rb`). This will start a thread to send metrics. ```ruby require "sidekiq" require "sidekiq/cloudwatchmetrics" Sidekiq::CloudWatchMetrics.enable! ``` -------------------------------- ### CloudWatch Metric Data Structure Example Source: https://context7.com/sj26/sidekiq-cloudwatchmetrics/llms.txt This snippet shows the structure of metric data that can be published to CloudWatch. It includes common metrics like ProcessedJobs, QueueSize, and Utilization, along with their associated dimensions and units. ```json { "namespace": "Sidekiq", "metric_data": [ { "metric_name": "ProcessedJobs", "timestamp": "2023-10-27T10:00:00Z", "value": 12345, "unit": "Count" }, { "metric_name": "QueueSize", "dimensions": [{"name": "QueueName", "value": "default"}], "timestamp": "2023-10-27T10:00:00Z", "value": 42, "unit": "Count" }, { "metric_name": "Utilization", "dimensions": [{"name": "Hostname", "value": "worker-1.example.com"}], "timestamp": "2023-10-27T10:00:00Z", "value": 75.0, "unit": "Percent" } ] } ``` -------------------------------- ### Manage Sidekiq CloudWatch Metrics Publisher Thread Source: https://context7.com/sj26/sidekiq-cloudwatchmetrics/llms.txt The `Publisher` class allows direct control over the metrics collection and publishing thread. You can instantiate it with custom configurations, start the thread, check its status, and gracefully stop or quiet it. This is an advanced usage scenario. ```ruby require "sidekiq/cloudwatchmetrics" # Direct instantiation (advanced usage) publisher = Sidekiq::CloudWatchMetrics::Publisher.new( client: Aws::CloudWatch::Client.new, namespace: "Sidekiq", interval: 60, process_metrics: true, additional_dimensions: {} ) # Start the publisher thread publisher.start # Check if publisher is running publisher.running? # => true # Gracefully quiet the publisher (stops publishing but keeps thread alive) publisher.quiet # Stop the publisher completely publisher.stop ``` -------------------------------- ### Configure CloudWatch Client for Metrics Source: https://github.com/sj26/sidekiq-cloudwatchmetrics/blob/main/README.md Optionally, you can explicitly provide an AWS CloudWatch Client instance to the `enable!` method. This is useful if you need custom AWS credential handling or client configurations. The default behavior relies on EC2 instance roles or environment variables. ```ruby Sidekiq::CloudWatchMetrics.enable!(client: Aws::CloudWatch::Client.new) ``` -------------------------------- ### Enable High-Resolution Metrics in Sidekiq Source: https://context7.com/sj26/sidekiq-cloudwatchmetrics/llms.txt This Ruby code demonstrates how to enable high-resolution metrics (1-second intervals) for Sidekiq using the sidekiq-cloudwatchmetrics gem. It also shows how to configure standard 60-second resolution. ```ruby # Enable high-resolution metrics (1-second resolution storage) Sidekiq::CloudWatchMetrics.enable!(interval: 30) # Metrics will include storage_resolution: 1 # { # metric_name: "ProcessedJobs", # timestamp: now, # value: 123, # unit: "Count", # storage_resolution: 1 # } # Standard resolution (60-second intervals, default) Sidekiq::CloudWatchMetrics.enable!(interval: 60) # or simply Sidekiq::CloudWatchMetrics.enable! ``` -------------------------------- ### Enable CloudWatch Metrics Publishing in Sidekiq Source: https://context7.com/sj26/sidekiq-cloudwatchmetrics/llms.txt The `enable!` method is the primary way to activate CloudWatch metrics publishing. It can be configured with a custom CloudWatch client, namespace, publishing interval, additional dimensions, and options to disable process metrics. This method should be called within Sidekiq's initializer. ```ruby # config/initializers/sidekiq.rb require "sidekiq" require "sidekiq/cloudwatchmetrics" # Basic usage - uses default AWS credentials from environment or EC2 instance role Sidekiq::CloudWatchMetrics.enable! # With custom CloudWatch client (explicit credentials) Sidekiq::CloudWatchMetrics.enable!( client: Aws::CloudWatch::Client.new( region: "us-east-1", access_key_id: ENV["AWS_ACCESS_KEY_ID"], secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"] ) ) # With custom namespace for environment separation Sidekiq::CloudWatchMetrics.enable!(namespace: "Sidekiq-Production") # With custom publishing interval (in seconds) Sidekiq::CloudWatchMetrics.enable!(interval: 30) # With additional custom dimensions for metric filtering Sidekiq::CloudWatchMetrics.enable!( additional_dimensions: { Environment: "production", AppCluster: "web-workers" } ) # Disable per-process utilization metrics (reduces CloudWatch costs) Sidekiq::CloudWatchMetrics.enable!(process_metrics: false) # Full configuration example Sidekiq::CloudWatchMetrics.enable!( client: Aws::CloudWatch::Client.new(region: "us-west-2"), namespace: "MyApp-Sidekiq", interval: 30, process_metrics: true, additional_dimensions: { Environment: "staging", Team: "backend" } ) ``` -------------------------------- ### Gemfile Configuration for Sidekiq CloudWatch Metrics Source: https://context7.com/sj26/sidekiq-cloudwatchmetrics/llms.txt This Ruby code snippet shows how to add the sidekiq-cloudwatchmetrics gem to your application's Gemfile. It specifies compatible versions for Sidekiq and Sidekiq Enterprise. ```ruby # Gemfile source "https://rubygems.org" gem "sidekiq", "~> 7.0" gem "sidekiq-cloudwatchmetrics", "~> 2.9" # For Sidekiq Enterprise with leader election support gem "sidekiq-ent", "~> 7.0" gem "sidekiq-cloudwatchmetrics", "~> 2.9" ``` -------------------------------- ### Customize CloudWatch Metrics Namespace Source: https://github.com/sj26/sidekiq-cloudwatchmetrics/blob/main/README.md Configure the CloudWatch namespace for your Sidekiq metrics using the `namespace` option in the `enable!` method. The default namespace is 'Sidekiq'. ```ruby Sidekiq::CloudWatchMetrics.enable!(namespace: "Sidekiq-Staging") ``` -------------------------------- ### AWS IAM Policy for CloudWatch Metrics Source: https://context7.com/sj26/sidekiq-cloudwatchmetrics/llms.txt This JSON policy grants the necessary permissions for a Sidekiq process to publish metrics to CloudWatch under the 'Sidekiq' namespace. It requires the 'cloudwatch:PutMetricData' action. ```json { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "cloudwatch:PutMetricData" ], "Resource": "*", "Condition": { "StringEquals": { "cloudwatch:namespace": "Sidekiq" } } } ] } ``` -------------------------------- ### Adjust Metrics Reporting Interval Source: https://github.com/sj26/sidekiq-cloudwatchmetrics/blob/main/README.md Modify the interval at which metrics are published to CloudWatch using the `interval` option in the `enable!` method. The default interval is 60 seconds. ```ruby Sidekiq::CloudWatchMetrics.enable!(interval: 30) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.