Skip to content

Commit 93efb5f

Browse files
committed
DEV: add tmp_dir_available_bytes metric
The Discourse application and plugins utilise the `/tmp` directory for local storage during request and job processing. If the directory has no space remaining, attempting to create files will fail. Exposing the amount of space available in the directory is useful in environments where this directory is limited in size. See t/111786.
1 parent 8998a36 commit 93efb5f

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

lib/collector.rb

+5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ def ensure_global_metrics
185185
"The highest last_value from the pg_sequences table",
186186
)
187187

188+
global_metrics << Gauge.new(
189+
"tmp_dir_available_bytes",
190+
"Available space in /tmp directory (bytes)",
191+
)
192+
188193
@global_metrics = global_metrics
189194
end
190195

lib/internal_metric/global.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class Global < Base
2727
:missing_s3_uploads,
2828
:version_info,
2929
:readonly_sites,
30-
:postgres_highest_sequence
30+
:postgres_highest_sequence,
31+
:tmp_dir_available_bytes
3132

3233
def initialize
3334
@active_app_reqs = 0
@@ -158,6 +159,8 @@ def collect
158159
@readonly_sites = collect_readonly_sites
159160

160161
@postgres_highest_sequence = calc_postgres_highest_sequence
162+
163+
@tmp_dir_available_bytes = collect_dir_stats("/tmp")
161164
end
162165

163166
# For testing purposes
@@ -180,6 +183,21 @@ def collect_readonly_sites
180183
result
181184
end
182185

186+
def collect_dir_stats(dir)
187+
stdout, status = Open3.capture2("df", "-B1", dir)
188+
return nil if !status.success?
189+
190+
begin
191+
dirstat = stdout.lines[-1].split()
192+
dirstat[3].to_i
193+
rescue Exception => e
194+
Discourse.warn_exception(
195+
e,
196+
message: "Failed to read disk usage for tmp_dir_available_bytes metric",
197+
)
198+
end
199+
end
200+
183201
def primary_site_readonly?
184202
return 1 if !defined?(Discourse::PG_READONLY_MODE_KEY)
185203
Discourse.redis.without_namespace.get("default:#{Discourse::PG_READONLY_MODE_KEY}") ? 1 : 0

spec/lib/internal_metric/global_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
expect(metric.postgres_replica_available).to eq(nil)
1515
expect(metric.redis_primary_available).to eq({ { type: "main" } => 1 })
1616
expect(metric.redis_replica_available).to eq({ { type: "main" } => 0 })
17+
expect(metric.tmp_dir_available_bytes).to be > 0
1718
end
1819

1920
it "collects the version_info metric" do

0 commit comments

Comments
 (0)