Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration from schema #38

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ find_package(absl REQUIRED)
find_package(Protobuf REQUIRED CONFIG)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
find_package(utilities)
find_package(confmodel)

set(OPMONLIB_DEPENDENCIES ${CETLIB} ${CETLIB_EXCEPT} ers::ers logging::logging nlohmann_json::nlohmann_json protobuf::libprotobuf absl::log_internal_check_op utilities::utilities )
set(OPMONLIB_DEPENDENCIES ${CETLIB} ${CETLIB_EXCEPT} ers::ers logging::logging nlohmann_json::nlohmann_json protobuf::libprotobuf absl::log_internal_check_op utilities::utilities confmodel::confmodel )

##############################################################################
# Schemas
Expand Down
10 changes: 7 additions & 3 deletions include/opmonlib/MonitorableObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

#include "opmonlib/Utils.hpp"
#include "opmonlib/OpMonFacility.hpp"
#include <google/protobuf/message.h>
#include <opmonlib/opmon/monitoring_tree.pb.h>

#include "confmodel/OpMonConf.hpp"

#include <google/protobuf/message.h>

#include <limits>
#include <type_traits>
#include <map>
Expand Down Expand Up @@ -45,7 +48,8 @@ namespace dunedaq {

namespace dunedaq::opmonlib {

using OpMonLevel = unsigned int;
using OpMonLevel = std::invoke_result<decltype(&dunedaq::confmodel::OpMonConf::get_level),
dunedaq::confmodel::OpMonConf>::type;

enum class SystemOpMonLevel : OpMonLevel {
kDisabled = std::numeric_limits<OpMonLevel>::min(),
Expand All @@ -60,7 +64,7 @@ namespace dunedaq::opmonlib {
};

template <class T>
auto to_level( T v ) {
constexpr auto to_level( T v ) {
return static_cast<typename std::underlying_type<T>::type>(v);
}

Expand Down
18 changes: 10 additions & 8 deletions include/opmonlib/OpMonFacility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <iostream>
#include <memory>
#include <string>
#include <optional>

#ifndef EXTERN_C_FUNC_DECLARE_START
// NOLINTNEXTLINE(build/define_used)
Expand All @@ -32,12 +33,12 @@
*/

// NOLINTNEXTLINE(build/define_used)
#define DEFINE_DUNE_OPMON_FACILITY(klass) \
EXTERN_C_FUNC_DECLARE_START \
std::shared_ptr<dunedaq::opmonlib::OpMonFacility> make(std::string facility) \
{ \
return std::shared_ptr<dunedaq::opmonlib::OpMonFacility>(new klass(facility)); \
} \
#define DEFINE_DUNE_OPMON_FACILITY(klass) \
EXTERN_C_FUNC_DECLARE_START \
std::shared_ptr<dunedaq::opmonlib::OpMonFacility> make(std::string facility, std::optional<dunedaq::opmon::OpMonId> o ) \
{ \
return std::shared_ptr<dunedaq::opmonlib::OpMonFacility>(new klass(facility, o)); \
} \
}

namespace dunedaq {
Expand All @@ -57,7 +58,7 @@ namespace dunedaq::opmonlib {
class OpMonFacility
{
public:
explicit OpMonFacility(std::string uri) : m_uri(uri) {;}
explicit OpMonFacility(std::string uri ) : m_uri(uri) {;}

virtual ~OpMonFacility() = default;
OpMonFacility(const OpMonFacility&) = delete; ///< OpMonFacility is not copy-constructible
Expand All @@ -79,7 +80,8 @@ class OpMonFacility
std::string m_uri;
};

std::shared_ptr<OpMonFacility> makeOpMonFacility(std::string const& facility) ;
using OptionalOrigin = std::optional<dunedaq::opmon::OpMonId>;
std::shared_ptr<OpMonFacility> makeOpMonFacility(std::string const& facility, OptionalOrigin = OptionalOrigin() ) ;

} // namespace dunedaq::opmonlib

Expand Down
21 changes: 17 additions & 4 deletions include/opmonlib/OpMonManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include <thread>

#include "opmonlib/MonitorableObject.hpp"
#include "opmonlib/Utils.hpp"

#include "confmodel/OpMonConf.hpp"
namespace dunedaq {

ERS_DECLARE_ISSUE( opmonlib,
Expand All @@ -31,6 +33,11 @@ namespace dunedaq {
FailedMonitoringThread,
"Monitoring thread failed to start",
ERS_EMPTY )

ERS_DECLARE_ISSUE( opmonlib,
MissingConfiguration,
"Confiration is not set",
ERS_EMPTY )
}


Expand All @@ -51,7 +58,7 @@ class OpMonManager : protected MonitorableObject
explicit OpMonManager(std::string session,
std::string name,
std::string opmon_facility_uri = "stdout") :
OpMonManager( session, name, makeOpMonFacility(opmon_facility_uri) ){;}
OpMonManager( session, name, makeOpMonFacility(opmon_facility_uri, make_origin(session, name)) ){;}

virtual ~OpMonManager() = default;

Expand All @@ -61,10 +68,15 @@ class OpMonManager : protected MonitorableObject
using MonitorableObject::set_opmon_level;

// data collecting loop
void start_monitoring(std::chrono::seconds);
void start_monitoring();
// The stop command is not necessary.
// The stop is invoked during the destruction of the thread or at the start of a new one
// The stop is invoked during the destruction of the thread
// the method requires a valid configuration because the time period is taken from there

void set_opmon_conf( const confmodel::OpMonConf* c ) {
m_cfg.store(c);
set_opmon_level( m_cfg.load()->get_level() );
}

protected:
using MonitorableObject::collect;
Expand All @@ -76,11 +88,12 @@ class OpMonManager : protected MonitorableObject
std::string name,
facility_ptr_t );

void run( std::stop_token, std::chrono::seconds ); // function used by the jthread
void run( std::stop_token ); // function used by the jthread

private:

std::jthread m_thread;
std::atomic<const confmodel::OpMonConf*> m_cfg{nullptr};

};

Expand Down
2 changes: 2 additions & 0 deletions include/opmonlib/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ namespace dunedaq::opmonlib {
void set_value( const google::protobuf::Reflection & ,
google::protobuf::Message & ,
const google::protobuf::FieldDescriptor* , T );

opmon::OpMonId make_origin( const std::string & session, const std::string & app ) ;

dunedaq::opmon::OpMonEntry to_entry(const google::protobuf::Message & m,
const CustomOrigin & co);
Expand Down
15 changes: 14 additions & 1 deletion plugins/fileOpMonFacility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace dunedaq::opmonlib {

fileOpMonFacility::fileOpMonFacility(std::string uri) :
fileOpMonFacility::fileOpMonFacility(std::string uri, OptionalOrigin o) :
JSonOpMonFacility(uri) {

std::string hook = "://";
Expand All @@ -27,6 +27,19 @@ namespace dunedaq::opmonlib {
} else {
fname = uri.substr(sep + hook.size());
}

if (o) {
auto slash_pos = fname.find_last_of('/');
auto dot_pos = slash_pos == std::string::npos ? fname.find_first_of('.') : fname.find_first_of('.', slash_pos);

auto origin = to_string( o.value() );
if (dot_pos == std::string::npos) {
fname += '.' + origin + ".json";
} else {
fname.insert(dot_pos, '.' + origin );
}

}

m_ofs.open(fname, std::ios::out | std::ios::app);
if (!m_ofs.is_open()) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/fileOpMonFacility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class fileOpMonFacility
: public JSonOpMonFacility {

public:
explicit fileOpMonFacility(std::string uri);
explicit fileOpMonFacility(std::string uri, OptionalOrigin);
~fileOpMonFacility();

void publish(opmon::OpMonEntry && e) const override;
Expand Down
2 changes: 1 addition & 1 deletion plugins/stdoutOpMonFacility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace dunedaq::opmonlib {
class stdoutOpMonFacility : public JSonOpMonFacility
{
public:
explicit stdoutOpMonFacility(std::string uri)
explicit stdoutOpMonFacility(std::string uri, OptionalOrigin )
: JSonOpMonFacility(uri) { ; }

void publish(opmon::OpMonEntry && e) const override {
Expand Down
52 changes: 0 additions & 52 deletions plugins/stdoutOpmonService.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/OpMonFacility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

using namespace dunedaq::opmonlib;

std::shared_ptr<OpMonFacility> dunedaq::opmonlib::makeOpMonFacility(std::string const& facility) {
std::shared_ptr<OpMonFacility> dunedaq::opmonlib::makeOpMonFacility(std::string const& facility, OptionalOrigin o) {

TLOG() << "FACILITY = " << facility;

Expand All @@ -30,7 +30,7 @@ std::shared_ptr<OpMonFacility> dunedaq::opmonlib::makeOpMonFacility(std::string
std::shared_ptr<OpMonFacility> os_ptr;

try {
os_ptr = bpf.makePlugin<std::shared_ptr<OpMonFacility>>(plugin_name, facility);
os_ptr = bpf.makePlugin<std::shared_ptr<OpMonFacility>>(plugin_name, facility, o);
} catch (const ers::Issue& iexpt) {
throw OpMonFacilityCreationFailed(ERS_HERE, plugin_name, iexpt);
} catch (const cet::exception& cexpt) {
Expand Down
17 changes: 9 additions & 8 deletions src/OpMonManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ OpMonManager::OpMonManager( std::string session,

}

void OpMonManager::start_monitoring(std::chrono::seconds interval) {
void OpMonManager::start_monitoring() {

TLOG() << "Starting a new monitoring thread with interval " << interval.count() << " seconds, at level " << get_opmon_level();
if ( ! m_cfg )
throw MissingConfiguration(ERS_HERE);

auto running_function = std::bind( & OpMonManager::run, this,
std::placeholders::_1, std::placeholders::_2);
m_thread = std::jthread( running_function, interval );
TLOG() << "Starting a new monitoring thread with interval " << m_cfg.load()->get_interval().count() << " seconds, at level " << get_opmon_level();

auto running_function = std::bind( & OpMonManager::run, this, std::placeholders::_1);
m_thread = std::jthread( running_function );
auto handle = m_thread.native_handle();
auto thread_name = "opmon";
auto rc = pthread_setname_np(handle, thread_name);
Expand All @@ -37,8 +39,7 @@ void OpMonManager::start_monitoring(std::chrono::seconds interval) {
}
}

void OpMonManager::run(std::stop_token stoken,
std::chrono::seconds interval) {
void OpMonManager::run(std::stop_token stoken ) {

auto sleep_interval = std::chrono::milliseconds(100);

Expand All @@ -49,7 +50,7 @@ void OpMonManager::run(std::stop_token stoken,
std::this_thread::sleep_for(sleep_interval);
auto time_span = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - last_collection_time);

if ( time_span >= interval ) {
if ( time_span >= m_cfg.load()->get_interval() ) {
last_collection_time = std::chrono::steady_clock::now();
publish( collect() );
// there is no catch here because collect is supposed to catch all possible exceptions
Expand Down
9 changes: 9 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

#include "opmonlib/Utils.hpp"


dunedaq::opmon::OpMonId dunedaq::opmonlib::make_origin( const std::string & session, const std::string & app ) {
opmon::OpMonId origin;
origin.set_session( session );
origin.set_application( app );
return origin;
}


dunedaq::opmon::OpMonEntry dunedaq::opmonlib::to_entry(const google::protobuf::Message & m,
const CustomOrigin & co) {

Expand Down
2 changes: 1 addition & 1 deletion unittest/opmon_facility_test.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BOOST_AUTO_TEST_CASE(File_facility) {
BOOST_CHECK_THROW( auto service = makeOpMonFacility("file:///impossible_file.txt"),
OpMonFacilityCreationFailed);

auto service = makeOpMonFacility("file://./test_file.txt");
auto service = makeOpMonFacility("file://./test_file", make_origin("test", "app") );

auto pub_func = [&](int i){
dunedaq::opmon::OpMonId id;
Expand Down
Loading