Skip to content

Commit

Permalink
FEATURE: Add content_type label to web performance metrics (#65)
Browse files Browse the repository at this point in the history
For now, `content_type` label can either be `json`, `html` or `other` to
limit the cardinality of the metrics.
  • Loading branch information
tgxworld authored Apr 11, 2023
1 parent 6275c8a commit efcee66
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,12 @@ def process_web(metric)
# STDERR.puts metric.to_h.inspect
# STDERR.puts metric.controller.to_s + " " + metric.action.to_s

labels = { cache: !!metric.cache, success: (200..299).include?(metric.status_code) }
labels = {
cache: !!metric.cache,
success: (200..299).include?(metric.status_code),
content_type: web_metric_content_type(metric),
logged_in: metric.logged_in,
}

if observe_timings?(metric)
labels[:controller] = metric.controller
Expand All @@ -269,8 +274,6 @@ def process_web(metric)
labels[:action] = "other"
end

labels[:logged_in] = metric.logged_in

@http_duration_seconds.observe(metric.duration, labels)
@http_sql_duration_seconds.observe(metric.sql_duration, labels)
@http_redis_duration_seconds.observe(metric.redis_duration, labels)
Expand Down Expand Up @@ -399,5 +402,15 @@ def observe_timings?(metric)
(metric.controller == "users" && metric.action == "show") ||
(metric.controller == "categories" && metric.action == "categories_and_latest")
end

def web_metric_content_type(metric)
if metric.json
"json"
elsif metric.html
"html"
else
"other"
end
end
end
end
4 changes: 4 additions & 0 deletions lib/internal_metric/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Web < Base
mobile
tracked
json
html
admin_api
user_api
forced_anon
Expand Down Expand Up @@ -97,6 +98,9 @@ def self.from_env_data(env, data, host)
env["PATH_INFO"].to_s.ends_with?(".json") ||
env["HTTP_ACCEPT"].to_s.include?("application/json")

metric.html =
env["PATH_INFO"].to_s.ends_with?(".html") || env["HTTP_ACCEPT"].to_s.include?("text/html")

metric.ajax = env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
metric.forced_anon = !!env["DISCOURSE_FORCE_ANON"]

Expand Down
4 changes: 4 additions & 0 deletions spec/lib/collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ module DiscoursePrometheus
sql_duration: 1,
redis_duration: 3,
net_duration: 5,
json: true,
controller: "list",
action: "latest",
logged_in: true,
Expand All @@ -272,6 +273,7 @@ module DiscoursePrometheus
action: "latest",
cache: true,
logged_in: false,
html: true,
)

collector = Collector.new
Expand All @@ -289,6 +291,7 @@ module DiscoursePrometheus
success: true,
cache: false,
logged_in: true,
content_type: "json",
} => {
"count" => 1,
"sum" => sum,
Expand All @@ -299,6 +302,7 @@ module DiscoursePrometheus
success: false,
cache: true,
logged_in: false,
content_type: "html",
} => {
"count" => 1,
"sum" => sum,
Expand Down

0 comments on commit efcee66

Please sign in to comment.