Skip to content
Open
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion metrics-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ path = "src/main.rs"

[dependencies]
cln-plugin = { version = "0.7" }
cln-rpc = { version = "0.6" }
cln-rpc = { version = "0.7" }
prometheus = { version = "0.14" }
axum = { version = "0.8" }
tokio = { version = "1", features = ["full"] }
Expand Down
45 changes: 9 additions & 36 deletions metrics-plugin/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
use anyhow::Result;
use cln_plugin::Plugin;
use cln_rpc::notifications::ChannelStateChangedNotification;
use cln_rpc::notifications::{ChannelStateChangedNotification, ForwardEventNotification};
use serde::Deserialize;
use serde_json::{Value, json};

use crate::PluginState;

#[derive(Deserialize)]
struct ForwardEventNotification {
forward_event: ForwardEvent,
}

#[derive(Deserialize)]
struct ForwardEvent {
status: String,
struct ForwardEventNotificationWrapper {
forward_event: ForwardEventNotification,
}

#[derive(Deserialize)]
Expand All @@ -22,49 +17,27 @@ struct ChannelStateChangedWrapper {
}

pub async fn on_channel_opened(plugin: Plugin<PluginState>, _v: Value) -> Result<()> {
plugin.state().counters.channel_opened_total.inc();
plugin.state().counters.on_channel_opened();
Ok(())
}

pub async fn on_forward_event(plugin: Plugin<PluginState>, v: Value) -> Result<()> {
let n: ForwardEventNotification = serde_json::from_value(v)?;
plugin
.state()
.counters
.forward_events_total
.with_label_values(&[&n.forward_event.status])
.inc();
let n: ForwardEventNotificationWrapper = serde_json::from_value(v)?;
plugin.state().counters.on_forward_event(&n.forward_event);
Ok(())
}

// CLN hook - must return {"result": "continue"} or the HTLC will be rejected by the node
pub async fn hook_htlc_accepted(plugin: Plugin<PluginState>, _v: Value) -> Result<Value> {
plugin.state().counters.htlc_accepted_total.inc();
plugin.state().counters.on_htlc_accepted();
// CLN hook - must return {"result": "continue"} or the HTLC will be rejected by the node
Ok(json!({ "result": "continue" }))
}

pub async fn on_channel_state_changed(plugin: Plugin<PluginState>, v: Value) -> Result<()> {
let n: ChannelStateChangedWrapper = serde_json::from_value(v)?;
let c = n.channel_state_changed;

let old = c
.old_state
.map(|s| s.to_string())
.unwrap_or_else(|| "unknown".to_string());
let new = c.new_state.to_string();

plugin
.state()
.counters
.channel_state_changes_total
.with_label_values(&[&old, &new])
.inc();

tracing::info!(
peer = %c.peer_id,
old_state = old,
new_state = new,
"channel_state_changed"
);
.on_channel_state_changed(&n.channel_state_changed);
Ok(())
}
6 changes: 1 addition & 5 deletions metrics-plugin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ async fn main() -> Result<()> {
.subscribe("forward_event", events::on_forward_event)
.subscribe("channel_state_changed", events::on_channel_state_changed)
.hook("htlc_accepted", events::hook_htlc_accepted)
.rpcmethod(
"metrics-status",
"Get metrics plugin status",
rpc_status,
)
.rpcmethod("metrics-status", "Get metrics plugin status", rpc_status)
.dynamic()
.configure()
.await?;
Expand Down
34 changes: 34 additions & 0 deletions metrics-plugin/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use axum::{
response::{IntoResponse, Response},
routing::get,
};
use cln_rpc::notifications::{ChannelStateChangedNotification, ForwardEventNotification};
use prometheus::{
Encoder, Gauge, GaugeVec, IntCounter, IntCounterVec, Opts, TextEncoder,
core::{Collector, Desc},
Expand Down Expand Up @@ -85,6 +86,39 @@ impl EventCounters {
)?)?,
})
}

pub fn on_forward_event(&self, event: &ForwardEventNotification) {
self.forward_events_total
.with_label_values(&[event.status.to_string()])
.inc();
}

pub fn on_channel_opened(&self) {
self.channel_opened_total.inc()
}

pub fn on_channel_state_changed(&self, event: &ChannelStateChangedNotification) {
let old = event
.old_state
.map(|s| s.to_string())
.unwrap_or_else(|| "unknown".to_string());
let new = event.new_state.to_string();

self.channel_state_changes_total
.with_label_values(&[&old, &new])
.inc();

tracing::info!(
peer = %event.peer_id,
old_state = old,
new_state = new,
"channel_state_changed"
);
}

pub fn on_htlc_accepted(&self) {
self.htlc_accepted_total.inc();
}
}

/// Registers the build_info gauge with the global registry and pins it to 1.0.
Expand Down