Skip to content

Commit 171a0c2

Browse files
authored
Send balance and expiry metrics in the alphasms balance check rake task (#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
1 parent 3b5f09a commit 171a0c2

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

lib/tasks/alpha_sms.rake

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
namespace :alpha_sms do
2-
desc "Fetch account balance and log if we're running low or close to expiry"
2+
desc "Fetch alphasms account balance"
33
task check_balance: :environment do
4-
max_cost_per_day = 5000
5-
alert_in_days = 5
6-
7-
response = Messaging::AlphaSms::Api.new
8-
.get_account_balance
9-
.with_indifferent_access
4+
response = Messaging::AlphaSms::Api.new.get_account_balance.with_indifferent_access
105
expiry_date = response.dig(:data, :validity)&.to_date
116
balance_amount = response.dig(:data, :balance)&.to_f
127

13-
if expiry_date < alert_in_days.days.from_now
14-
Rails.logger.error("AlphaSms account balance expires in less than #{alert_in_days}. Please recharge before #{expiry_date}.")
15-
elsif balance_amount < (alert_in_days * max_cost_per_day)
16-
Rails.logger.error("Alphasms account balance remaining is #{balance_amount} BDT. May expire in less than #{alert_in_days} days.")
17-
end
8+
Statsd.instance.gauge("alpha_sms_balance_bdt", balance_amount)
9+
Statsd.instance.gauge("alpha_sms_balance_days_till_expiry", (expiry_date - Date.current).to_i)
10+
Statsd.instance.flush
11+
1812
print("Balance: #{balance_amount} BDT\nExpiry: #{expiry_date}")
1913
end
2014
end

0 commit comments

Comments
 (0)