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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Static and dynamic plugins.
//!
//! Plugins are an essential part of Alumet, as they provide the
//! [`Source`](crate::pipeline::Source)s, [`Transform`](crate::pipeline::Transform)s and [`Output`](crate::pipeline::Output)s.
//!
//! # Plugin lifecycle
//! Every plugin follow these steps:
//!
//! 1. **Metadata loading**: the Alumet application loads basic information about the plugin.
//! For static plugins, this is done at compile time (cargo takes care of the dependencies).
//!
//! 2. **Initialization**: the plugin is initialized, a value that implements the [`Plugin`] trait is created.
//! During the initialization phase, the plugin can read its configuration.
//!
//! 3. **Start-up**: the plugin is started via its [`start`](Plugin::start) method.
//! During the start-up phase, the plugin can create metrics, register new pipeline elements
//! (sources, transforms, outputs), and more.
//!
//! 4. **Operation**: the measurement pipeline has started, and the elements registered by the plugin
//! are in use. Alumet takes care of the lifetimes and of the triggering of those elements.
//!
//! 5. **Post start-up**: [`post_pipeline_start`](Plugin::post_pipeline_start) is called with
//! arguments that allow to interact with the running measurement pipeline (which is not possible
//! in `start`, since the pipeline is not fully constructed nor started at this point).
//!
//! 5. **Stop**: when the pipeline is stopped, the elements registered by the plugin are stopped and dropped.
//! Then, [`stop`](Plugin::stop) is called.
//!
//! 6. **Drop**: like any Rust value, the plugin is dropped when it goes out of scope.
//! To customize the destructor of your static plugin, implement the [`Drop`] trait on your plugin structure.
//! For a dynamic plugin, modify the `plugin_drop` function.
//!
//! # Static plugins
//!
//! A static plugin is a plugin that is included in the Alumet measurement application
//! at compile-time. The measurement tool and the static plugin are compiled together,
//! in a single executable binary.
//!
//! To create a static plugin in Rust, make a new library crate that depends on:
//! - the core of Alumet (the `alumet` crate).
//! - the `anyhow` crate
//!
//! To do this, here is an example (bash commands):
//! ```sh
//! cargo init --lib my-plugin
//! cd my-plugin
//! cargo add alumet anyhow
//! ```
//!
//! In your library, define a structure for your plugin, and implement the [`rust::AlumetPlugin`] trait for it.
//! ```no_run
//! use alumet::plugin::{rust::AlumetPlugin, AlumetPluginStart, ConfigTable};
//!
//! struct MyPlugin {}
//!
//! impl AlumetPlugin for MyPlugin {
//! fn name() -> &'static str {
//! "my-plugin"
//! }
//!
//! fn version() -> &'static str {
//! "0.1.0"
//! }
//!
//! fn default_config() -> anyhow::Result<Option<ConfigTable>> {
//! Ok(None)
//! }
//!
//! fn init(config: ConfigTable) -> anyhow::Result<Box<Self>> {
//! // You can read the config and store some settings in your structure.
//! Ok(Box::new(MyPlugin {}))
//! }
//!
//! fn start(&mut self, alumet: &mut AlumetPluginStart) -> anyhow::Result<()> {
//! println!("My first plugin is starting!");
//! Ok(())
//! }
//!
//! fn stop(&mut self) -> anyhow::Result<()> {
//! println!("My first plugin is stopping!");
//! Ok(())
//! }
//! }
//! ```
//!
//! Finally, modify the measurement application in the following ways:
//! 1. Add a dependency to your plugin crate (for example with `cargo add my-plugin --path=path/to/my-plugin`).
//! 2. Modify your `main` to initialize and load the plugin. See [`agent::Builder`](crate::agent::Builder).
//!
//! # Dynamic plugins
//!
//! WIP
//!
use Debug;
use AlumetPlugin;
pub
pub use ;
/// Plugin metadata, and a function that allows to initialize the plugin.
/// A configuration table for plugins.
///
/// `ConfigTable` is currently a wrapper around [`toml::Table`].
/// However, you probably don't need to add a dependency on the `toml` crate,
/// since Alumet provides functions to easily serialize and deserialize configurations
/// with `serde`.
///
/// # Example
///
/// ```
/// use serde::{Serialize, Deserialize};
/// use alumet::plugin::ConfigTable;
/// use alumet::plugin::rust::{serialize_config, deserialize_config};
///
/// #[derive(Serialize, Deserialize)]
/// struct MyConfig {
/// field: String
/// }
///
/// // serialize struct to config
/// let my_struct = MyConfig { field: String::from("value") };
/// let serialized: ConfigTable = serialize_config(my_struct).expect("serialization failed");
///
/// // deserialize config to struct
/// let my_table: ConfigTable = serialized;
/// let deserialized: MyConfig = deserialize_config(my_table).expect("deserialization failed");
/// ```
;
/// Trait for plugins.
///
/// # Note for plugin authors
///
/// You should _not_ implement this trait manually.
///
/// If you are writing a plugin in Rust, implement [`AlumetPlugin`] instead.
/// If you are writing a plugin in C, you need to define the right symbols in your shared library.