Skip to content

Commit

Permalink
a bit of refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
kinnou02 committed Mar 28, 2018
1 parent bbe92ee commit e427aea
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ add_subdirectory(third_party/librabbitmq-c)
add_subdirectory(third_party/SimpleAmqpClient)
#prometheus-cpp cmake will refuse to build if the CMAKE_INSTALL_PREFIX is empty
#setting it before will have side effects on how we build packages
SET(CMAKE_INSTALL_PREFIX "/")
SET(CMAKE_INSTALL_PREFIX "/usr/local")
add_subdirectory(third_party/prometheus-cpp)
9 changes: 6 additions & 3 deletions source/kraken/kraken_zmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,19 @@ int main(int argn, char** argv){
return 1;
}

navitia::Metrics metrics(conf.metrics_binding());
const navitia::Metrics metrics(conf.metrics_binding(), conf.instance_name());

threads.create_thread(navitia::MaintenanceWorker(data_manager, conf));


int nb_threads = conf.nb_threads();
// Launch pool of worker threads
LOG4CPLUS_INFO(logger, "starting workers threads");
for(int thread_nbr = 0; thread_nbr < nb_threads; ++thread_nbr) {
threads.create_thread(std::bind(&doWork, std::ref(context), std::ref(data_manager), conf, std::ref(metrics)));
threads.create_thread(std::bind(&doWork,
std::ref(context),
std::ref(data_manager),
conf,
std::ref(metrics)));
}

// Connect worker threads to client threads via a queue
Expand Down
1 change: 1 addition & 0 deletions source/kraken/kraken_zmq.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ inline void doWork(zmq::context_t& context,
//on gére le cas du sighup durant un recv
continue;
}

pbnavitia::Request pb_req;
pt::ptime start = pt::microsec_clock::universal_time();
pbnavitia::API api = pbnavitia::UNKNOWN_API;
Expand Down
21 changes: 15 additions & 6 deletions source/kraken/metrics.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

/* Copyright © 2001-2014, Canal TP and/or its affiliates. All rights reserved.
/* Copyright © 2001-2018, Canal TP and/or its affiliates. All rights reserved.
This file is part of Navitia,
the software to build cool stuff with public transport.
Expand Down Expand Up @@ -35,6 +34,7 @@ www.navitia.io
#include <prometheus/exposer.h>
#include <prometheus/registry.h>
#include <prometheus/counter.h>
#include "utils/logger.h"

namespace navitia {

Expand All @@ -51,22 +51,25 @@ static prometheus::Histogram::BucketBoundaries create_exponential_buckets(double
}


Metrics::Metrics(const boost::optional<std::string>& endpoint){
Metrics::Metrics(const boost::optional<std::string>& endpoint, const std::string& coverage){
if(endpoint == boost::none){
return;
}
auto logger = log4cplus::Logger::getInstance("metrics");
LOG4CPLUS_INFO(logger, "metrics available at http://" << *endpoint << "/metrics");
exposer = std::make_unique<prometheus::Exposer>(*endpoint);
registry = std::make_shared<prometheus::Registry>();
exposer->RegisterCollectable(registry);

auto& histogram_family = prometheus::BuildHistogram()
.Name("kraken_request_duration")
.Name("kraken_request_duration_seconds")
.Help("duration of request in seconds")
.Labels({{"coverage", coverage}})
.Register(*registry);
auto desc = pbnavitia::API_descriptor();
for(int i = 0; i < desc->value_count(); ++i){
auto value = desc->value(i);
auto& histo = histogram_family.Add({{"api", value->name()}}, create_exponential_buckets(0.001, 25, 1.5));
auto& histo = histogram_family.Add({{"api", value->name()}}, create_exponential_buckets(0.001, 1.5, 25));
this->request_histogram[static_cast<pbnavitia::API>(value->number())] = &histo;
}
}
Expand All @@ -75,7 +78,13 @@ void Metrics::observe_api(pbnavitia::API api, float duration) const{
if(!registry) {
return;
}
this->request_histogram.at(api)->Observe(duration);
auto it = this->request_histogram.find(api);
if(it != std::end(this->request_histogram)){
it->second->Observe(duration);
}else{
auto logger = log4cplus::Logger::getInstance("metrics");
LOG4CPLUS_WARN(logger, "api " << pbnavitia::API_Name(api) << " not found in metrics");
}
}


Expand Down
5 changes: 2 additions & 3 deletions source/kraken/metrics.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

/* Copyright © 2001-2014, Canal TP and/or its affiliates. All rights reserved.
/* Copyright © 2001-2018, Canal TP and/or its affiliates. All rights reserved.
This file is part of Navitia,
the software to build cool stuff with public transport.
Expand Down Expand Up @@ -54,7 +53,7 @@ class Metrics: boost::noncopyable {
std::map<pbnavitia::API, prometheus::Histogram*> request_histogram;

public:
Metrics(const boost::optional<std::string>& endpoint);
Metrics(const boost::optional<std::string>& endpoint, const std::string& coverage);
void observe_api(pbnavitia::API api, float duration) const;

};
Expand Down
2 changes: 1 addition & 1 deletion source/tests/mock_kraken.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct mock_kraken {
threads.create_thread(navitia::MaintenanceWorker(data_manager, conf));
}

navitia::Metrics metric(boost::none);
navitia::Metrics metric(boost::none, "mock");
// Launch only one thread for the tests
threads.create_thread(std::bind(&doWork, std::ref(context), std::ref(data_manager), conf, std::ref(metric)));

Expand Down

0 comments on commit e427aea

Please sign in to comment.