Skip to content

Commit

Permalink
Add a plugin for prometheus (drogonframework#1632)
Browse files Browse the repository at this point in the history
  • Loading branch information
an-tao committed Sep 4, 2023
1 parent 4e5638f commit cedeeb5
Show file tree
Hide file tree
Showing 17 changed files with 1,134 additions and 35 deletions.
52 changes: 35 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,18 @@ endif (BUILD_BROTLI)

set(DROGON_SOURCES
lib/src/AOPAdvice.cc
lib/src/AccessLogger.cc
lib/src/CacheFile.cc
lib/src/ConfigAdapterManager.cc
lib/src/ConfigLoader.cc
lib/src/Cookie.cc
lib/src/DrClassMap.cc
lib/src/DrTemplateBase.cc
lib/src/FiltersFunction.cc
lib/src/FixedWindowRateLimiter.cc
lib/src/GlobalFilters.cc
lib/src/Histogram.cc
lib/src/Hodor.cc
lib/src/HttpAppFrameworkImpl.cc
lib/src/HttpBinder.cc
lib/src/HttpClientImpl.cc
Expand All @@ -251,33 +257,29 @@ set(DROGON_SOURCES
lib/src/HttpUtils.cc
lib/src/HttpViewData.cc
lib/src/IntranetIpFilter.cc
lib/src/JsonConfigAdapter.cc
lib/src/ListenerManager.cc
lib/src/LocalHostFilter.cc
lib/src/MultiPart.cc
lib/src/NotFound.cc
lib/src/PluginsManager.cc
lib/src/PromExporter.cc
lib/src/RangeParser.cc
lib/src/SecureSSLRedirector.cc
lib/src/GlobalFilters.cc
lib/src/AccessLogger.cc
lib/src/RateLimiter.cc
lib/src/RealIpResolver.cc
lib/src/SecureSSLRedirector.cc
lib/src/SessionManager.cc
lib/src/SlashRemover.cc
lib/src/SlidingWindowRateLimiter.cc
lib/src/StaticFileRouter.cc
lib/src/TaskTimeoutFlag.cc
lib/src/TokenBucketRateLimiter.cc
lib/src/Utilities.cc
lib/src/WebSocketClientImpl.cc
lib/src/WebSocketConnectionImpl.cc
lib/src/WebsocketControllersRouter.cc
lib/src/RateLimiter.cc
lib/src/FixedWindowRateLimiter.cc
lib/src/SlidingWindowRateLimiter.cc
lib/src/TokenBucketRateLimiter.cc
lib/src/Hodor.cc
lib/src/SlashRemover.cc
lib/src/drogon_test.cc
lib/src/ConfigAdapterManager.cc
lib/src/JsonConfigAdapter.cc
lib/src/YamlConfigAdapter.cc)
lib/src/YamlConfigAdapter.cc
lib/src/drogon_test.cc)
set(private_headers
lib/src/AOPAdvice.h
lib/src/CacheFile.h
Expand Down Expand Up @@ -689,18 +691,32 @@ set(DROGON_UTIL_HEADERS
lib/inc/drogon/utils/FunctionTraits.h
lib/inc/drogon/utils/HttpConstraint.h
lib/inc/drogon/utils/OStringStream.h
lib/inc/drogon/utils/Utilities.h)
lib/inc/drogon/utils/Utilities.h
lib/inc/drogon/utils/monitoring.h)
install(FILES ${DROGON_UTIL_HEADERS}
DESTINATION ${INSTALL_INCLUDE_DIR}/drogon/utils)

set(DROGON_MONITORING_HEADERS
lib/inc/drogon/utils/monitoring/Counter.h
lib/inc/drogon/utils/monitoring/Metric.h
lib/inc/drogon/utils/monitoring/Registry.h
lib/inc/drogon/utils/monitoring/Collector.h
lib/inc/drogon/utils/monitoring/Sample.h
lib/inc/drogon/utils/monitoring/Gauge.h
lib/inc/drogon/utils/monitoring/Histogram.h)

install(FILES ${DROGON_MONITORING_HEADERS}
DESTINATION ${INSTALL_INCLUDE_DIR}/drogon/utils/monitoring)

set(DROGON_PLUGIN_HEADERS
lib/inc/drogon/plugins/Plugin.h
lib/inc/drogon/plugins/SecureSSLRedirector.h
lib/inc/drogon/plugins/AccessLogger.h
lib/inc/drogon/plugins/RealIpResolver.h
lib/inc/drogon/plugins/Hodor.h
lib/inc/drogon/plugins/SlashRemover.h
lib/inc/drogon/plugins/GlobalFilters.h)
lib/inc/drogon/plugins/GlobalFilters.h
lib/inc/drogon/plugins/PromExporter.h)

install(FILES ${DROGON_PLUGIN_HEADERS}
DESTINATION ${INSTALL_INCLUDE_DIR}/drogon/plugins)
Expand All @@ -712,15 +728,17 @@ target_sources(${PROJECT_NAME} PRIVATE
${ORM_HEADERS}
${DROGON_UTIL_HEADERS}
${DROGON_PLUGIN_HEADERS}
${NOSQL_HEADERS})
${NOSQL_HEADERS}
${DROGON_MONITORING_HEADERS})

source_group("Public API"
FILES
${DROGON_HEADERS}
${ORM_HEADERS}
${DROGON_UTIL_HEADERS}
${DROGON_PLUGIN_HEADERS}
${NOSQL_HEADERS})
${NOSQL_HEADERS}
${DROGON_MONITORING_HEADERS})
source_group("Private Headers"
FILES
${private_headers})
Expand Down
33 changes: 15 additions & 18 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,19 @@
//0 means cache forever, the negative value means no cache
"static_files_cache_time": 5,
//simple_controllers_map: Used to configure mapping from path to simple controller
"simple_controllers_map": [
{
"path": "/path/name",
"controller": "controllerClassName",
"http_methods": [
"get",
"post"
],
"filters": [
"FilterClassName"
]
}
],
//"simple_controllers_map": [
// {
// "path": "/path/name",
// "controller": "controllerClassName",
// "http_methods": [
// "get",
// "post"
// ],
// "filters": [
// "FilterClassName"
// ]
// }
//],
//idle_connection_timeout: Defaults to 60 seconds, the lifetime
//of the connection without read or write
"idle_connection_timeout": 60,
Expand Down Expand Up @@ -307,16 +307,13 @@
"plugins": [
{
//name: The class name of the plugin
//"name": "drogon::plugin::SecureSSLRedirector",
"name": "drogon::plugin::PromExporter",
//dependencies: Plugins that the plugin depends on. It can be commented out
"dependencies": [],
//config: The configuration of the plugin. This json object is the parameter to initialize the plugin.
//It can be commented out
"config": {
"ssl_redirect_exempt": [
".*\\.jpg"
],
"secure_ssl_host": "localhost:8849"
"path": "/metrics"
}
}
],
Expand Down
1 change: 1 addition & 0 deletions lib/inc/drogon/drogon.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <drogon/plugins/Hodor.h>
#include <drogon/plugins/SlashRemover.h>
#include <drogon/plugins/GlobalFilters.h>
#include <drogon/plugins/PromExporter.h>
#include <drogon/IntranetIpFilter.h>
#include <drogon/LocalHostFilter.h>
#include <drogon/Cookie.h>
Expand Down
98 changes: 98 additions & 0 deletions lib/inc/drogon/plugins/PromExporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* @file PromExporter.h
* @author An Tao
*
* Copyright 2018, An Tao. All rights reserved.
* https://github.com/an-tao/drogon
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Drogon
*
*/

#pragma once
#include <drogon/plugins/Plugin.h>
#include <drogon/utils/monitoring/Registry.h>
#include <drogon/utils/monitoring/Collector.h>
#include <memory>
#include <mutex>

namespace drogon
{
namespace plugin
{
/**
* @brief The PromExporter plugin implements a prometheus exporter.
* The json configuration is as follows:
* @code
{
"name": "drogon::plugin::PromExporter",
"dependencies": [],
"config": {
// The path of the metrics. the default value is "/metrics".
"path": "/metrics",
// The list of collectors.
"collectors":[
{
// The name of the collector.
"name": "http_requests_total",
// The help message of the collector.
"help": "The total number of http requests",
// The type of the collector. The default value is "counter".
// The other possible value is as following:
// "gauge", "histogram".
"type": "counter",
// The labels of the collector.
"labels": ["method", "status"]
}
]
}
}
@endcode
* */
class DROGON_EXPORT PromExporter
: public drogon::Plugin<PromExporter>,
public std::enable_shared_from_this<PromExporter>,
public drogon::monitoring::Registry
{
public:
PromExporter()
{
}

void initAndStart(const Json::Value &config) override;

void shutdown() override
{
}

~PromExporter() override
{
}

void registerCollector(
const std::shared_ptr<drogon::monitoring::CollectorBase> &collector)
override;

std::shared_ptr<drogon::monitoring::CollectorBase> getCollector(
const std::string &name) const noexcept(false);

template <typename T>
std::shared_ptr<drogon::monitoring::Collector<T>> getCollector(
const std::string &name) const
{
return std::dynamic_pointer_cast<drogon::monitoring::Collector<T>>(
getCollector(name));
}

private:
mutable std::mutex mutex_;
std::unordered_map<std::string,
std::shared_ptr<drogon::monitoring::CollectorBase>>
collectors_;
std::string path_{"/metrics"};
std::string exportMetrics();
};
} // namespace plugin
} // namespace drogon
18 changes: 18 additions & 0 deletions lib/inc/drogon/utils/monitoring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
*
* monitoring.h
* An Tao
*
* Copyright 2018, An Tao. All rights reserved.
* https://github.com/an-tao/drogon
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Drogon
*
*/
#pragma once
#include <drogon/utils/monitoring/Metric.h>
#include <drogon/utils/monitoring/Registry.h>
#include <drogon/utils/monitoring/Collector.h>
#include <drogon/utils/monitoring/Sample.h>
Loading

0 comments on commit cedeeb5

Please sign in to comment.