Skip to content

Commit

Permalink
filter dynamic ids from prometheus label (#3784)
Browse files Browse the repository at this point in the history
  • Loading branch information
omgitsbillryan authored Jan 20, 2020
1 parent 465ebf4 commit 10b67a2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
1 change: 0 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ GEM
pundit (1.1.0)
activesupport (>= 3.0.0)
raabro (1.1.6)
rack (2.1.1)
rack-attack (6.2.1)
rack (>= 1.0, < 3)
rack-cors (1.1.1)
Expand Down
48 changes: 32 additions & 16 deletions lib/breakers/statsd_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,7 @@ class StatsdPlugin
def get_tags(request)
tags = []
if request
if request.url&.path
# replace identifiers with 'xxx'
# this nasty-looking regex attempts to cover:
# * (possibly negative) digit identifiers
# * uuid's with or without dashes
# * institution id's of form 111A2222 or 11A22222
digit = /\-?\d+/
contact_id = /\d{10}V\d{6}(%5ENI%5E200M%5EUSVHA)*/
uuids = /[a-fA-F0-9]{8}(\-?[a-fA-F0-9]{4}){3}\-?[a-fA-F0-9]{12}/
institution_ids = /[\dA-Z]{8}/
provider_ids = /Providers\(\d{10}\)/
r = %r{(\/)(#{digit}|#{contact_id}|#{uuids}|#{institution_ids}|#{provider_ids})(\/|$)}
endpoint = request.url.path.gsub(r, '\1xxx\5')
tags.append("endpoint:#{endpoint}")
end

tags.append("endpoint:#{filtered_endpoint_tag(request.url.path)}") if request.url&.path
tags.append("method:#{request.method}") if request.method
end
tags
Expand All @@ -46,5 +31,36 @@ def send_metric(status, service, request_env, response_env)
StatsD.measure(metric_base + 'time', response_env[:duration], tags: tags)
end
end

private

def filtered_endpoint_tag(path)
# replace identifiers with 'xxx'
# this nasty-looking regex attempts to cover:
# * (possibly negative) digit identifiers
# * uuid's with or without dashes
# * institution id's of form 111A2222 or 11A22222
digit = /\-?\d+/
contact_id = /\d{10}V\d{6}(%5ENI%5E200M%5EUSVHA)*/
uuids = /[a-fA-F0-9]{8}(\-?[a-fA-F0-9]{4}){3}\-?[a-fA-F0-9]{12}/
institution_ids = /[\dA-Z]{8}/
provider_ids = /Providers\(\d{10}\)/
v1_user_ids_1 = /[0-9a-f]{22}$/ # e.g. /api/v1/users/bb9c0f499977be68611151
v1_user_ids_2 = /00u2[0-9a-zA-Z]{16}/ # e.g. /api/v1/users/00u2gskfa6kXUvU0N792

r = %r{
(\/)
(#{digit}
|#{contact_id}
|#{uuids}
|#{institution_ids}
|#{provider_ids}
|#{v1_user_ids_1}
|#{v1_user_ids_2})
(\/|$)
}x

path.gsub(r, '\1xxx\5')
end
end
end
16 changes: 16 additions & 0 deletions spec/lib/breakers/statsd_plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@

request.url = URI(test_host + '/v0.0/Providers(1234567890)/bar')
expect(subject.get_tags(request)).to include('endpoint:/v0.0/xxx/bar')

request.url = URI(test_host + '/api/v1/users/b3363659ac50d661149470')
expect(subject.get_tags(request)).to include('endpoint:/api/v1/users/xxx')

request.url = URI(test_host + '/api/v1/users/00u2sgjcthlgio12o297')
expect(subject.get_tags(request)).to include('endpoint:/api/v1/users/xxx')

request.url = URI(test_host + '/api/v1/users/00u2i1p2u2m3l7FYb712/grants')
expect(subject.get_tags(request)).to include('endpoint:/api/v1/users/xxx/grants')
end
end

context 'request without an id' do
it 'doesnt replace anything' do
request.url = URI(test_host + '/foo/bar')
expect(subject.get_tags(request)).to include('endpoint:/foo/bar')
end
end
end
Expand Down

0 comments on commit 10b67a2

Please sign in to comment.