diff --git a/api/v01/api.rb b/api/v01/api.rb index 2fc4583..3207960 100644 --- a/api/v01/api.rb +++ b/api/v01/api.rb @@ -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 diff --git a/api/v01/entities/metrics.rb b/api/v01/entities/metrics.rb new file mode 100644 index 0000000..c66f76a --- /dev/null +++ b/api/v01/entities/metrics.rb @@ -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: +# +# + +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 diff --git a/config/access.rb b/config/access.rb index e046fd5..02749f7 100644 --- a/config/access.rb +++ b/config/access.rb @@ -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 diff --git a/test/api/v01/api_test.rb b/test/api/v01/api_test.rb index 130e52a..5a60bc0 100644 --- a/test/api/v01/api_test.rb +++ b/test/api/v01/api_test.rb @@ -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