Commit 894f01cd authored by Adam Niedzielski's avatar Adam Niedzielski

Include endpoint in metrics for ETag caching middleware

parent 49bdd8d6
---
title: Include endpoint in metrics for ETag caching middleware
merge_request: 10495
author:
...@@ -2,26 +2,34 @@ module Gitlab ...@@ -2,26 +2,34 @@ module Gitlab
module EtagCaching module EtagCaching
class Middleware class Middleware
RESERVED_WORDS = NamespaceValidator::WILDCARD_ROUTES.map { |word| "/#{word}/" }.join('|') RESERVED_WORDS = NamespaceValidator::WILDCARD_ROUTES.map { |word| "/#{word}/" }.join('|')
ROUTE_REGEXP = Regexp.union( ROUTES = [
%r(^(?!.*(#{RESERVED_WORDS})).*/noteable/issue/\d+/notes\z), {
%r(^(?!.*(#{RESERVED_WORDS})).*/issues/\d+/rendered_title\z) regexp: %r(^(?!.*(#{RESERVED_WORDS})).*/noteable/issue/\d+/notes\z),
) name: 'issue_notes'
},
{
regexp: %r(^(?!.*(#{RESERVED_WORDS})).*/issues/\d+/rendered_title\z),
name: 'issue_title'
}
].freeze
def initialize(app) def initialize(app)
@app = app @app = app
end end
def call(env) def call(env)
return @app.call(env) unless enabled_for_current_route?(env) route = match_current_route(env)
Gitlab::Metrics.add_event(:etag_caching_middleware_used) return @app.call(env) unless route
track_event(:etag_caching_middleware_used, route)
etag, cached_value_present = get_etag(env) etag, cached_value_present = get_etag(env)
if_none_match = env['HTTP_IF_NONE_MATCH'] if_none_match = env['HTTP_IF_NONE_MATCH']
if if_none_match == etag if if_none_match == etag
handle_cache_hit(etag) handle_cache_hit(etag, route)
else else
track_cache_miss(if_none_match, cached_value_present) track_cache_miss(if_none_match, cached_value_present, route)
status, headers, body = @app.call(env) status, headers, body = @app.call(env)
headers['ETag'] = etag headers['ETag'] = etag
...@@ -31,8 +39,8 @@ module Gitlab ...@@ -31,8 +39,8 @@ module Gitlab
private private
def enabled_for_current_route?(env) def match_current_route(env)
ROUTE_REGEXP.match(env['PATH_INFO']) ROUTES.find { |route| route[:regexp].match(env['PATH_INFO']) }
end end
def get_etag(env) def get_etag(env)
...@@ -52,23 +60,27 @@ module Gitlab ...@@ -52,23 +60,27 @@ module Gitlab
%Q{W/"#{value}"} %Q{W/"#{value}"}
end end
def handle_cache_hit(etag) def handle_cache_hit(etag, route)
Gitlab::Metrics.add_event(:etag_caching_cache_hit) track_event(:etag_caching_cache_hit, route)
status_code = Gitlab::PollingInterval.polling_enabled? ? 304 : 429 status_code = Gitlab::PollingInterval.polling_enabled? ? 304 : 429
[status_code, { 'ETag' => etag }, ['']] [status_code, { 'ETag' => etag }, ['']]
end end
def track_cache_miss(if_none_match, cached_value_present) def track_cache_miss(if_none_match, cached_value_present, route)
if if_none_match.blank? if if_none_match.blank?
Gitlab::Metrics.add_event(:etag_caching_header_missing) track_event(:etag_caching_header_missing, route)
elsif !cached_value_present elsif !cached_value_present
Gitlab::Metrics.add_event(:etag_caching_key_not_found) track_event(:etag_caching_key_not_found, route)
else else
Gitlab::Metrics.add_event(:etag_caching_resource_changed) track_event(:etag_caching_resource_changed, route)
end end
end end
def track_event(name, route)
Gitlab::Metrics.add_event(name, endpoint: route[:name])
end
end end
end end
end end
...@@ -47,9 +47,9 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -47,9 +47,9 @@ describe Gitlab::EtagCaching::Middleware do
it 'tracks "etag_caching_key_not_found" event' do it 'tracks "etag_caching_key_not_found" event' do
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_middleware_used) .with(:etag_caching_middleware_used, endpoint: 'issue_notes')
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_key_not_found) .with(:etag_caching_key_not_found, endpoint: 'issue_notes')
middleware.call(build_env(path, if_none_match)) middleware.call(build_env(path, if_none_match))
end end
...@@ -93,9 +93,9 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -93,9 +93,9 @@ describe Gitlab::EtagCaching::Middleware do
it 'tracks "etag_caching_cache_hit" event' do it 'tracks "etag_caching_cache_hit" event' do
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_middleware_used) .with(:etag_caching_middleware_used, endpoint: 'issue_notes')
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_cache_hit) .with(:etag_caching_cache_hit, endpoint: 'issue_notes')
middleware.call(build_env(path, if_none_match)) middleware.call(build_env(path, if_none_match))
end end
...@@ -132,9 +132,9 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -132,9 +132,9 @@ describe Gitlab::EtagCaching::Middleware do
mock_app_response mock_app_response
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_middleware_used) .with(:etag_caching_middleware_used, endpoint: 'issue_notes')
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_resource_changed) .with(:etag_caching_resource_changed, endpoint: 'issue_notes')
middleware.call(build_env(path, if_none_match)) middleware.call(build_env(path, if_none_match))
end end
...@@ -150,9 +150,9 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -150,9 +150,9 @@ describe Gitlab::EtagCaching::Middleware do
it 'tracks "etag_caching_header_missing" event' do it 'tracks "etag_caching_header_missing" event' do
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_middleware_used) .with(:etag_caching_middleware_used, endpoint: 'issue_notes')
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_header_missing) .with(:etag_caching_header_missing, endpoint: 'issue_notes')
middleware.call(build_env(path, if_none_match)) middleware.call(build_env(path, if_none_match))
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment