Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): Add option to send hostname with every metric #513

Merged
merged 4 commits into from
Aug 3, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- GCS, S3, HTTP, and local filesystem sources: Attempt to retry failed downloads at least once. ([#485](https://github.com/getsentry/symbolicator/pull/485))
- Refresh symcaches when a new `BcSymbolMap` becomes available. ([#493](https://github.com/getsentry/symbolicator/pull/493))
- Cache download failures and do not retry the download for a while ([#484](https://github.com/getsentry/symbolicator/pull/484), [#501](https://github.com/getsentry/symbolicator/pull/501))
- New configuration option `hostname_tag` ([#513](https://github.com/getsentry/symbolicator/pull/513))

### Tools

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/symbolicator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fragile = "1.0.0" # used for vendoring sentry-actix
futures = { version = "0.3.12", features = ["compat"] }
futures01 = { version = "0.1.29", package = "futures" }
glob = "0.3.0"
hostname = "0.3.1"
humantime-serde = "1.0.1"
ipnetwork = "0.18.0"
jsonwebtoken = "7.2.0"
Expand Down
8 changes: 7 additions & 1 deletion crates/symbolicator/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ pub fn execute() -> Result<()> {

logging::init_logging(&config);
if let Some(ref statsd) = config.metrics.statsd {
metrics::configure_statsd(&config.metrics.prefix, statsd);
let hostname = config.metrics.hostname_tag.clone().and_then(|tag| {
hostname::get()
.ok()
.and_then(|s| s.into_string().ok())
.map(|name| (tag, name))
});
metrics::configure_statsd(&config.metrics.prefix, statsd, hostname);
}

procspawn::ProcConfig::new()
Expand Down
3 changes: 3 additions & 0 deletions crates/symbolicator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct Metrics {
pub statsd: Option<String>,
/// The prefix that should be added to all metrics.
pub prefix: String,
/// A tag name to report the hostname to, for each metric. Defaults to not sending such a tag.
pub hostname_tag: Option<String>,
}

impl Default for Metrics {
Expand All @@ -65,6 +67,7 @@ impl Default for Metrics {
Err(_) => None,
},
prefix: "symbolicator".into(),
hostname_tag: None,
}
}
}
Expand Down
105 changes: 79 additions & 26 deletions crates/symbolicator/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
//! Provides access to the metrics sytem.
use std::net::ToSocketAddrs;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;

use cadence::{StatsdClient, UdpMetricSink};
use cadence::{Metric, MetricBuilder, StatsdClient, UdpMetricSink};
use parking_lot::RwLock;

type Hostname = String;
type HostnameTag = String;
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm curious why you chose aliases rather than newtypes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted it to be clear which string is which in the definition of MetricsClient. I don't think a newtype would provide any extra safety in this situation.


lazy_static::lazy_static! {
static ref METRICS_CLIENT: RwLock<Option<Arc<StatsdClient>>> = RwLock::new(None);
static ref METRICS_CLIENT: RwLock<Option<Arc<MetricsClient>>> = RwLock::new(None);
}

thread_local! {
static CURRENT_CLIENT: Option<Arc<StatsdClient>> = METRICS_CLIENT.read().clone();
static CURRENT_CLIENT: Option<Arc<MetricsClient>> = METRICS_CLIENT.read().clone();
}

/// Internal prelude for the macro
Expand All @@ -25,21 +29,64 @@ pub mod prelude {
pub use cadence::prelude::*;
}

#[derive(Debug)]
pub struct MetricsClient {
/// The raw statsd client.
pub statsd_client: StatsdClient,
/// The hostname and the tag to report it to.
pub hostname: Option<(HostnameTag, Hostname)>,
}

impl MetricsClient {
#[inline(always)]
pub fn send_metric<'a, T>(&'a self, mut metric: MetricBuilder<'a, '_, T>)
where
T: Metric + From<String>,
{
if let Some((tag, name)) = self.hostname.as_ref() {
metric = metric.with_tag(tag, name);
}
metric.send()
}
}

impl Deref for MetricsClient {
type Target = StatsdClient;

fn deref(&self) -> &Self::Target {
&self.statsd_client
}
}

impl DerefMut for MetricsClient {
fn deref_mut(&mut self) -> &mut StatsdClient {
&mut self.statsd_client
}
}

/// Set a new statsd client.
pub fn set_client(statsd_client: StatsdClient) {
*METRICS_CLIENT.write() = Some(Arc::new(statsd_client));
pub fn set_client(client: MetricsClient) {
*METRICS_CLIENT.write() = Some(Arc::new(client));
}

/// Tell the metrics system to report to statsd.
pub fn configure_statsd<A: ToSocketAddrs>(prefix: &str, host: A) {
pub fn configure_statsd<A: ToSocketAddrs>(
prefix: &str,
host: A,
hostname: Option<(HostnameTag, Hostname)>,
) {
let addrs: Vec<_> = host.to_socket_addrs().unwrap().collect();
if !addrs.is_empty() {
log::info!("Reporting metrics to statsd at {}", addrs[0]);
}
let socket = std::net::UdpSocket::bind("0.0.0.0:0").unwrap();
socket.set_nonblocking(true).unwrap();
let sink = UdpMetricSink::from(&addrs[..], socket).unwrap();
set_client(StatsdClient::from_sink(prefix, sink));
let statsd_client = StatsdClient::from_sink(prefix, sink);
set_client(MetricsClient {
statsd_client,
hostname,
});
}

/// Invoke a callback with the current statsd client.
Expand All @@ -49,7 +96,7 @@ pub fn configure_statsd<A: ToSocketAddrs>(prefix: &str, host: A) {
#[inline(always)]
pub fn with_client<F, R>(f: F) -> R
where
F: FnOnce(&StatsdClient) -> R,
F: FnOnce(&MetricsClient) -> R,
R: Default,
{
CURRENT_CLIENT.with(|client| {
Expand All @@ -68,57 +115,63 @@ macro_rules! metric {
(counter($id:expr) += $value:expr $(, $k:expr => $v:expr)* $(,)?) => {{
use $crate::metrics::_pred::*;
$crate::metrics::with_client(|client| {
client.count_with_tags($id, $value)
$(.with_tag($k, $v))*
.send();
client.send_metric(
client.count_with_tags($id, $value)
$(.with_tag($k, $v))*
);
})
}};
(counter($id:expr) -= $value:expr $(, $k:expr => $v:expr)* $(,)?) => {{
use $crate::metrics::_pred::*;
$crate::metrics::with_client(|client| {
client.count_with_tags($id, -$value)
$(.with_tag($k, $v))*
.send();
client.send_metric(
client.count_with_tags($id, -$value)
$(.with_tag($k, $v))*
);
})
}};

// gauges
(gauge($id:expr) = $value:expr $(, $k:expr => $v:expr)* $(,)?) => {{
use $crate::metrics::_pred::*;
$crate::metrics::with_client(|client| {
client.gauge_with_tags($id, $value)
$(.with_tag($k, $v))*
.send();
client.send_metric(
client.gauge_with_tags($id, $value)
$(.with_tag($k, $v))*
);
})
}};

// timers
(timer($id:expr) = $value:expr $(, $k:expr => $v:expr)* $(,)?) => {{
use $crate::metrics::_pred::*;
$crate::metrics::with_client(|client| {
client.time_duration_with_tags($id, $value)
$(.with_tag($k, $v))*
.send();
client.send_metric(
client.time_duration_with_tags($id, $value)
$(.with_tag($k, $v))*
);
})
}};

// we use statsd timers to send things such as filesizes as well.
(time_raw($id:expr) = $value:expr $(, $k:expr => $v:expr)* $(,)?) => {{
use $crate::metrics::_pred::*;
$crate::metrics::with_client(|client| {
client.time_with_tags($id, $value)
$(.with_tag($k, $v))*
.send();
client.send_metric(
client.time_with_tags($id, $value)
$(.with_tag($k, $v))*
);
})
}};

// histograms
(histogram($id:expr) = $value:expr $(, $k:expr => $v:expr)* $(,)?) => {{
use $crate::metrics::_pred::*;
$crate::metrics::with_client(|client| {
client.histogram_with_tags($id, $value)
$(.with_tag($k, $v))*
.send();
client.send_metric(
client.histogram_with_tags($id, $value)
$(.with_tag($k, $v))*
);
})
}};
}
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ metrics:
environment variable or in case it is not defined, then it defaults to `null`,
which disables metric submission.
- `prefix`: A prefix for every metric, defaults to `symbolicator`.
- `hostname_tag`: If set, report the current hostname under the given tag name for all metrics.
- `sentry_dsn`: DSN to a Sentry project for internal error reporting. Defaults
to `null`, which disables reporting to Sentry.
- `sources`: An optional list of preconfigured sources. If these are configured
Expand Down