Skip to content

Commit fb142ef

Browse files
authored
Merge pull request #6743 from FireLemons/performance_monitoring
correct gc stats not coming from web server process
2 parents ebcd354 + 6d091b6 commit fb142ef

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

app/controllers/health_controller.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class HealthController < ApplicationController
22
skip_before_action :authenticate_user!
33
skip_after_action :verify_authorized
4+
before_action :verify_token_for_gc_stats, only: [:gc]
45

56
def index
67
respond_to do |format|
@@ -12,6 +13,14 @@ def index
1213
end
1314
end
1415

16+
def gc
17+
render body: JSON.pretty_generate([
18+
Time.now.in_time_zone("Central Time (US & Canada)").strftime("%H"),
19+
GC.stat
20+
]),
21+
content_type: "application/json"
22+
end
23+
1524
def case_contacts_creation_times_in_last_week
1625
case_contacts_created_in_last_week = CaseContact.where("created_at >= ?", 1.week.ago)
1726

@@ -67,4 +76,12 @@ def monthly_unique_users_graph_data
6776

6877
render json: monthly_line_graph_combined_data
6978
end
79+
80+
private
81+
82+
def verify_token_for_gc_stats
83+
gc_access_token = ENV["GC_ACCESS_TOKEN"]
84+
85+
head :forbidden unless params[:token] == gc_access_token && !gc_access_token.nil?
86+
end
7087
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
resources :health, only: %i[index] do
4444
collection do
4545
get :case_contacts_creation_times_in_last_week
46+
get :gc
4647
get :monthly_line_graph_data
4748
get :monthly_unique_users_graph_data
4849
end

lib/tasks/post_gc_stat_to_discord.rake

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
desc "Post gc stats to discord channel"
22

33
task post_gc_stat_to_discord: :environment do
4-
stats = GC.stat
4+
require "net/http"
5+
6+
url = URI("https://casavolunteertracking.org/health/gc?token=#{ENV["GC_ACCESS_TOKEN"]}")
7+
response = Net::HTTP.get_response(url)
8+
9+
unless response.is_a?(Net::HTTPSuccess)
10+
raise "Failed to fetch GC stats. HTTP status code:#{response.code}"
11+
end
12+
13+
stats = response.body
514

615
unless ENV["DISCORD_WEBHOOK_URL"].nil?
7-
formatted_stats = JSON.pretty_generate(stats)
816
discord_message = <<~MULTILINE
917
```json
10-
#{formatted_stats}
18+
#{stats}
1119
```
1220
MULTILINE
1321

0 commit comments

Comments
 (0)