Skip to content

Commit

Permalink
feat: move telemetry-otlp setup into telemetry crate (#446)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Paitrault <simon.paitrault@gmail.com>
  • Loading branch information
Freyskeyd authored Feb 8, 2024
1 parent 23cc558 commit 8a15fc4
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions crates/topos-telemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ tracing-opentelemetry.workspace = true
tracing.workspace = true
tonic.workspace = true

tracing-subscriber = { optional = true, workspace = true, features = ["env-filter", "json", "ansi", "fmt"] }
opentelemetry-otlp = { optional = true, workspace = true, features = ["grpc-tonic", "metrics", "tls-roots"] }

serde = { workspace = true, features = ["derive", "std"] }

[features]
tracing = ["tracing-subscriber", "opentelemetry-otlp"]
8 changes: 6 additions & 2 deletions crates/topos-telemetry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{collections::HashMap, str::FromStr};

use ::tracing::warn;
use opentelemetry::{
global,
propagation::{Extractor, Injector},
Expand All @@ -8,6 +9,9 @@ use opentelemetry::{
use serde::{Deserialize, Serialize};
use tonic::metadata::MetadataKey;

#[cfg(feature = "tracing")]
pub mod tracing;

pub struct TonicMetaInjector<'a>(pub &'a mut tonic::metadata::MetadataMap);
pub struct TonicMetaExtractor<'a>(pub &'a tonic::metadata::MetadataMap);

Expand All @@ -32,10 +36,10 @@ impl<'a> Injector for TonicMetaInjector<'a> {
if let Ok(val) = value.parse() {
self.0.insert(key, val);
} else {
tracing::warn!("Invalid value: {}", value);
warn!("Invalid value: {}", value);
}
} else {
tracing::warn!("Invalid key: {}", key);
warn!("Invalid key: {}", key);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ fn verbose_to_level(verbose: u8) -> Level {
}
}

fn build_resources(otlp_service_name: String) -> Vec<KeyValue> {
fn build_resources(otlp_service_name: String, version: &'static str) -> Vec<KeyValue> {
let mut resources = Vec::new();

resources.push(KeyValue::new("service.name", otlp_service_name));
resources.push(KeyValue::new("service.version", env!("TOPOS_VERSION")));
resources.push(KeyValue::new("service.version", version));

let custom_resources: Vec<_> = std::env::var("TOPOS_OTLP_TAGS")
.unwrap_or_default()
Expand Down Expand Up @@ -60,11 +60,12 @@ fn create_filter(verbose: u8) -> EnvFilter {

// Setup tracing
// If otlp agent and otlp service name are provided, opentelemetry collection will be used
pub(crate) fn setup_tracing(
pub fn setup_tracing(
verbose: u8,
no_color: bool,
otlp_agent: Option<String>,
otlp_service_name: Option<String>,
version: &'static str,
) -> Result<Option<BasicController>, Box<dyn std::error::Error>> {
let mut layers = Vec::new();

Expand Down Expand Up @@ -98,7 +99,7 @@ pub(crate) fn setup_tracing(
let metrics: Option<_> = if let (Some(otlp_agent), Some(otlp_service_name)) =
(otlp_agent, otlp_service_name)
{
let resources = build_resources(otlp_service_name);
let resources = build_resources(otlp_service_name, version);

let mut trace_config = opentelemetry::sdk::trace::config();

Expand Down
1 change: 1 addition & 0 deletions crates/topos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ topos-core = { workspace = true, features = ["api"] }
topos-certificate-spammer = { path = "../topos-certificate-spammer" }
topos-tce-broadcast = { path = "../topos-tce-broadcast", optional = true }
topos-wallet = { path = "../topos-wallet" }
topos-telemetry = { path = "../topos-telemetry/", features = ["tracing"] }

async-stream.workspace = true
async-trait.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/topos/src/components/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use tokio::{
};
use tokio_util::sync::CancellationToken;
use tonic::transport::{Channel, Endpoint};
use topos_telemetry::tracing::setup_tracing;
use tower::Service;
use tracing::{error, info};
use tracing_opentelemetry::OpenTelemetrySpanExt;

use self::commands::{NodeCommand, NodeCommands};
use crate::tracing::setup_tracing;
use topos_config::{edge::command::BINARY_NAME, genesis::Genesis, Config};
use topos_config::{node::NodeConfig, node::NodeRole};
use topos_core::api::grpc::tce::v1::console_service_client::ConsoleServiceClient;
Expand Down Expand Up @@ -200,6 +200,7 @@ pub(crate) async fn handle_command(
no_color,
cmd_cloned.otlp_agent,
cmd_cloned.otlp_service_name,
env!("TOPOS_VERSION"),
)?;

let (shutdown_sender, shutdown_receiver) = mpsc::channel(1);
Expand Down
12 changes: 8 additions & 4 deletions crates/topos/src/components/regtest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use tokio::{
sync::{mpsc, oneshot},
};
use topos_certificate_spammer::CertificateSpammerConfig;
use topos_telemetry::tracing::setup_tracing;
use tracing::{error, info};
use tracing_opentelemetry::OpenTelemetrySpanExt;

use crate::tracing::setup_tracing;

pub(crate) mod commands;

pub(crate) async fn handle_command(
Expand All @@ -34,8 +33,13 @@ pub(crate) async fn handle_command(

// Setup instrumentation if both otlp agent and otlp service name
// are provided as arguments
let basic_controller =
setup_tracing(verbose, false, cmd.otlp_agent, cmd.otlp_service_name)?;
let basic_controller = setup_tracing(
verbose,
false,
cmd.otlp_agent,
cmd.otlp_service_name,
env!("TOPOS_VERSION"),
)?;

let (shutdown_sender, shutdown_receiver) = mpsc::channel::<oneshot::Sender<()>>(1);
let mut runtime = spawn(topos_certificate_spammer::run(config, shutdown_receiver));
Expand Down
1 change: 0 additions & 1 deletion crates/topos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use clap::Parser;

pub(crate) mod components;
pub(crate) mod options;
mod tracing;

use crate::options::ToposCommand;
use tracing_log::LogTracer;
Expand Down

0 comments on commit 8a15fc4

Please sign in to comment.