Skip to content

Commit

Permalink
feat: add a field in config file to specify listen socket address for…
Browse files Browse the repository at this point in the history
… prometheus (#592)
  • Loading branch information
henil authored Mar 21, 2023
1 parent 819184d commit c3f82f5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
2 changes: 2 additions & 0 deletions rumqttd/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- `PrometheusSetting` now takes `listen` to specify listener address instead of default `127.0.0.1`. Do not use `listen` and `port` together.

### Changed

### Deprecated
- `PrometheusSetting`'s `port` will be removed in favour of `listen`.

### Removed

Expand Down
4 changes: 2 additions & 2 deletions rumqttd/rumqttd.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ next_connection_delay_ms = 1
max_inflight_size = 1024

[prometheus]
port = 9042
listen = "127.0.0.1:9042"
interval = 1

[ws]
Expand All @@ -91,4 +91,4 @@ listen = "0.0.0.0:3030"
# [metrics.alerts]
# push_interval = 1
# [metrics.meters]
# push_interval = 1
# push_interval = 1
9 changes: 4 additions & 5 deletions rumqttd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ pub struct Config {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PrometheusSetting {
port: u16,
#[deprecated(note = "Use listen instead")]
port: Option<u16>,
listen: Option<SocketAddr>,
// How frequently to update metrics
interval: u64,
}
Expand Down Expand Up @@ -144,8 +146,7 @@ impl ConsoleSettings {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Default)]
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub enum Transport {
#[serde(rename = "tcp")]
#[default]
Expand All @@ -157,8 +158,6 @@ pub enum Transport {
},
}



#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ClientAuth {
certs: PathBuf,
Expand Down
1 change: 0 additions & 1 deletion rumqttd/src/link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ pub mod remote;
#[cfg(feature = "websockets")]
pub mod shadow;
pub mod timer;

20 changes: 15 additions & 5 deletions rumqttd/src/server/broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::protocol::Protocol;
use crate::server::tls::{self, TLSAcceptor};
use crate::{meters, ConnectionSettings, Meter};
use flume::{RecvError, SendError, Sender};
use std::net::SocketAddr;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;
use tracing::{error, field, info, Instrument};
#[cfg(feature = "websockets")]
Expand Down Expand Up @@ -233,14 +233,24 @@ impl Broker {
}

if let Some(prometheus_setting) = &self.config.prometheus {
let port = prometheus_setting.port;
let timeout = prometheus_setting.interval;
// If port is specified use it instead of listen.
// NOTE: This means listen is ignored when `port` is specified.
// `port` will be removed in future release in favour of `listen`
let addr = {
#[allow(deprecated)]
match prometheus_setting.port {
Some(port) => SocketAddr::new("127.0.0.1".parse().unwrap(), port),
None => prometheus_setting.listen.unwrap_or(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
9042,
)),
}
};
let metrics_thread = thread::Builder::new().name("Metrics".to_owned());
let meter_link = self.meters().unwrap();

metrics_thread.spawn(move || {
let builder = PrometheusBuilder::new()
.with_http_listener(SocketAddr::new("127.0.0.1".parse().unwrap(), port));
let builder = PrometheusBuilder::new().with_http_listener(addr);
builder.install().unwrap();

let total_publishes = register_gauge!("metrics.router.total_publishes");
Expand Down

0 comments on commit c3f82f5

Please sign in to comment.