feat(tvix/tracing): introduce common tvix-tracing crate

Introduce a new common crate that contains tracing boilerplate which then
can be used in the cli, tvix-store and tvix-build crates.
It has otlp as an optional feature, which is currently only used by
tvix-store.

Change-Id: I41468ac4d9c65174515d721513b96fea463d6ed2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11758
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Autosubmit: Simon Hauser <simon.hauser@helsinki-systems.de>
This commit is contained in:
Simon Hauser 2024-06-06 15:44:11 +02:00 committed by clbot
parent 11a6ff7706
commit 825d498908
15 changed files with 271 additions and 203 deletions

26
tvix/Cargo.lock generated
View file

@ -4127,8 +4127,8 @@ dependencies = [
"tonic-build", "tonic-build",
"tonic-reflection", "tonic-reflection",
"tracing", "tracing",
"tracing-subscriber",
"tvix-castore", "tvix-castore",
"tvix-tracing",
"url", "url",
] ]
@ -4204,12 +4204,12 @@ dependencies = [
"tikv-jemallocator", "tikv-jemallocator",
"tokio", "tokio",
"tracing", "tracing",
"tracing-subscriber",
"tvix-build", "tvix-build",
"tvix-castore", "tvix-castore",
"tvix-eval", "tvix-eval",
"tvix-glue", "tvix-glue",
"tvix-store", "tvix-store",
"tvix-tracing",
"wu-manber", "wu-manber",
] ]
@ -4326,13 +4326,9 @@ dependencies = [
"count-write", "count-write",
"data-encoding", "data-encoding",
"futures", "futures",
"indicatif",
"lazy_static", "lazy_static",
"lru", "lru",
"nix-compat", "nix-compat",
"opentelemetry",
"opentelemetry-otlp",
"opentelemetry_sdk",
"parking_lot 0.12.2", "parking_lot 0.12.2",
"pin-project-lite", "pin-project-lite",
"prost", "prost",
@ -4359,13 +4355,27 @@ dependencies = [
"tower", "tower",
"tracing", "tracing",
"tracing-indicatif", "tracing-indicatif",
"tracing-opentelemetry",
"tracing-subscriber",
"tvix-castore", "tvix-castore",
"tvix-tracing",
"url", "url",
"walkdir", "walkdir",
] ]
[[package]]
name = "tvix-tracing"
version = "0.1.0"
dependencies = [
"indicatif",
"lazy_static",
"opentelemetry",
"opentelemetry-otlp",
"opentelemetry_sdk",
"tracing",
"tracing-indicatif",
"tracing-opentelemetry",
"tracing-subscriber",
]
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.17.0" version = "1.17.0"

View file

@ -119,6 +119,16 @@ rec {
packageId = "tvix-store"; packageId = "tvix-store";
}; };
# Debug support which might change between releases.
# File a bug if you depend on any for non-debug work!
debug = internal.debugCrate { inherit packageId; };
};
"tvix-tracing" = rec {
packageId = "tvix-tracing";
build = internal.buildRustCrateWithFeatures {
packageId = "tvix-tracing";
};
# Debug support which might change between releases. # Debug support which might change between releases.
# File a bug if you depend on any for non-debug work! # File a bug if you depend on any for non-debug work!
debug = internal.debugCrate { inherit packageId; }; debug = internal.debugCrate { inherit packageId; };
@ -12953,14 +12963,14 @@ rec {
name = "tracing"; name = "tracing";
packageId = "tracing"; packageId = "tracing";
} }
{
name = "tracing-subscriber";
packageId = "tracing-subscriber";
}
{ {
name = "tvix-castore"; name = "tvix-castore";
packageId = "tvix-castore"; packageId = "tvix-castore";
} }
{
name = "tvix-tracing";
packageId = "tvix-tracing";
}
{ {
name = "url"; name = "url";
packageId = "url"; packageId = "url";
@ -13298,11 +13308,6 @@ rec {
{ {
name = "tracing"; name = "tracing";
packageId = "tracing"; packageId = "tracing";
features = [ "max_level_trace" "release_max_level_info" ];
}
{
name = "tracing-subscriber";
packageId = "tracing-subscriber";
} }
{ {
name = "tvix-build"; name = "tvix-build";
@ -13325,6 +13330,10 @@ rec {
packageId = "tvix-store"; packageId = "tvix-store";
usesDefaultFeatures = false; usesDefaultFeatures = false;
} }
{
name = "tvix-tracing";
packageId = "tvix-tracing";
}
{ {
name = "wu-manber"; name = "wu-manber";
packageId = "wu-manber"; packageId = "wu-manber";
@ -13794,10 +13803,6 @@ rec {
name = "futures"; name = "futures";
packageId = "futures"; packageId = "futures";
} }
{
name = "indicatif";
packageId = "indicatif";
}
{ {
name = "lazy_static"; name = "lazy_static";
packageId = "lazy_static"; packageId = "lazy_static";
@ -13811,22 +13816,6 @@ rec {
packageId = "nix-compat"; packageId = "nix-compat";
features = [ "async" ]; features = [ "async" ];
} }
{
name = "opentelemetry";
packageId = "opentelemetry";
optional = true;
}
{
name = "opentelemetry-otlp";
packageId = "opentelemetry-otlp";
optional = true;
}
{
name = "opentelemetry_sdk";
packageId = "opentelemetry_sdk";
optional = true;
features = [ "rt-tokio" ];
}
{ {
name = "parking_lot"; name = "parking_lot";
packageId = "parking_lot 0.12.2"; packageId = "parking_lot 0.12.2";
@ -13916,19 +13905,14 @@ rec {
name = "tracing-indicatif"; name = "tracing-indicatif";
packageId = "tracing-indicatif"; packageId = "tracing-indicatif";
} }
{
name = "tracing-opentelemetry";
packageId = "tracing-opentelemetry";
}
{
name = "tracing-subscriber";
packageId = "tracing-subscriber";
features = [ "env-filter" ];
}
{ {
name = "tvix-castore"; name = "tvix-castore";
packageId = "tvix-castore"; packageId = "tvix-castore";
} }
{
name = "tvix-tracing";
packageId = "tvix-tracing";
}
{ {
name = "url"; name = "url";
packageId = "url"; packageId = "url";
@ -13974,12 +13958,72 @@ rec {
"cloud" = [ "dep:bigtable_rs" "tvix-castore/cloud" ]; "cloud" = [ "dep:bigtable_rs" "tvix-castore/cloud" ];
"default" = [ "cloud" "fuse" "otlp" "tonic-reflection" ]; "default" = [ "cloud" "fuse" "otlp" "tonic-reflection" ];
"fuse" = [ "tvix-castore/fuse" ]; "fuse" = [ "tvix-castore/fuse" ];
"otlp" = [ "dep:opentelemetry" "dep:opentelemetry-otlp" "dep:opentelemetry_sdk" ]; "otlp" = [ "tvix-tracing/otlp" ];
"tonic-reflection" = [ "dep:tonic-reflection" "tvix-castore/tonic-reflection" ]; "tonic-reflection" = [ "dep:tonic-reflection" "tvix-castore/tonic-reflection" ];
"virtiofs" = [ "tvix-castore/virtiofs" ]; "virtiofs" = [ "tvix-castore/virtiofs" ];
}; };
resolvedDefaultFeatures = [ "cloud" "default" "fuse" "integration" "otlp" "tonic-reflection" "virtiofs" ]; resolvedDefaultFeatures = [ "cloud" "default" "fuse" "integration" "otlp" "tonic-reflection" "virtiofs" ];
}; };
"tvix-tracing" = rec {
crateName = "tvix-tracing";
version = "0.1.0";
edition = "2021";
# We can't filter paths with references in Nix 2.4
# See https://github.com/NixOS/nix/issues/5410
src =
if ((lib.versionOlder builtins.nixVersion "2.4pre20211007") || (lib.versionOlder "2.5" builtins.nixVersion))
then lib.cleanSourceWith { filter = sourceFilter; src = ./tracing; }
else ./tracing;
dependencies = [
{
name = "indicatif";
packageId = "indicatif";
}
{
name = "lazy_static";
packageId = "lazy_static";
}
{
name = "opentelemetry";
packageId = "opentelemetry";
optional = true;
}
{
name = "opentelemetry-otlp";
packageId = "opentelemetry-otlp";
optional = true;
}
{
name = "opentelemetry_sdk";
packageId = "opentelemetry_sdk";
optional = true;
features = [ "rt-tokio" ];
}
{
name = "tracing";
packageId = "tracing";
features = [ "max_level_trace" "release_max_level_info" ];
}
{
name = "tracing-indicatif";
packageId = "tracing-indicatif";
}
{
name = "tracing-opentelemetry";
packageId = "tracing-opentelemetry";
optional = true;
}
{
name = "tracing-subscriber";
packageId = "tracing-subscriber";
features = [ "env-filter" ];
}
];
features = {
"otlp" = [ "dep:tracing-opentelemetry" "dep:opentelemetry" "dep:opentelemetry-otlp" "dep:opentelemetry_sdk" ];
};
resolvedDefaultFeatures = [ "default" "otlp" ];
};
"typenum" = rec { "typenum" = rec {
crateName = "typenum"; crateName = "typenum";
version = "1.17.0"; version = "1.17.0";

View file

@ -28,6 +28,7 @@ members = [
"nix-compat", "nix-compat",
"serde", "serde",
"store", "store",
"tracing",
] ]
[workspace.lints.clippy] [workspace.lints.clippy]

View file

@ -13,8 +13,8 @@ tokio = { version = "1.32.0" }
tokio-listener = { version = "0.4.1", features = [ "tonic011" ] } tokio-listener = { version = "0.4.1", features = [ "tonic011" ] }
tonic = { version = "0.11.0", features = ["tls", "tls-roots"] } tonic = { version = "0.11.0", features = ["tls", "tls-roots"] }
tvix-castore = { path = "../castore" } tvix-castore = { path = "../castore" }
tracing = "0.1.37" tvix-tracing = { path = "../tracing" }
tracing-subscriber = "0.3.16" tracing = "0.1.40"
url = "2.4.0" url = "2.4.0"
[dependencies.tonic-reflection] [dependencies.tonic-reflection]

View file

@ -7,7 +7,6 @@ use tokio_listener::SystemOptions;
use tokio_listener::UserOptions; use tokio_listener::UserOptions;
use tonic::{self, transport::Server}; use tonic::{self, transport::Server};
use tracing::{info, Level}; use tracing::{info, Level};
use tracing_subscriber::prelude::*;
use tvix_build::{ use tvix_build::{
buildservice, buildservice,
proto::{build_service_server::BuildServiceServer, GRPCBuildServiceWrapper}, proto::{build_service_server::BuildServiceServer, GRPCBuildServiceWrapper},
@ -23,8 +22,12 @@ use tvix_castore::proto::FILE_DESCRIPTOR_SET as CASTORE_FILE_DESCRIPTOR_SET;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
struct Cli { struct Cli {
#[arg(long)] /// A global log level to use when printing logs.
log_level: Option<Level>, /// It's also possible to set `RUST_LOG` according to
/// `tracing_subscriber::filter::EnvFilter`, which will always have
/// priority.
#[arg(long, default_value_t=Level::INFO)]
log_level: Level,
#[command(subcommand)] #[command(subcommand)]
command: Commands, command: Commands,
@ -51,16 +54,7 @@ enum Commands {
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cli = Cli::parse(); let cli = Cli::parse();
// configure log settings tvix_tracing::init(cli.log_level)?;
let level = cli.log_level.unwrap_or(Level::INFO);
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::Layer::new()
.with_writer(std::io::stderr.with_max_level(level))
.pretty(),
)
.init();
match cli.command { match cli.command {
Commands::Daemon { Commands::Daemon {

View file

@ -14,6 +14,7 @@ tvix-castore = { path = "../castore" }
tvix-store = { path = "../store", default-features = false, features = []} tvix-store = { path = "../store", default-features = false, features = []}
tvix-eval = { path = "../eval" } tvix-eval = { path = "../eval" }
tvix-glue = { path = "../glue" } tvix-glue = { path = "../glue" }
tvix-tracing = { path = "../tracing" }
bytes = "1.4.0" bytes = "1.4.0"
clap = { version = "4.0", features = ["derive", "env"] } clap = { version = "4.0", features = ["derive", "env"] }
dirs = "4.0.0" dirs = "4.0.0"
@ -21,8 +22,7 @@ rustyline = "10.0.0"
rnix = "0.11.0" rnix = "0.11.0"
thiserror = "1.0.38" thiserror = "1.0.38"
tokio = "1.28.0" tokio = "1.28.0"
tracing = { version = "0.1.37", features = ["max_level_trace", "release_max_level_info"] } tracing = "0.1.40"
tracing-subscriber = "0.3.16"
[dependencies.wu-manber] [dependencies.wu-manber]
git = "https://github.com/tvlfyi/wu-manber.git" git = "https://github.com/tvlfyi/wu-manber.git"

View file

@ -5,9 +5,6 @@ use repl::Repl;
use std::rc::Rc; use std::rc::Rc;
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use tracing::Level; use tracing::Level;
use tracing_subscriber::fmt::writer::MakeWriterExt;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use tracing_subscriber::{EnvFilter, Layer};
use tvix_build::buildservice; use tvix_build::buildservice;
use tvix_eval::builtins::impure_builtins; use tvix_eval::builtins::impure_builtins;
use tvix_eval::observer::{DisassemblingObserver, TracingObserver}; use tvix_eval::observer::{DisassemblingObserver, TracingObserver};
@ -27,8 +24,12 @@ static GLOBAL: Jemalloc = Jemalloc;
#[derive(Parser, Clone)] #[derive(Parser, Clone)]
struct Args { struct Args {
#[arg(long)] /// A global log level to use when printing logs.
log_level: Option<Level>, /// It's also possible to set `RUST_LOG` according to
/// `tracing_subscriber::filter::EnvFilter`, which will always have
/// priority.
#[arg(long, default_value_t=Level::INFO)]
log_level: Level,
/// Path to a script to evaluate /// Path to a script to evaluate
script: Option<PathBuf>, script: Option<PathBuf>,
@ -270,24 +271,7 @@ fn lint(code: &str, path: Option<PathBuf>, args: &Args) -> bool {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
// configure log settings tvix_tracing::init(args.log_level).expect("unable to set up tracing subscriber");
let level = args.log_level.unwrap_or(Level::INFO);
let subscriber = tracing_subscriber::registry().with(
tracing_subscriber::fmt::Layer::new()
.with_writer(std::io::stderr.with_max_level(level))
.compact()
.with_filter(
EnvFilter::builder()
.with_default_directive(level.into())
.from_env()
.expect("invalid RUST_LOG"),
),
);
subscriber
.try_init()
.expect("unable to set up tracing subscriber");
let tokio_runtime = tokio::runtime::Runtime::new().expect("failed to setup tokio runtime"); let tokio_runtime = tokio::runtime::Runtime::new().expect("failed to setup tokio runtime");
let io_handle = init_io_handle(&tokio_runtime, &args); let io_handle = init_io_handle(&tokio_runtime, &args);

View file

@ -128,6 +128,10 @@ let
src = filterRustCrateSrc { root = prev.src.origSrc; }; src = filterRustCrateSrc { root = prev.src.origSrc; };
}; };
tvix-tracing = prev: {
src = filterRustCrateSrc { root = prev.src.origSrc; };
};
nix-compat = prev: { nix-compat = prev: {
src = filterRustCrateSrc rec { src = filterRustCrateSrc rec {
root = prev.src.origSrc; root = prev.src.origSrc;

View file

@ -198,14 +198,9 @@ logs etc, but this is something requiring a lot of designing.
- Some work ongoing on the worker operation parsing (griff, picnoir) - Some work ongoing on the worker operation parsing (griff, picnoir)
### O11Y ### O11Y
- `[tracing-]indicatif` for progress/log reporting (cl/11747) - Maybe drop `--log-level` entirely, and only use `RUST_LOG` env exclusively?
- Currently there's a lot of boilerplate in the `tvix-store` CLI entrypoint, `debug`,`trace` level across all crates is a bit useless, and `RUST_LOG` can
and half of the boilerplate copied over to `tvix-cli`. be much more granular…
Setup of the tracing things should be unified into the `tvix-tracing` crate,
maybe including some of the CLI parameters (@simon).
Or maybe drop `--log-level` entirely, and only use `RUST_LOG` env
exclusively? `debug`,`trace` level across all crates is a bit useless, and
`RUST_LOG` can be much more granular…
- The OTLP stack is quite spammy if there's no OTLP collector running on - The OTLP stack is quite spammy if there's no OTLP collector running on
localhost. localhost.
https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/ https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/

View file

@ -18,9 +18,6 @@ lazy_static = "1.4.0"
nix-compat = { path = "../nix-compat", features = ["async"] } nix-compat = { path = "../nix-compat", features = ["async"] }
pin-project-lite = "0.2.13" pin-project-lite = "0.2.13"
prost = "0.12.1" prost = "0.12.1"
opentelemetry = { version = "0.22.0", optional = true}
opentelemetry-otlp = { version = "0.15.0", optional = true }
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"], optional = true}
serde = { version = "1.0.197", features = [ "derive" ] } serde = { version = "1.0.197", features = [ "derive" ] }
serde_json = "1.0" serde_json = "1.0"
serde_with = "3.7.0" serde_with = "3.7.0"
@ -34,16 +31,14 @@ tokio-stream = { version = "0.1.14", features = ["fs"] }
tokio-util = { version = "0.7.9", features = ["io", "io-util", "compat"] } tokio-util = { version = "0.7.9", features = ["io", "io-util", "compat"] }
tonic = { version = "0.11.0", features = ["tls", "tls-roots"] } tonic = { version = "0.11.0", features = ["tls", "tls-roots"] }
tower = "0.4.13" tower = "0.4.13"
tracing = "0.1.37"
tracing-opentelemetry = "0.23.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tvix-castore = { path = "../castore" } tvix-castore = { path = "../castore" }
url = "2.4.0" url = "2.4.0"
walkdir = "2.4.0" walkdir = "2.4.0"
reqwest = { version = "0.11.22", features = ["rustls-tls-native-roots", "stream"], default-features = false } reqwest = { version = "0.11.22", features = ["rustls-tls-native-roots", "stream"], default-features = false }
lru = "0.12.3" lru = "0.12.3"
parking_lot = "0.12.2" parking_lot = "0.12.2"
indicatif = "0.17.8" tvix-tracing = { path = "../tracing" }
tracing = "0.1.40"
tracing-indicatif = "0.3.6" tracing-indicatif = "0.3.6"
[dependencies.tonic-reflection] [dependencies.tonic-reflection]
@ -74,7 +69,7 @@ cloud = [
"tvix-castore/cloud" "tvix-castore/cloud"
] ]
fuse = ["tvix-castore/fuse"] fuse = ["tvix-castore/fuse"]
otlp = ["dep:opentelemetry", "dep:opentelemetry-otlp", "dep:opentelemetry_sdk"] otlp = ["tvix-tracing/otlp"]
tonic-reflection = ["dep:tonic-reflection", "tvix-castore/tonic-reflection"] tonic-reflection = ["dep:tonic-reflection", "tvix-castore/tonic-reflection"]
virtiofs = ["tvix-castore/virtiofs"] virtiofs = ["tvix-castore/virtiofs"]
# Whether to run the integration tests. # Whether to run the integration tests.

View file

@ -4,7 +4,6 @@ use clap::Subcommand;
use futures::future::try_join_all; use futures::future::try_join_all;
use futures::StreamExt; use futures::StreamExt;
use futures::TryStreamExt; use futures::TryStreamExt;
use indicatif::ProgressStyle;
use nix_compat::path_info::ExportedPathInfo; use nix_compat::path_info::ExportedPathInfo;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
@ -14,14 +13,8 @@ use tokio_listener::Listener;
use tokio_listener::SystemOptions; use tokio_listener::SystemOptions;
use tokio_listener::UserOptions; use tokio_listener::UserOptions;
use tonic::transport::Server; use tonic::transport::Server;
use tracing::info; use tracing::{info, info_span, instrument, Level, Span};
use tracing::info_span; use tracing_indicatif::span_ext::IndicatifSpanExt as _;
use tracing::instrument;
use tracing::Level;
use tracing::Span;
use tracing_indicatif::filter::IndicatifFilter;
use tracing_indicatif::{span_ext::IndicatifSpanExt, IndicatifLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
use tvix_castore::import::fs::ingest_path; use tvix_castore::import::fs::ingest_path;
use tvix_store::nar::NarCalculationService; use tvix_store::nar::NarCalculationService;
use tvix_store::proto::NarInfo; use tvix_store::proto::NarInfo;
@ -35,35 +28,12 @@ use tvix_store::pathinfoservice::PathInfoService;
use tvix_store::proto::path_info_service_server::PathInfoServiceServer; use tvix_store::proto::path_info_service_server::PathInfoServiceServer;
use tvix_store::proto::GRPCPathInfoServiceWrapper; use tvix_store::proto::GRPCPathInfoServiceWrapper;
use lazy_static::lazy_static;
// FUTUREWORK: move this to tracing crate
lazy_static! {
pub static ref PB_PROGRESS_STYLE: ProgressStyle = ProgressStyle::with_template(
"{span_child_prefix}{bar:30} {wide_msg} [{elapsed_precise}] {pos:>7}/{len:7}"
)
.expect("invalid progress template");
pub static ref PB_SPINNER_STYLE: ProgressStyle = ProgressStyle::with_template(
"{span_child_prefix}{spinner} {wide_msg} [{elapsed_precise}] {pos:>7}/{len:7}"
)
.expect("invalid progress template");
}
#[cfg(any(feature = "fuse", feature = "virtiofs"))] #[cfg(any(feature = "fuse", feature = "virtiofs"))]
use tvix_store::pathinfoservice::make_fs; use tvix_store::pathinfoservice::make_fs;
#[cfg(feature = "fuse")] #[cfg(feature = "fuse")]
use tvix_castore::fs::fuse::FuseDaemon; use tvix_castore::fs::fuse::FuseDaemon;
#[cfg(feature = "otlp")]
use opentelemetry::KeyValue;
#[cfg(feature = "otlp")]
use opentelemetry_sdk::{
resource::{ResourceDetector, SdkProvidedResourceDetector},
trace::BatchConfig,
Resource,
};
#[cfg(feature = "virtiofs")] #[cfg(feature = "virtiofs")]
use tvix_castore::fs::virtiofs::start_virtiofs_daemon; use tvix_castore::fs::virtiofs::start_virtiofs_daemon;
@ -83,8 +53,8 @@ struct Cli {
/// It's also possible to set `RUST_LOG` according to /// It's also possible to set `RUST_LOG` according to
/// `tracing_subscriber::filter::EnvFilter`, which will always have /// `tracing_subscriber::filter::EnvFilter`, which will always have
/// priority. /// priority.
#[arg(long)] #[arg(long, default_value_t=Level::INFO)]
log_level: Option<Level>, log_level: Level,
#[command(subcommand)] #[command(subcommand)]
command: Commands, command: Commands,
@ -234,75 +204,18 @@ fn default_threads() -> usize {
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cli = Cli::parse(); let cli = Cli::parse();
// configure log settings
let level = cli.log_level.unwrap_or(Level::INFO);
let indicatif_layer = IndicatifLayer::new().with_progress_style(PB_SPINNER_STYLE.clone());
// Set up the tracing subscriber.
let subscriber = tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::Layer::new()
.with_writer(indicatif_layer.get_stderr_writer())
.compact()
.with_filter(
EnvFilter::builder()
.with_default_directive(level.into())
.from_env()
.expect("invalid RUST_LOG"),
),
)
.with(indicatif_layer.with_filter(
// only show progress for spans with indicatif.pb_show field being set
IndicatifFilter::new(false),
));
// Add the otlp layer (when otlp is enabled, and it's not disabled in the CLI)
// then init the registry.
// If the feature is feature-flagged out, just init without adding the layer.
// It's necessary to do this separately, as every with() call chains the
// layer into the type of the registry.
#[cfg(feature = "otlp")] #[cfg(feature = "otlp")]
{ {
let subscriber = if cli.otlp { if cli.otlp {
let tracer = opentelemetry_otlp::new_pipeline() tvix_tracing::init_with_otlp(cli.log_level, "tvix.store")?;
.tracing()
.with_exporter(opentelemetry_otlp::new_exporter().tonic())
.with_batch_config(BatchConfig::default())
.with_trace_config(opentelemetry_sdk::trace::config().with_resource({
// use SdkProvidedResourceDetector.detect to detect resources,
// but replace the default service name with our default.
// https://github.com/open-telemetry/opentelemetry-rust/issues/1298
let resources =
SdkProvidedResourceDetector.detect(std::time::Duration::from_secs(0));
// SdkProvidedResourceDetector currently always sets
// `service.name`, but we don't like its default.
if resources.get("service.name".into()).unwrap() == "unknown_service".into() {
resources.merge(&Resource::new([KeyValue::new(
"service.name",
"tvix.store",
)]))
} else {
resources
}
}))
.install_batch(opentelemetry_sdk::runtime::Tokio)?;
// Create a tracing layer with the configured tracer
let layer = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(Some(layer))
} else { } else {
subscriber.with(None) tvix_tracing::init(cli.log_level)?;
}; }
subscriber.try_init()?;
} }
// Init the registry (when otlp is not enabled)
#[cfg(not(feature = "otlp"))] #[cfg(not(feature = "otlp"))]
{ {
subscriber.try_init()?; tvix_tracing::init(cli.log_level)?;
} }
match cli.command { match cli.command {
@ -383,7 +296,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let root_span = { let root_span = {
let s = Span::current(); let s = Span::current();
s.pb_set_style(&PB_PROGRESS_STYLE); s.pb_set_style(&tvix_tracing::PB_PROGRESS_STYLE);
s.pb_set_message("Importing paths"); s.pb_set_message("Importing paths");
s.pb_set_length(paths.len() as u64); s.pb_set_length(paths.len() as u64);
s.pb_start(); s.pb_start();
@ -458,7 +371,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
"indicatif.pb_show" = tracing::field::Empty "indicatif.pb_show" = tracing::field::Empty
); );
lookups_span.pb_set_length(reference_graph.closure.len() as u64); lookups_span.pb_set_length(reference_graph.closure.len() as u64);
lookups_span.pb_set_style(&PB_PROGRESS_STYLE); lookups_span.pb_set_style(&tvix_tracing::PB_PROGRESS_STYLE);
lookups_span.pb_start(); lookups_span.pb_start();
// From our reference graph, lookup all pathinfos that might exist. // From our reference graph, lookup all pathinfos that might exist.

View file

@ -6,8 +6,7 @@ use nix_compat::nixbase32;
use prost::Message; use prost::Message;
use std::path::Path; use std::path::Path;
use tonic::async_trait; use tonic::async_trait;
use tracing::instrument; use tracing::{instrument, warn};
use tracing::warn;
use tvix_castore::Error; use tvix_castore::Error;
/// SledPathInfoService stores PathInfo in a [sled](https://github.com/spacejam/sled). /// SledPathInfoService stores PathInfo in a [sled](https://github.com/spacejam/sled).

28
tvix/tracing/Cargo.toml Normal file
View file

@ -0,0 +1,28 @@
[package]
name = "tvix-tracing"
version = "0.1.0"
edition = "2021"
[dependencies]
lazy_static = "1.4.0"
tracing = { version = "0.1.40", features = ["max_level_trace", "release_max_level_info"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
indicatif = "0.17.8"
tracing-indicatif = "0.3.6"
tracing-opentelemetry = { version = "0.23.0", optional = true }
opentelemetry = { version = "0.22.0", optional = true }
opentelemetry-otlp = { version = "0.15.0", optional = true }
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"], optional = true }
[features]
default = []
otlp = [
"dep:tracing-opentelemetry",
"dep:opentelemetry",
"dep:opentelemetry-otlp",
"dep:opentelemetry_sdk"
]
[lints]
workspace = true

11
tvix/tracing/default.nix Normal file
View file

@ -0,0 +1,11 @@
{ depot, lib, ... }:
(depot.tvix.crates.workspaceMembers.tvix-tracing.build.override {
runTests = true;
}).overrideAttrs (old: rec {
meta.ci.targets = lib.filter (x: lib.hasPrefix "with-features" x || x == "no-features") (lib.attrNames passthru);
passthru = depot.tvix.utils.mkFeaturePowerset {
inherit (old) crateName;
features = [ "otlp" ];
};
})

90
tvix/tracing/src/lib.rs Normal file
View file

@ -0,0 +1,90 @@
use indicatif::ProgressStyle;
use lazy_static::lazy_static;
use tracing::Level;
use tracing_indicatif::{filter::IndicatifFilter, IndicatifLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
#[cfg(feature = "otlp")]
use opentelemetry::KeyValue;
#[cfg(feature = "otlp")]
use opentelemetry_sdk::{
resource::{ResourceDetector, SdkProvidedResourceDetector},
trace::BatchConfig,
Resource,
};
lazy_static! {
pub static ref PB_PROGRESS_STYLE: ProgressStyle = ProgressStyle::with_template(
"{span_child_prefix}{bar:30} {wide_msg} [{elapsed_precise}] {pos:>7}/{len:7}"
)
.expect("invalid progress template");
pub static ref PB_SPINNER_STYLE: ProgressStyle = ProgressStyle::with_template(
"{span_child_prefix}{spinner} {wide_msg} [{elapsed_precise}] {pos:>7}/{len:7}"
)
.expect("invalid progress template");
}
// using a macro_rule here because of the complex return type
macro_rules! init_base_subscriber {
($level: expr) => {{
let indicatif_layer = IndicatifLayer::new().with_progress_style(PB_SPINNER_STYLE.clone());
// Set up the tracing subscriber.
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::Layer::new()
.with_writer(indicatif_layer.get_stderr_writer())
.compact()
.with_filter(
EnvFilter::builder()
.with_default_directive($level.into())
.from_env()
.expect("invalid RUST_LOG"),
),
)
.with(indicatif_layer.with_filter(
// only show progress for spans with indicatif.pb_show field being set
IndicatifFilter::new(false),
))
}};
}
pub fn init(level: Level) -> Result<(), tracing_subscriber::util::TryInitError> {
init_base_subscriber!(level).try_init()
}
#[cfg(feature = "otlp")]
pub fn init_with_otlp(
level: Level,
service_name: &'static str,
) -> Result<(), tracing_subscriber::util::TryInitError> {
let subscriber = init_base_subscriber!(level);
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(opentelemetry_otlp::new_exporter().tonic())
.with_batch_config(BatchConfig::default())
.with_trace_config(opentelemetry_sdk::trace::config().with_resource({
// use SdkProvidedResourceDetector.detect to detect resources,
// but replace the default service name with our default.
// https://github.com/open-telemetry/opentelemetry-rust/issues/1298
let resources = SdkProvidedResourceDetector.detect(std::time::Duration::from_secs(0));
// SdkProvidedResourceDetector currently always sets
// `service.name`, but we don't like its default.
if resources.get("service.name".into()).unwrap() == "unknown_service".into() {
resources.merge(&Resource::new([KeyValue::new(
"service.name",
service_name,
)]))
} else {
resources
}
}))
.install_batch(opentelemetry_sdk::runtime::Tokio)
.expect("Failed to install tokio runtime");
// Create a tracing layer with the configured tracer
let layer = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(Some(layer)).try_init()
}