Skip to content

Commit

Permalink
Refactor plugins folder (#1)
Browse files Browse the repository at this point in the history
Refactored Prometheus plugin files into plugins/prometheus folder, use BMQ_TARGET_PROMETHEUS_NEEDED var, fixed test.

---------

Signed-off-by: Aleksandr Ivanov <[email protected]>
  • Loading branch information
alexander-e1off authored Oct 27, 2023
1 parent 10f1e68 commit b24cfe4
Show file tree
Hide file tree
Showing 30 changed files with 187 additions and 186 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ if (NOT DEFINED INSTALL_TARGETS)
set(BMQ_TARGET_MQB_NEEDED YES)
set(BMQ_TARGET_E_BMQBRKR_NEEDED YES)
set(BMQ_TARGET_TUTORIAL_NEEDED YES)
set(BMQ_TARGET_PLUGINS_NEEDED NO)
set(BMQ_TARGET_PROMETHEUS_NEEDED NO)
else()
bbproject_check_install_target("bmqbrkr" installBMQBRKR)
bbproject_check_install_target("BMQBRKR_NIGHTLY" installNightly)
Expand All @@ -147,7 +147,7 @@ else()
set(BMQ_TARGET_BMQ_NEEDED NO)
set(BMQ_TARGET_MQB_NEEDED NO)
set(BMQ_TARGET_TUTORIAL_NEEDED NO)
set(BMQ_TARGET_PLUGINS_NEEDED NO)
set(BMQ_TARGET_PROMETHEUS_NEEDED NO)

bbproject_check_install_target("mwc" installMWC)
bbproject_check_install_target("bmq" installBMQ)
Expand Down Expand Up @@ -197,7 +197,7 @@ else()
set(BMQ_TARGET_MQB_NEEDED YES)
set(BMQ_TARGET_E_BMQBRKR_NEEDED YES)
set(BMQ_TARGET_TUTORIAL_NEEDED YES)
set(BMQ_TARGET_PLUGINS_NEEDED YES)
set(BMQ_TARGET_PROMETHEUS_NEEDED YES)
endif()
endif()

Expand Down
11 changes: 1 addition & 10 deletions src/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
# plugins
# ------------

if (NOT BMQ_TARGET_PLUGINS_NEEDED)
return()
endif()

find_package( PkgConfig REQUIRED )

bmq_add_plugin( plugins )
add_subdirectory( "prometheus" )
5 changes: 0 additions & 5 deletions src/plugins/package/plugins.mem

This file was deleted.

10 changes: 10 additions & 0 deletions src/plugins/prometheus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Prometheus plugin
# ------------

if (NOT BMQ_TARGET_PROMETHEUS_NEEDED )
return()
endif()

find_package( PkgConfig REQUIRED )

bmq_add_plugin( prometheus )
File renamed without changes.
5 changes: 5 additions & 0 deletions src/plugins/prometheus/package/prometheus.mem
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
prometheus_entry
prometheus_pluginlibrary
prometheus_prometheusstatconsumer
prometheus_version
prometheus_versiontag
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// plugins_entry.cpp -*-C++-*-
// prometheus_entry.cpp -*-C++-*-

// PLUGINS
#include <plugins_pluginlibrary.h>
#include <plugins_version.h>
// PROMETHEUS
#include <prometheus_pluginlibrary.h>
#include <prometheus_version.h>

// MQB
#include <mqbplug_pluginlibrary.h>
Expand All @@ -40,12 +40,13 @@ void instantiatePluginLibrary(
bslma::ManagedPtr<mqbplug::PluginLibrary>* library,
bslma::Allocator* allocator)
{
BALL_LOG_SET_CATEGORY("PLUGINS.ENTRY");
BALL_LOG_SET_CATEGORY("PROMETHEUS.ENTRY");

BALL_LOG_INFO << "Instantiating 'libplugins.so' plugin library "
BALL_LOG_INFO << "Instantiating 'libprometheus.so' plugin library "
"(version: "
<< plugins::Version::version() << ")";
<< prometheus::Version::version() << ")";

*library = bslma::ManagedPtrUtil::allocateManaged<plugins::PluginLibrary>(
allocator);
*library =
bslma::ManagedPtrUtil::allocateManaged<prometheus::PluginLibrary>(
allocator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// plugins_pluginlibrary.cpp -*-C++-*-
#include <plugins_pluginlibrary.h>
// prometheus_pluginlibrary.cpp -*-C++-*-
#include <prometheus_pluginlibrary.h>

// PLUGINS
#include <plugins_prometheusstatconsumer.h>
#include <plugins_version.h>

//// BUL
// #include <bulscm_version.h>

//// SIM
// #include <simscm_version.h>
// PROMETHEUS
#include <prometheus_prometheusstatconsumer.h>
#include <prometheus_version.h>

// BDE
#include <bsl_sstream.h>
#include <bslma_default.h>

namespace BloombergLP {
namespace plugins {
namespace prometheus {

// -------------------
// class PluginLibrary
Expand Down Expand Up @@ -67,6 +61,7 @@ int PluginLibrary::activate()

void PluginLibrary::deactivate()
{
// NOTHING
}

const bsl::vector<mqbplug::PluginInfo>& PluginLibrary::plugins() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// plugins_pluginlibrary.h -*-C++-*-
#ifndef INCLUDED_PLUGINS_PLUGINLIBRARY
#define INCLUDED_PLUGINS_PLUGINLIBRARY
// prometheus_pluginlibrary.h -*-C++-*-
#ifndef INCLUDED_PROMETHEUS_PLUGINLIBRARY
#define INCLUDED_PROMETHEUS_PLUGINLIBRARY

//@PURPOSE: Provide library of enterprise broker plugins for Bloomberg.
//@PURPOSE: Provide library of Prometheus plugin for broker.
//
//@CLASSES:
// plugins::PluginLibrary: Library of Bloomberg enterprise broker plugins.
// prometheus::PluginLibrary: Library of Prometheus plugin.
//
//@DESCRIPTION: This component provides the definition for the 'PluginLibrary'
// class, which represents and publishes various plugins for interfacing
// between the BMQ broker (i.e., 'bmqbrkr.tsk') and various enterprise services
// available only within Bloomberg.
// class, which represents and publishes Prometheus plugin for interfacing
// with the BMQ broker (i.e., 'bmqbrkr.tsk').

// MQB
#include <mqbplug_plugininfo.h>
Expand All @@ -39,7 +38,7 @@
#include <bsls_keyword.h>

namespace BloombergLP {
namespace plugins {
namespace prometheus {
// ===================
// class PluginLibrary
// ===================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// plugins_prometheusstatconsumer.cpp -*-C++-*-
#include <plugins_prometheusstatconsumer.h>
// prometheus_prometheusstatconsumer.cpp -*-C++-*-
#include <prometheus_prometheusstatconsumer.h>

// PLUGINS
#include <plugins_version.h>
// PROMETHEUS
#include <prometheus_version.h>

// MQB
#include <mqbstat_brokerstats.h>
Expand Down Expand Up @@ -46,7 +46,7 @@
#include "prometheus/labels.h"

namespace BloombergLP {
namespace plugins {
namespace prometheus {

namespace {

Expand All @@ -57,7 +57,7 @@ const char* k_THREADNAME = "bmqPrometheusPush";
class Tagger {
private:
// DATA
prometheus::Labels labels;
::prometheus::Labels labels;

public:
// MANIPULATORS
Expand Down Expand Up @@ -110,14 +110,14 @@ class Tagger {
}

// ACCESSORS
prometheus::Labels& getLabels() { return labels; }
::prometheus::Labels& getLabels() { return labels; }
};

bsl::unique_ptr<PrometheusStatExporter>
makeExporter(const bsl::string& mode,
const bsl::string& host,
const bsl::size_t port,
std::shared_ptr<prometheus::Registry>& registry);
makeExporter(const bsl::string& mode,
const bsl::string& host,
const bsl::size_t port,
std::shared_ptr< ::prometheus::Registry>& registry);

} // close unnamed namespace

Expand Down Expand Up @@ -148,7 +148,7 @@ PrometheusStatConsumer::PrometheusStatConsumer(
, d_snapshotId(0)
, d_actionCounter(0)
, d_isStarted(false)
, d_prometheusRegistry_p(std::make_shared<prometheus::Registry>())
, d_prometheusRegistry_p(std::make_shared< ::prometheus::Registry>())
{
// Initialize stat contexts
d_systemStatContext_p = getStatContext("system");
Expand Down Expand Up @@ -274,7 +274,7 @@ void PrometheusStatConsumer::captureQueueStats()
// a time series containing all the tags that can be leveraged
// in Grafana.

auto& heartbeatGauge = prometheus::BuildGauge()
auto& heartbeatGauge = ::prometheus::BuildGauge()
.Name("queue_heartbeat")
.Register(*d_prometheusRegistry_p);
heartbeatGauge.Add(labels).Set(0);
Expand Down Expand Up @@ -374,8 +374,8 @@ void PrometheusStatConsumer::captureSystemStats()

#undef COPY_METRIC

prometheus::Labels labels{{"DataType", "host-data"}};
bslstl::StringRef instanceName =
::prometheus::Labels labels{{"DataType", "host-data"}};
bslstl::StringRef instanceName =
mqbcfg::BrokerConfig::get().brokerInstanceName();
if (!instanceName.empty()) {
labels.emplace("instanceName", instanceName);
Expand All @@ -385,7 +385,7 @@ void PrometheusStatConsumer::captureSystemStats()
datapoints.begin();
it != datapoints.end();
++it) {
auto& gauge = prometheus::BuildGauge().Name(it->first).Register(
auto& gauge = ::prometheus::BuildGauge().Name(it->first).Register(
*d_prometheusRegistry_p);
gauge.Add(labels).Set(it->second);
}
Expand Down Expand Up @@ -422,8 +422,8 @@ void PrometheusStatConsumer::captureNetworkStats()

#undef RETRIEVE_METRIC

prometheus::Labels labels{{"DataType", "host-data"}};
bslstl::StringRef instanceName =
::prometheus::Labels labels{{"DataType", "host-data"}};
bslstl::StringRef instanceName =
mqbcfg::BrokerConfig::get().brokerInstanceName();
if (!instanceName.empty()) {
labels.emplace("instanceName", instanceName);
Expand All @@ -433,7 +433,7 @@ void PrometheusStatConsumer::captureNetworkStats()
datapoints.begin();
it != datapoints.end();
++it) {
auto& counter = prometheus::BuildCounter().Name(it->first).Register(
auto& counter = ::prometheus::BuildCounter().Name(it->first).Register(
*d_prometheusRegistry_p);
counter.Add(labels).Increment(it->second);
}
Expand Down Expand Up @@ -673,20 +673,20 @@ void PrometheusStatConsumer::captureDomainStats(const LeaderSet& leaders)
}
}

void PrometheusStatConsumer::updateMetric(const DatapointDef* def_p,
const prometheus::Labels& labels,
const bsls::Types::Int64 value)
void PrometheusStatConsumer::updateMetric(const DatapointDef* def_p,
const ::prometheus::Labels& labels,
const bsls::Types::Int64 value)
{
if (value != 0) {
// To save metrics, only report non-null values
if (def_p->d_isCounter) {
auto& counter = prometheus::BuildCounter()
auto& counter = ::prometheus::BuildCounter()
.Name(def_p->d_name)
.Register(*d_prometheusRegistry_p);
counter.Add(labels).Increment(static_cast<double>(value));
}
else {
auto& gauge = prometheus::BuildGauge()
auto& gauge = ::prometheus::BuildGauge()
.Name(def_p->d_name)
.Register(*d_prometheusRegistry_p);
gauge.Add(labels).Set(static_cast<double>(value));
Expand Down Expand Up @@ -773,15 +773,15 @@ bslma::ManagedPtr<StatConsumer> PrometheusStatConsumerPluginFactory::create(
// --------------------------------

class PrometheusPullStatExporter : public PrometheusStatExporter {
std::weak_ptr<prometheus::Registry> d_registry_p;
bsl::unique_ptr<prometheus::Exposer> d_exposer_p;
bsl::string d_exposerEndpoint;
std::weak_ptr< ::prometheus::Registry> d_registry_p;
bsl::unique_ptr< ::prometheus::Exposer> d_exposer_p;
bsl::string d_exposerEndpoint;

public:
PrometheusPullStatExporter(
const bsl::string& host,
const bsl::size_t port,
const std::shared_ptr<prometheus::Registry>& registry)
const bsl::string& host,
const bsl::size_t port,
const std::shared_ptr< ::prometheus::Registry>& registry)
: d_registry_p(registry)
{
bsl::ostringstream endpoint;
Expand All @@ -791,7 +791,8 @@ class PrometheusPullStatExporter : public PrometheusStatExporter {

void start() override
{
d_exposer_p = bsl::make_unique<prometheus::Exposer>(d_exposerEndpoint);
d_exposer_p = bsl::make_unique< ::prometheus::Exposer>(
d_exposerEndpoint);
d_exposer_p->RegisterCollectable(d_registry_p);
}

Expand All @@ -803,7 +804,7 @@ class PrometheusPullStatExporter : public PrometheusStatExporter {
// --------------------------------

class PrometheusPushStatExporter : public PrometheusStatExporter {
bsl::unique_ptr<prometheus::Gateway> d_prometheusGateway_p;
bsl::unique_ptr< ::prometheus::Gateway> d_prometheusGateway_p;
/// Handle of the Prometheus publishing thread
bslmt::ThreadUtil::Handle d_prometheusPushThreadHandle;
bslmt::Mutex d_prometheusThreadMutex;
Expand Down Expand Up @@ -849,15 +850,15 @@ class PrometheusPushStatExporter : public PrometheusStatExporter {

public:
PrometheusPushStatExporter(
const bsl::string& host,
const bsl::size_t& port,
const std::shared_ptr<prometheus::Registry>& registry)
const bsl::string& host,
const bsl::size_t& port,
const std::shared_ptr< ::prometheus::Registry>& registry)
: d_threadStop(false)
{
// create a push gateway
const auto label = prometheus::Gateway::GetInstanceLabel(
const auto label = ::prometheus::Gateway::GetInstanceLabel(
mqbcfg::BrokerConfig::get().hostName());
d_prometheusGateway_p = bsl::make_unique<prometheus::Gateway>(
d_prometheusGateway_p = bsl::make_unique< ::prometheus::Gateway>(
host,
bsl::to_string(port),
"bmq",
Expand Down Expand Up @@ -892,10 +893,10 @@ class PrometheusPushStatExporter : public PrometheusStatExporter {
namespace {

bsl::unique_ptr<PrometheusStatExporter>
makeExporter(const bsl::string& mode,
const bsl::string& host,
const bsl::size_t port,
std::shared_ptr<prometheus::Registry>& registry)
makeExporter(const bsl::string& mode,
const bsl::string& host,
const bsl::size_t port,
std::shared_ptr< ::prometheus::Registry>& registry)
{
bsl::unique_ptr<PrometheusStatExporter> result;
if (mode == "pull") {
Expand Down
Loading

0 comments on commit b24cfe4

Please sign in to comment.