Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
894f01cd
Commit
894f01cd
authored
Apr 06, 2017
by
Adam Niedzielski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Include endpoint in metrics for ETag caching middleware
parent
49bdd8d6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
24 deletions
+40
-24
changelogs/unreleased/add-dimension-etag-caching-metrics.yml
changelogs/unreleased/add-dimension-etag-caching-metrics.yml
+4
-0
lib/gitlab/etag_caching/middleware.rb
lib/gitlab/etag_caching/middleware.rb
+28
-16
spec/lib/gitlab/etag_caching/middleware_spec.rb
spec/lib/gitlab/etag_caching/middleware_spec.rb
+8
-8
No files found.
changelogs/unreleased/add-dimension-etag-caching-metrics.yml
0 → 100644
View file @
894f01cd
---
title
:
Include endpoint in metrics for ETag caching middleware
merge_request
:
10495
author
:
lib/gitlab/etag_caching/middleware.rb
View file @
894f01cd
...
...
@@ -2,26 +2,34 @@ module Gitlab
module
EtagCaching
class
Middleware
RESERVED_WORDS
=
NamespaceValidator
::
WILDCARD_ROUTES
.
map
{
|
word
|
"/
#{
word
}
/"
}.
join
(
'|'
)
ROUTE_REGEXP
=
Regexp
.
union
(
%r(^(?!.*(
#{
RESERVED_WORDS
}
)).*/noteable/issue/
\d
+/notes
\z
)
,
%r(^(?!.*(
#{
RESERVED_WORDS
}
)).*/issues/
\d
+/rendered_title
\z
)
)
ROUTES
=
[
{
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
)
@app
=
app
end
def
call
(
env
)
return
@app
.
call
(
env
)
unless
enabled_for_current_route?
(
env
)
Gitlab
::
Metrics
.
add_event
(
:etag_caching_middleware_used
)
route
=
match_current_route
(
env
)
return
@app
.
call
(
env
)
unless
route
track_event
(
:etag_caching_middleware_used
,
route
)
etag
,
cached_value_present
=
get_etag
(
env
)
if_none_match
=
env
[
'HTTP_IF_NONE_MATCH'
]
if
if_none_match
==
etag
handle_cache_hit
(
etag
)
handle_cache_hit
(
etag
,
route
)
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
)
headers
[
'ETag'
]
=
etag
...
...
@@ -31,8 +39,8 @@ module Gitlab
private
def
enabled_for_current_route?
(
env
)
ROUTE
_REGEXP
.
match
(
env
[
'PATH_INFO'
])
def
match_current_route
(
env
)
ROUTE
S
.
find
{
|
route
|
route
[
:regexp
].
match
(
env
[
'PATH_INFO'
])
}
end
def
get_etag
(
env
)
...
...
@@ -52,22 +60,26 @@ module Gitlab
%Q{W/"
#{
value
}
"}
end
def
handle_cache_hit
(
etag
)
Gitlab
::
Metrics
.
add_event
(
:etag_caching_cache_hit
)
def
handle_cache_hit
(
etag
,
route
)
track_event
(
:etag_caching_cache_hit
,
route
)
status_code
=
Gitlab
::
PollingInterval
.
polling_enabled?
?
304
:
429
[
status_code
,
{
'ETag'
=>
etag
},
[
''
]]
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?
Gitlab
::
Metrics
.
add_event
(
:etag_caching_header_missing
)
track_event
(
:etag_caching_header_missing
,
route
)
elsif
!
cached_value_present
Gitlab
::
Metrics
.
add_event
(
:etag_caching_key_not_found
)
track_event
(
:etag_caching_key_not_found
,
route
)
else
Gitlab
::
Metrics
.
add_event
(
:etag_caching_resource_changed
)
track_event
(
:etag_caching_resource_changed
,
route
)
end
end
def
track_event
(
name
,
route
)
Gitlab
::
Metrics
.
add_event
(
name
,
endpoint:
route
[
:name
])
end
end
end
...
...
spec/lib/gitlab/etag_caching/middleware_spec.rb
View file @
894f01cd
...
...
@@ -47,9 +47,9 @@ describe Gitlab::EtagCaching::Middleware do
it
'tracks "etag_caching_key_not_found" event'
do
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
)
.
with
(
:etag_caching_key_not_found
)
.
with
(
:etag_caching_key_not_found
,
endpoint:
'issue_notes'
)
middleware
.
call
(
build_env
(
path
,
if_none_match
))
end
...
...
@@ -93,9 +93,9 @@ describe Gitlab::EtagCaching::Middleware do
it
'tracks "etag_caching_cache_hit" event'
do
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
)
.
with
(
:etag_caching_cache_hit
)
.
with
(
:etag_caching_cache_hit
,
endpoint:
'issue_notes'
)
middleware
.
call
(
build_env
(
path
,
if_none_match
))
end
...
...
@@ -132,9 +132,9 @@ describe Gitlab::EtagCaching::Middleware do
mock_app_response
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
)
.
with
(
:etag_caching_resource_changed
)
.
with
(
:etag_caching_resource_changed
,
endpoint:
'issue_notes'
)
middleware
.
call
(
build_env
(
path
,
if_none_match
))
end
...
...
@@ -150,9 +150,9 @@ describe Gitlab::EtagCaching::Middleware do
it
'tracks "etag_caching_header_missing" event'
do
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
)
.
with
(
:etag_caching_header_missing
)
.
with
(
:etag_caching_header_missing
,
endpoint:
'issue_notes'
)
middleware
.
call
(
build_env
(
path
,
if_none_match
))
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment