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 environment with every metric #517

Merged
merged 4 commits into from
Aug 18, 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 @@ -19,6 +19,7 @@
- New configuration option `hostname_tag` ([#513](https://github.com/getsentry/symbolicator/pull/513))
- Introduced a new cache status to represent failures specific to the cache: Download failures that aren't related to the file being missing in download caches and conversion errors in derived caches. It is currently unused. ([#509](https://github.com/getsentry/symbolicator/pull/509))
- Malformed and Cache-specific Error cache entries now contain some diagnostic info. ([#510](https://github.com/getsentry/symbolicator/pull/510))
- New configuration option `environment_tag` ([#517](https://github.com/getsentry/symbolicator/pull/517))

### Fixes

Expand Down
29 changes: 21 additions & 8 deletions crates/symbolicator/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Exposes the command line application.
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
Expand Down Expand Up @@ -63,7 +64,7 @@ pub fn execute() -> Result<()> {
let cli = Cli::from_args();
let config = Config::get(cli.config()).context("failed loading config")?;

let _sentry = sentry::init(sentry::ClientOptions {
let sentry = sentry::init(sentry::ClientOptions {
dsn: config.sentry_dsn.clone(),
release: Some(env!("SYMBOLICATOR_RELEASE").into()),
session_mode: sentry::SessionMode::Request,
Expand All @@ -73,13 +74,25 @@ pub fn execute() -> Result<()> {

logging::init_logging(&config);
if let Some(ref statsd) = config.metrics.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);
let mut tags = BTreeMap::new();

if let Some(hostname_tag) = config.metrics.hostname_tag.clone() {
if let Some(hostname) = hostname::get().ok().and_then(|s| s.into_string().ok()) {
tags.insert(hostname_tag, hostname);
} else {
log::error!("could not read host name");
}
};
if let Some(environment_tag) = config.metrics.environment_tag.clone() {
if let Some(environment) = sentry.options().environment.as_ref().map(|s| s.to_string())
{
tags.insert(environment_tag, environment);
} else {
log::error!("environment name not available");
}
};

metrics::configure_statsd(&config.metrics.prefix, statsd, tags);
}

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 @@ -57,6 +57,8 @@ pub struct 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>,
/// A tag name to report the environment to, for each metric. Defaults to not sending such a tag.
pub environment_tag: Option<String>,
}

impl Default for Metrics {
Expand All @@ -68,6 +70,7 @@ impl Default for Metrics {
},
prefix: "symbolicator".into(),
hostname_tag: None,
environment_tag: None,
}
}
}
Expand Down
21 changes: 8 additions & 13 deletions crates/symbolicator/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//! Provides access to the metrics sytem.
use std::collections::BTreeMap;
use std::net::ToSocketAddrs;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;

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

type Hostname = String;
type HostnameTag = String;

lazy_static::lazy_static! {
static ref METRICS_CLIENT: RwLock<Option<Arc<MetricsClient>>> = RwLock::new(None);
}
Expand All @@ -33,8 +31,9 @@ pub mod prelude {
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)>,

/// A collection of tags and values that will be sent with every metric.
tags: BTreeMap<String, String>,
}

impl MetricsClient {
Expand All @@ -43,8 +42,8 @@ impl MetricsClient {
where
T: Metric + From<String>,
{
if let Some((tag, name)) = self.hostname.as_ref() {
metric = metric.with_tag(tag, name);
for (tag, value) in self.tags.iter() {
metric = metric.with_tag(tag, value);
}
metric.send()
}
Expand All @@ -70,11 +69,7 @@ pub fn set_client(client: MetricsClient) {
}

/// Tell the metrics system to report to statsd.
pub fn configure_statsd<A: ToSocketAddrs>(
prefix: &str,
host: A,
hostname: Option<(HostnameTag, Hostname)>,
) {
pub fn configure_statsd<A: ToSocketAddrs>(prefix: &str, host: A, tags: BTreeMap<String, String>) {
let addrs: Vec<_> = host.to_socket_addrs().unwrap().collect();
if !addrs.is_empty() {
log::info!("Reporting metrics to statsd at {}", addrs[0]);
Expand All @@ -85,7 +80,7 @@ pub fn configure_statsd<A: ToSocketAddrs>(
let statsd_client = StatsdClient::from_sink(prefix, sink);
set_client(MetricsClient {
statsd_client,
hostname,
tags,
});
}

Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ metrics:
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.
- `environment_tag`: If set, report the current environment 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