Skip to content

Commit

Permalink
update tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
enaut committed Sep 9, 2024
1 parent bf3b3bc commit cab8514
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
18 changes: 10 additions & 8 deletions pslink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ anyhow = "1.0"
argonautica = "0.2"
clap = "2.33"
dotenv = "0.15.0"
fluent-langneg = "0.13"
image = "0.23"
opentelemetry = "0.14"
opentelemetry-jaeger = "0.13"
qrcode = "0.12"
image = "0.25"
opentelemetry = { version = "0.24" }
opentelemetry_sdk = { version = "0.24", features = ["rt-tokio-current-thread"] }
opentelemetry-jaeger = "0.22"
opentelemetry-otlp = "0.17"
opentelemetry-semantic-conventions = "0.16"
qrcode = "0.14"
rand = "0.8"
rpassword = "7.3"
serde = "1.0"
thiserror = "1.0"
tracing-actix-web = "0.4.0-beta.1"
tracing-bunyan-formatter = "0.2.0"
tracing-opentelemetry = "0.13"
tracing-actix-web = "0.7"
tracing-bunyan-formatter = "0.3.0"
tracing-opentelemetry = "0.25"
async-trait = "0.1"
enum-map = {version="1", features = ["serde"]}

Expand Down
78 changes: 49 additions & 29 deletions pslink/src/bin/pslink/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,68 @@ extern crate sqlx;

mod cli;

use std::sync::LazyLock;

use opentelemetry_otlp::WithExportConfig as _;
use pslink::ServerConfig;

use opentelemetry::trace::TracerProvider;
use tracing::instrument;
use tracing::{subscriber::set_global_default, Subscriber};
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};

/// Compose multiple layers into a `tracing`'s subscriber.
#[must_use]
pub fn get_subscriber(name: &str, env_filter: &str) -> impl Subscriber + Send + Sync {
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
// Create a jaeger exporter pipeline for a `trace_demo` service.
let tracer = opentelemetry_jaeger::new_pipeline()
.with_service_name(name)
.install_simple()
.expect("Error initializing Jaeger exporter");
let formatting_layer = tracing_subscriber::fmt::layer().with_target(false);
const APP_NAME: &str = "pslink";

// Create a layer with the configured tracer
let otel_layer = OpenTelemetryLayer::new(tracer);
static RESOURCE: LazyLock<opentelemetry_sdk::Resource> = LazyLock::new(|| {
opentelemetry_sdk::Resource::new(vec![opentelemetry::KeyValue::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
APP_NAME,
)])
});

// Use the tracing subscriber `Registry`, or any other subscriber
// that impls `LookupSpan`
Registry::default()
.with(otel_layer)
.with(env_filter)
.with(formatting_layer)
}
/// Compose multiple layers into a `tracing`'s subscriber.
fn init_telemetry() {
// Start a new otlp trace pipeline.
// Spans are exported in batch - recommended setup for a production application.
opentelemetry::global::set_text_map_propagator(
opentelemetry_sdk::propagation::TraceContextPropagator::new(),
);
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://127.0.0.1:4317"),
)
.with_trace_config(
opentelemetry_sdk::trace::Config::default().with_resource(RESOURCE.clone()),
)
.install_batch(opentelemetry_sdk::runtime::TokioCurrentThread)
.expect("Failed to install OpenTelemetry tracer.")
.tracer_builder(APP_NAME)
.build();

/// Register a subscriber as global default to process span data.
///
/// It should only be called once!
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
set_global_default(subscriber).expect("Failed to set subscriber");
// Filter based on level - trace, debug, info, warn, error
// Tunable via `RUST_LOG` env variable
let env_filter = EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("info"));
// Create a `tracing` layer using the otlp tracer
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
// Create a `tracing` layer to emit spans as structured logs to stdout
let formatting_layer =
tracing_bunyan_formatter::BunyanFormattingLayer::new(APP_NAME.into(), std::io::stdout);
// Combined them all together in a `tracing` subscriber
let subscriber = Registry::default()
.with(env_filter)
.with(telemetry)
.with(tracing_bunyan_formatter::JsonStorageLayer)
.with(formatting_layer);
tracing::subscriber::set_global_default(subscriber)
.expect("Failed to install `tracing` subscriber.")
}

#[instrument]
#[actix_web::main]
async fn main() -> std::result::Result<(), std::io::Error> {
let subscriber = get_subscriber("fhs.li", "info");
init_subscriber(subscriber);
init_telemetry();

match cli::setup().await {
Ok(Some(server_config)) => {
Expand Down

0 comments on commit cab8514

Please sign in to comment.