class

EasyAwscr::CloudWatch::Counters

Inherits Reference < Object

Counts events in memory and uploads them to CloudWatch: one data point per metric and flush interval, so a busy application sends one request per minute instead of one per event.

By default, a background loop uploads the counters periodically. With flush_interval: nil there is no loop and flush uploads on demand.

Configure once, afterwards it is only count:

metrics = EasyAwscr::CloudWatch::Counters.new("MyApp")
metrics.count("Requests")

Constants

MAX_METRICS_PER_REQUEST = 1000

AWS allows at most 1000 data points and 1 MB per request. The size grows with the length of the metric names and dimensions, but stays well below 1 MB for typical counters (a full batch is around 300 KB).

Constructors

new(namespace : String, *, client : Client = Client.new, flush_interval : Time::Span | Nil = 1.minute, dimensions : Hash(String, String) | Nil = nil)

flush_interval starts a background loop that uploads the counters periodically (every minute by default). nil starts no loop: call flush to upload instead. dimensions are attached to every metric, e.g. {"Environment" => "Dev"}.

Source

Instance methods

close

Sends what is left and stops the background upload, e.g. at shutdown. Later calls to close or flush are ignored.

Source
closed?

Whether close was called. Once closed, nothing is uploaded anymore: counters counted afterwards stay in pending (flush is a no-op).

Source
count(name : String, by : Int = 1) : Nil
Source
flush

Uploads everything counted so far. What could not be uploaded stays and goes out with the next flush. Ignored after close.

Source
pending

Counts not uploaded yet, by metric name. For debugging and tests.

Source