-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_loader_example.cpp
More file actions
122 lines (103 loc) · 3.78 KB
/
Copy pathplugin_loader_example.cpp
File metadata and controls
122 lines (103 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// BSD 3-Clause License
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
/**
* @file plugin_loader_example.cpp
* @brief Example demonstrating dynamic plugin loading
* @example plugin_example/plugin_loader_example.cpp
*
* This program shows how to:
* 1. Load a plugin from a shared library
* 2. Initialize and use the plugin
* 3. Collect metrics from the plugin
* 4. Unload the plugin
*
* Build:
* g++ -o plugin_loader_example plugin_loader_example.cpp \
* -I../../include -L../../build -lmonitoring_system -std=c++20
*
* Run:
* ./plugin_loader_example ./libexample_plugin.so
*/
#include "kcenon/monitoring/plugins/collector_registry.h"
#include "kcenon/monitoring/plugins/plugin_loader.h"
#include <iostream>
#include <thread>
#include <chrono>
using namespace kcenon::monitoring;
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <plugin_path>\n";
std::cerr << "Example: " << argv[0] << " ./libexample_plugin.so\n";
return 1;
}
std::string plugin_path = argv[1];
std::cout << "=== Dynamic Plugin Loading Example ===\n\n";
// Get the collector registry instance
auto& registry = collector_registry::instance();
// Load the plugin from shared library
std::cout << "Loading plugin from: " << plugin_path << "\n";
if (!registry.load_plugin(plugin_path)) {
std::cerr << "Failed to load plugin: "
<< registry.get_plugin_loader_error() << "\n";
return 1;
}
std::cout << "Plugin loaded successfully\n\n";
// Get the loaded plugin
auto* plugin = registry.get_plugin("example_plugin");
if (!plugin) {
std::cerr << "Plugin not found in registry\n";
return 1;
}
// Display plugin metadata
auto metadata = plugin->get_metadata();
std::cout << "Plugin Metadata:\n";
std::cout << " Name: " << plugin->name() << "\n";
std::cout << " Description: " << metadata.description << "\n";
std::cout << " Version: " << metadata.version << "\n";
std::cout << " Available: " << (plugin->is_available() ? "yes" : "no") << "\n\n";
// Initialize the plugin
std::cout << "Initializing plugin...\n";
config_map config; // Empty config for this example
if (!plugin->initialize(config)) {
std::cerr << "Failed to initialize plugin\n";
return 1;
}
std::cout << "Plugin initialized\n\n";
// Collect metrics from the plugin
std::cout << "Collecting metrics (5 iterations)...\n";
for (int i = 0; i < 5; ++i) {
std::cout << "\nIteration " << (i + 1) << ":\n";
auto metrics = plugin->collect();
for (const auto& metric : metrics) {
std::cout << " " << metric.name << ": " << metric.value
<< " " << metric.unit;
if (!metric.labels.empty()) {
std::cout << " [";
bool first = true;
for (const auto& [key, value] : metric.labels) {
if (!first) std::cout << ", ";
std::cout << key << "=" << value;
first = false;
}
std::cout << "]";
}
std::cout << "\n";
}
// Wait before next collection
if (i < 4) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
// Shutdown and unload the plugin
std::cout << "\nShutting down plugin...\n";
plugin->shutdown();
std::cout << "Unloading plugin...\n";
if (!registry.unload_plugin("example_plugin")) {
std::cerr << "Failed to unload plugin\n";
return 1;
}
std::cout << "Plugin unloaded successfully\n";
std::cout << "\n=== Example Complete ===\n";
return 0;
}