WIP: Enhance tracing in Peer

- Add a bunch of events to Peer's async connection handling logic
- Use an EnvFilter to allow more control over the verbosity level of tracing on a per-module basis
- Wire up logging to emit trace events (we actually probably want to do this the other way around)

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Nathan Sobo 2022-05-12 09:58:17 -06:00
parent 85d9ac5b95
commit 9f6e82720d
8 changed files with 148 additions and 53 deletions

View file

@ -11,7 +11,9 @@ use std::{
net::{SocketAddr, TcpListener},
sync::Arc,
};
use tracing::metadata::LevelFilter;
use tracing_log::LogTracer;
use tracing_subscriber::filter::EnvFilter;
use util::ResultExt;
#[derive(Default, Deserialize)]
pub struct Config {
@ -20,7 +22,7 @@ pub struct Config {
pub api_token: String,
pub honeycomb_api_key: Option<String>,
pub honeycomb_dataset: Option<String>,
pub trace_level: Option<String>,
pub rust_log: Option<String>,
}
pub struct AppState {
@ -41,8 +43,6 @@ impl AppState {
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
if let Err(error) = env::load_dotenv() {
log::error!(
"error loading .env.toml (this is expected in production): {}",
@ -119,42 +119,44 @@ pub fn init_tracing(config: &Config) -> Option<()> {
use std::str::FromStr;
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::layer::SubscriberExt;
let rust_trace = config.rust_log.clone()?;
let (honeycomb_api_key, honeycomb_dataset) = config
LogTracer::init().log_err()?;
let open_telemetry_layer = config
.honeycomb_api_key
.clone()
.zip(config.honeycomb_dataset.clone())?;
.zip(config.honeycomb_dataset.clone())
.map(|(honeycomb_api_key, honeycomb_dataset)| {
let mut metadata = tonic::metadata::MetadataMap::new();
metadata.insert("x-honeycomb-team", honeycomb_api_key.parse().unwrap());
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("https://api.honeycomb.io")
.with_metadata(metadata),
)
.with_trace_config(opentelemetry::sdk::trace::config().with_resource(
opentelemetry::sdk::Resource::new(vec![KeyValue::new(
"service.name",
honeycomb_dataset,
)]),
))
.install_batch(opentelemetry::runtime::Tokio)
.expect("failed to initialize tracing");
let mut metadata = tonic::metadata::MetadataMap::new();
metadata.insert("x-honeycomb-team", honeycomb_api_key.parse().unwrap());
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("https://api.honeycomb.io")
.with_metadata(metadata),
)
.with_trace_config(opentelemetry::sdk::trace::config().with_resource(
opentelemetry::sdk::Resource::new(vec![KeyValue::new(
"service.name",
honeycomb_dataset,
)]),
))
.install_batch(opentelemetry::runtime::Tokio)
.expect("failed to initialize tracing");
OpenTelemetryLayer::new(tracer)
});
let subscriber = tracing_subscriber::Registry::default()
.with(OpenTelemetryLayer::new(tracer))
.with(tracing_subscriber::fmt::layer())
.with(open_telemetry_layer)
.with(
config
.trace_level
.as_ref()
.map_or(LevelFilter::INFO, |level| {
LevelFilter::from_str(level).unwrap()
}),
);
tracing_subscriber::fmt::layer()
.event_format(tracing_subscriber::fmt::format().pretty()),
)
.with(EnvFilter::from_str(rust_trace.as_str()).log_err()?);
tracing::subscriber::set_global_default(subscriber).unwrap();