Skip to content

Fix internal api metrics#244

Merged
davidor merged 4 commits intomasterfrom
fix-internal-api-metrics
Dec 3, 2020
Merged

Fix internal api metrics#244
davidor merged 4 commits intomasterfrom
fix-internal-api-metrics

Conversation

@davidor
Copy link
Copy Markdown
Contributor

@davidor davidor commented Dec 2, 2020

Fixes THREESCALE-6453

Some metrics of the internal API were not being counted correctly. The reason is that the array of regexes that we use to determine the counter to update was not sorted properly.

This issue was not caught by our test suite, so I added tests for all the requests types.

@davidor davidor requested a review from unleashed December 2, 2020 16:50
Copy link
Copy Markdown
Contributor

@unleashed unleashed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but check the comments out for any optional stuff you might want to consider.

Comment thread spec/server/listener_metrics_spec.rb Outdated
request_types = [
[
'alerts',
Proc.new { get_alerts_internal_api(service_id) }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use proc as a shorthand, or lambda if checking that the arity is right and the body does not do awesome but confusing things like return on behalf of the caller.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Comment thread spec/server/listener_metrics_spec.rb Outdated
]
]

request_types.each do |(request_type, proc)|
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call the 2nd parameter blk to be more idiomatic and avoid confusion with proc from Kernel#proc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed 👍

Comment thread spec/server/listener_metrics_spec.rb Outdated

request_types.each do |(request_type, proc)|
n_calls = rand(1..5)
n_calls.times { proc.call }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n_calls.times &blk

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Comment on lines +298 to +352
def do_internal_api_get_req(path)
do_get_req("/internal#{path}")
end

def do_internal_api_delete_req(path)
do_delete_req("/internal#{path}")
end

def get_service_internal_api(service_id)
do_internal_api_get_req("/services/#{service_id}")
end

def get_alerts_internal_api(service_id)
do_internal_api_get_req("/services/#{service_id}/alert_limits/")
end

def get_app_keys_internal_api(service_id, app_id)
do_internal_api_get_req("/services/#{service_id}/applications/#{app_id}/keys/")
end

def get_app_referrer_filters_internal_api(service_id, app_id)
do_internal_api_get_req("/services/#{service_id}/applications/#{app_id}/referrer_filters")
end

def get_app_internal_api(service_id, app_id)
do_internal_api_get_req("/services/#{service_id}/applications/#{app_id}")
end

def get_errors_internal_api(service_id)
do_internal_api_get_req("/services/#{service_id}/errors/")
end

def get_events_internal_api
do_internal_api_get_req("/events/")
end

def get_metric_internal_api(service_id, metric_id)
do_internal_api_get_req("/services/#{service_id}/metrics/#{metric_id}")
end

def get_service_token_internal_api(token, service_id)
do_internal_api_get_req("/service_tokens/#{token}/#{service_id}/provider_key")
end

def delete_stats_internal_api(service_id)
do_internal_api_delete_req("/services/#{service_id}/stats")
end

def get_usage_limits_internal_api(service_id, plan_id, metric_id, period)
do_internal_api_get_req("/services/#{service_id}/plans/#{plan_id}/usagelimits/" +
"#{metric_id}/#{period}")
end

def get_utilization_internal_api(service_id, app_id)
do_internal_api_get_req("/services/#{service_id}/applications/#{app_id}/utilization/")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a magnificient opportunity to generate these programmatically by iterating a hashmap with method names and paths with placeholders for arguments. :D

... but now it's done and we wouldn't gain anything from it at this point, so 👍.

Comment on lines +16 to +17
# Only the first match is taken into account, that's why for example,
# "/\/services\/.*\/stats/" needs to appear before "/\/services/"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could write some code to ensure this gets sorted properly regardless of people ignoring this comment and breaking it. Probably not worth the effort, though... unless you start automating the information about these endpoints and generate also their associated methods for building paths usable in tests, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that but I don't think it's worth the effort. Also, now we have tests that should fail if this is not sorted.

Comment thread lib/3scale/backend/listener_metrics.rb Outdated
[/\/services\/.*\/stats/, 'stats'],
[/\/services\/.*\/plans\/.*\/usagelimits/, 'usage_limits'],
[/\/services\/.*\/applications\/.*\/utilization/, 'utilization'],
[/\/services/, 'services'],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not freezing the strings?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

@davidor davidor force-pushed the fix-internal-api-metrics branch from 1ef653f to 4ba4ef0 Compare December 3, 2020 09:07
@davidor davidor merged commit a586fa8 into master Dec 3, 2020
@bors bors Bot deleted the fix-internal-api-metrics branch December 3, 2020 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants