Skip to content

Commit

Permalink
Add count metrics exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
alain-andre committed Nov 30, 2022
1 parent 7cd8491 commit e7c0f55
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
28 changes: 28 additions & 0 deletions api/v01/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,34 @@ def count_incr(operation, options)
end
end

##
# Use to export prometheus metrics
resource :metrics do
desc 'Return Prometheus metrics', {}
get do
error!('Unauthorized', 401) unless OptimizerWrapper.access[params[:api_key]][:metrics] == true

status 200
present(
redis_count.keys("*#{count_base_key_no_key('optimize').join(':')}*").flat_map{ |key|
hkey = split_key(key)
hredis = redis_count.hgetall(key)

{
count_asset: hkey['asset'],
count_date: hkey['date'],
count_endpoint: hkey['endpoint'],
count_hits: hredis['hits'],
count_ip: hkey['ip'],
count_key: hkey['key'],
count_service: hkey['service'],
count_transactions: hredis['transactions'],
}
}, with: Metrics
)
end
end

mount Route
mount Matrix
mount Isoline
Expand Down
37 changes: 37 additions & 0 deletions api/v01/entities/metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@@ -0,0 +1,36 @@
# Copyright © Mapotempo, 2015
#
# This file is part of Mapotempo.
#
# Mapotempo is free software. You can redistribute it and/or
# modify since you respect the terms of the GNU Affero General
# Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Mapotempo is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Mapotempo. If not, see:
# <http://www.gnu.org/licenses/agpl.html>
#

module Api
module V01
class Metrics < Grape::Entity
def self.entity_name
'Metrics'
end

expose(:count_asset, documentation: {type: String, desc: 'Asset given to the request' })
expose(:count_date, documentation: {type: Date, desc: 'The date of the request' })
expose(:count_endpoint, documentation: {type: String, desc: 'Endpoint of the request' })
expose(:count_hits, documentation: {type: Integer, desc: 'Hits of the request' })
expose(:count_ip, documentation: {type: String, desc: 'IP of the request' })
expose(:count_key, documentation: {type: String, desc: 'Key used for the request' })
expose(:count_service, documentation: {type: String, desc: 'Service used for the request' })
expose(:count_transactions, documentation: {type: Integer, desc: 'Transactions in the service for the request' })
end
end
end
1 change: 1 addition & 0 deletions config/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module RouterWrapper
{ operation: :isoline, daily: nil },
{ operation: :matrix, daily: nil }
]},
'metrics' => { profile: :demo, params_limit: { points: nil, vehicles: nil }, metrics: true},
'expired' => { profile: :standard, expire_at: '2000-01-01' }
}
end
23 changes: 23 additions & 0 deletions test/api/v01/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,27 @@ def test_should_not_access_if_expired
assert_equal 402, last_response.status
assert_equal '402 Subscription expired', JSON.parse(last_response.body)['error']
end

def test_metrics
clear_optim_redis_count
post '/0.1/vrp/submit?asset=myAsset', { api_key: 'demo', vrp: VRP.toy }.to_json, \
'CONTENT_TYPE' => 'application/json'
assert last_response.ok?, last_response.body

get '0.1/metrics', { api_key: 'demo'}
assert_equal 401, last_response.status

get '0.1/metrics', { api_key: 'metrics'}
assert last_response.ok?, last_response.body
json = JSON.parse(last_response.body).first

assert_equal Date.today.strftime("%Y-%m-%d"), json["count_date"]
assert_equal "1", json["count_hits"]
assert_equal "1", json["count_transactions"]
assert_equal "127.0.0.1", json["count_ip"]
assert_equal "demo", json["count_key"]
assert_equal "myAsset", json["count_asset"]
assert_equal "optimizer", json["count_service"]
assert_equal "optimize", json["count_endpoint"]
end
end

0 comments on commit e7c0f55

Please sign in to comment.