This project represents a Rust binding of New Relic Plugin API. This crate can be used to build plugin agents.
```rust
// Plugin agent setup
let mut agent = Agent::new("
// Poll cycle function. This function is excuted every [pollcycle] seconds. fn cycle(agent: &mut Agent){ agent.reportmetric( "com.testplugin.pluginname".into(), "Component/Request/Rate/host1[requests/second]".into(), (1000) as f64, None ); agent.reportmetric( "com.testplugin.plugin_name".into(), "Component/Request/Rate/host2[requests/second]".into(), (1000) as f64, None ); } // Start the agent agent.run(cycle); ```
An agent has the option to have a state to be able to preserve metric readings through cycles. States are passed to Agent instances via the set_state
function. States are generic types, which means you can create a custom struct like in this example:
```rust struct State{ prevfilesize: i32 }
// Plugin agent setup
let mut agent = Agent::new("
// Poll cycle function. This function is excuted every [pollcycle] seconds.
fn cycle(agent: &mut Agent){
let prevsize = agent.getstate().asref().unwrap().prevfilesize.clone();
let newsize = 2000;
if prevsize != 0{
agent.reportmetric(
"com.testplugin.pluginname".into(),
"Component/File/Size/host1[bytes]".into(),
newsize - prevsize as f64, None
);
}
agent.setstate(State{prevfilesize: new_size});
}
// Start the agent
agent.run(cycle);
```
NewRelic plugin reads configuration from a config.yml
file located in the current working directory. If no config.yml
file is present, default values are used. Possible config keys and values:
| Config key | Description | Default | | ------------- |:-------------:| ----- | | endpoint | NewRelic custom plugin API endpoint| https://platform-api.newrelic.com/platform/v1/metrics | | log4rsfile | log4rs config file | log4rs.yml | | delivercycle | metric reporting frequency | 60 (seconds) | | poll_cycle | poll cycle frequency | 20 (seconds) |
NewRelic plugin uses log4rs. All events logged from within the agent are sent to a logger named "agent". You can add more loggers in log4rs config file to log messages from your plugin's code.
Example log4rs.yml
file:
```yml refresh_rate: 60 seconds
appenders: agent: kind: file path: "log/agent.log" encoder: pattern: "{d} - {l} - {f} - {m}{n}" plugin: kind: file path: "log/plugin.log" encoder: pattern: "{d} - {l} - {f} - {m}{n}"
loggers: agent: level: error appenders: - agent plugin: level: info appenders: - plugin ```