Skip to content

Commit

Permalink
Send balance and expiry metrics in the alphasms balance check rake ta…
Browse files Browse the repository at this point in the history
…sk (#5379)

**Story card:**
[sc-11640](https://app.shortcut.com/simpledotorg/story/11640/automate-alphasms-balance-check)

## Because

- We want to continue to track this via Prometheus, and metrics are a
better way to do this than by logs
- We will be able to graph our usage with metrics

## Considerations

- Since we're still sending metrics via statsd and are planning to use
an exporter to translate these metrics to the Prometheus format, the
metrics in this PR are also being sent via Statsd. The format is
standard for both Statsd and Prometheus
- We could send the expiry days as a label on the balance metric instead
of as a second metric. The statsd exporter supports conversion of Statsd
tags into Prometheus labels. Keeping them as separate metrics for now
since the Prometheus migration's implementation details are unknown
- I've removed the older logs, will set up metric-based alerts on
Datadog until the move to Prometheus is complete
  • Loading branch information
qptr authored Jan 25, 2024
1 parent 3b5f09a commit 171a0c2
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions lib/tasks/alpha_sms.rake
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
namespace :alpha_sms do
desc "Fetch account balance and log if we're running low or close to expiry"
desc "Fetch alphasms account balance"
task check_balance: :environment do
max_cost_per_day = 5000
alert_in_days = 5

response = Messaging::AlphaSms::Api.new
.get_account_balance
.with_indifferent_access
response = Messaging::AlphaSms::Api.new.get_account_balance.with_indifferent_access
expiry_date = response.dig(:data, :validity)&.to_date
balance_amount = response.dig(:data, :balance)&.to_f

if expiry_date < alert_in_days.days.from_now
Rails.logger.error("AlphaSms account balance expires in less than #{alert_in_days}. Please recharge before #{expiry_date}.")
elsif balance_amount < (alert_in_days * max_cost_per_day)
Rails.logger.error("Alphasms account balance remaining is #{balance_amount} BDT. May expire in less than #{alert_in_days} days.")
end
Statsd.instance.gauge("alpha_sms_balance_bdt", balance_amount)
Statsd.instance.gauge("alpha_sms_balance_days_till_expiry", (expiry_date - Date.current).to_i)
Statsd.instance.flush

print("Balance: #{balance_amount} BDT\nExpiry: #{expiry_date}")
end
end

0 comments on commit 171a0c2

Please sign in to comment.