class

EasyAwscr::CloudWatch::Client

Inherits Reference < Object

CloudWatch with credentials and connections taken care of.

The everyday calls are wrapped below and documented in Awscr::CloudWatch. Everything else is reachable through #with_native_client.

client = EasyAwscr::CloudWatch::Client.new
client.put_counter("MyApp", "Requests")
client.describe_alarms(alarm_name_prefix: "MyApp").metric_alarms.each { |a| puts a.alarm_name }

Constants

DEFAULT_POOL_REFRESH_INTERVAL = 24.hours

From time to time, we should recreated the connection pool, so it will reload the SSL context (see https://github.com/crystal-lang/crystal/issues/15419).

MAX_QUERIES_PER_REQUEST = 500

AWS accepts at most 500 queries per GetMetricData request: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricData.html

Constructors

from_native_client(native_client : Awscr::CloudWatch::Client) : self

Converts an existing client from "awscr-cloudwatch" to use the "easy-awscr" client interface.

Source
new(*, region : String = EasyAwscr::Config.default_region!, credential_provider : EasyAwscr::Config::Provider = EasyAwscr::Config.default_credential_provider, client_provider : ClientProvider | Nil = nil, endpoint : String | Nil = nil, lazy_init = false)
Source

Instance methods

close

Closes this client. If used again, a new connection will be opened.

Source
delete_alarms(alarm_names : Array(String)) : Nil
Source
delete_anomaly_detector(namespace : String, metric_name : String, stat : String, **options) : Nil
Source
delete_dashboards(dashboard_names : Array(String)) : Nil
Source
describe_alarm_history
Source
describe_alarms
Source
describe_alarms_for_metric(namespace : String, metric_name : String, **options)
Source
describe_anomaly_detectors
Source
disable_alarm_actions(alarm_names : Array(String)) : Nil
Source
each_datapoint(metrics : Array(Awscr::CloudWatch::Metric), *, stat : String, period : Time::Span, start_time : Time, end_time : Time, & : Awscr::CloudWatch::Metric, Time, Float64 -> ) : Nil

The stat (Sum, Average, p99, ...) of every metric per period. Yields (metric, time, value) for every data point, in time order per metric. Works for any number of metrics and any time range: 500 metrics per request, all pages followed.

Old data is kept in coarse resolution only. Use a period that is a multiple of 5 minutes for start times older than 15 days, and a multiple of 1 hour for start times older than 63 days, or those points come back empty. The periods start at start_time, so start on a full hour to get calendar hours.

metrics = ["requests.total", "requests.errors"].map { |name| Awscr::CloudWatch::Metric.new("MyApp", name) }
client.each_datapoint(metrics, stat: "Sum", period: 1.hour, start_time: Time.utc - 7.days, end_time: Time.utc) do |metric, time, value|
  puts "#{metric.metric_name} #{time}: #{value}"
end
Source
each_metric_data(queries : Array(Awscr::CloudWatch::MetricDataQuery), *, start_time : Time, end_time : Time, scan_by : String = "TimestampAscending", & : Awscr::CloudWatch::Response::MetricDataResult -> ) : Nil

Like get_metric_data, but follows the pages to the end and yields every result (one per query and page). Points come oldest first unless scan_by says otherwise, and memory stays flat also for long time ranges.

Source
enable_alarm_actions(alarm_names : Array(String)) : Nil
Source
get_dashboard(dashboard_name : String)
Source
get_metric_data(queries : Array(Awscr::CloudWatch::MetricDataQuery), **options)
Source
get_metric_statistics(namespace : String, metric_name : String, **options)
Source
get_metric_widget_image(metric_widget : String)
Source
list_dashboards
Source
list_metrics(namespace : String | Nil = nil, **options)
Source
put_anomaly_detector(namespace : String, metric_name : String, stat : String, **options) : Nil
Source
put_composite_alarm(alarm_name : String, alarm_rule : String, **options) : Nil
Source
put_counter(namespace : String, metric_name : String, value : Number = 1, **options) : Nil

Publishes a single Count value.

Source
put_dashboard(dashboard_name : String, dashboard_body : String) : Nil
Source
put_metric_alarm(alarm_name : String, **options) : Nil
Source
put_metric_data(namespace : String, metric_data : Array(Awscr::CloudWatch::MetricDatum)) : Nil

Publishes up to 1000 data points (1 MB) in one request.

Source
set_alarm_state(alarm_name : String, state_value : String, state_reason : String, state_reason_data : String | Nil = nil) : Nil
Source
with_native_client

Runs the block with the native client, for everything that is not wrapped above. Expired credentials are refreshed, like for the wrapped calls.

client.with_native_client &.metrics.put_metric_stream("Stream", "arn:aws:firehose:...", "arn:aws:iam::...", "JSON")
client.with_native_client &.alarms.tag_resource(alarm_arn, {"Env" => "prod"})
Source

Nested types