Skip to content

Commit

Permalink
feat: add PING_EXPORTER_RESOLVER option
Browse files Browse the repository at this point in the history
  • Loading branch information
knsd committed Aug 12, 2019
1 parent b619af0 commit 8d37beb
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log
All notable changes to this project will be documented in this file.

## 0.3.0 - 2019-08-12

- Add `PING_EXPORTER_RESOLVER` option


## 0.2.0 - 2018-11-12

- Add `ping_resolve_error` metric
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ping-exporter"
version = "0.2.0"
version = "0.3.0"
authors = ["Fedor Gogolev <knsd@knsd.net>"]
publish = false

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Visiting [http://localhost:9346/ping?target=google.com](http://localhost:9346/pi
| -------------------------------------- | ------------- |
| PING_EXPORTER_LISTEN | [::]:9346 |
| PING_EXPORTER_DEFAULT_PROTOCOL | v4 |
| PING_EXPORTER_RESOLVER | system |
| PING_EXPORTER_DEFAULT_COUNT | 5 |
| PING_EXPORTER_MAX_COUNT | 30 |
| PING_EXPORTER_DEFAULT_PING_TIMEOUT | 1000 |
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn run() -> i32 {
let (stop_sender, stop_receiver) = oneshot::channel();

runtime.spawn(futures::lazy(move || {
let server_future = pinger::Pinger::new()
let server_future = pinger::Pinger::new(settings.clone())
.map_err(|_| {
error!("Unable to create pinger, please check capabilities");
}).and_then(move |pinger| http::server(settings, pinger));
Expand Down
5 changes: 3 additions & 2 deletions src/pinger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::timer::Deadline;
use tokio_ping::{Error as PingError, Pinger as LowLevelPinger};

use resolver::{Error as ResolveError, Resolver};
use settings::Settings;
use utils::{NameOrIpAddr, Protocol};

#[derive(Debug, Fail)]
Expand Down Expand Up @@ -52,8 +53,8 @@ struct PingerInner {
}

impl Pinger {
pub fn new() -> impl Future<Item = Self, Error = Error> {
let resolver_future = Resolver::new().map_err(From::from);
pub fn new(settings: Settings) -> impl Future<Item = Self, Error = Error> {
let resolver_future = Resolver::new(settings).map_err(From::from);
let pinger_future = LowLevelPinger::new().map_err(From::from);
resolver_future
.join(pinger_future)
Expand Down
19 changes: 16 additions & 3 deletions src/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

use futures::{future, Future};
use rand::{thread_rng, Rng};
use trust_dns_resolver::config::{self, ResolverConfig, NameServerConfig, ResolverOpts};
use trust_dns_resolver::error::{ResolveError, ResolveErrorKind};
use trust_dns_resolver::ResolverFuture;

use settings::Settings;
use utils::{boxed, NameOrIpAddr, Protocol};

pub struct Resolver {
Expand All @@ -30,8 +32,19 @@ impl From<ResolveError> for Error {
}

impl Resolver {
pub fn new() -> impl Future<Item = Self, Error = Error> {
let future = future::result(ResolverFuture::from_system_conf()).flatten();
pub fn new(settings: Settings) -> impl Future<Item = Self, Error = Error> {
let future = future::result(match settings.resolver {
Some(resolver_addr) => {
let mut config = ResolverConfig::new();
config.add_name_server(NameServerConfig {
socket_addr: SocketAddr::new(resolver_addr, 53),
protocol: config::Protocol::Udp,
tls_dns_name: None,
});
Ok(ResolverFuture::new(config, ResolverOpts::default()))
},
None => ResolverFuture::from_system_conf(),
}).flatten();

future
.map_err(From::from)
Expand Down
12 changes: 11 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use std::fmt;
use std::net::SocketAddr;
use std::net::{IpAddr, SocketAddr};
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;
Expand All @@ -24,6 +24,10 @@ impl fmt::Display for Settings {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "listen address: {}, ", self.listen)?;
write!(f, "preferred protocol: {}, ", self.protocol)?;
match self.resolver {
Some(resolver) => write!(f, "resolver: {}, ", resolver)?,
None => write!(f, "resolver: system, ")?,
}
write!(f, "default number of ICMP packets: {}, ", self.count)?;
write!(f, "maximum number of ICMP packets: {}, ", self.max_count)?;
write!(
Expand All @@ -50,6 +54,7 @@ impl fmt::Display for Settings {
pub struct SettingsInner {
pub listen: SocketAddr,
pub protocol: Protocol,
pub resolver: Option<IpAddr>,
pub count: usize,
pub max_count: usize,
pub ping_timeout: u64,
Expand All @@ -64,6 +69,11 @@ impl Settings {
inner: Arc::new(SettingsInner {
listen: get_env_or("LISTEN", DEFAULT_LISTEN.clone())?,
protocol: get_env_or("DEFAULT_PROTOCOL", Protocol::V4)?,
resolver: match get_env_("RESOLVER") {
Ok(resolver) => Some(resolver),
Err(Error::MissingEnvVar { .. }) => None,
Err(err) => return Err(err.into()),
},
count: get_env_or("DEFAULT_COUNT", 5)?,
max_count: get_env_or("MAX_COUNT", 30)?,
ping_timeout: get_env_or("DEFAULT_PING_TIMEOUT", 1000)?,
Expand Down

0 comments on commit 8d37beb

Please sign in to comment.