about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-01-02T14·10+0200
committerclbot <clbot@tvl.fyi>2024-01-03T13·29+0000
commita82214b3ad00a402db222fa2df276a30c0a86c47 (patch)
tree42abeed868c50d84b9468bc64b2e6b680a09ffed
parent9abe735061a91422936775a8164953d21c5cc70a (diff)
fix(tvix/store): set service.name r/7315
While this can be influenced with `OTEL_SERVICE_NAME` or
`OTEL_RESOURCE_ATTRIBUTES`, "unknown_service" is an annoying name.

Let's pick tvix.store as a default.

Change-Id: I9b5c45703d9546775bb9266b29ab54daf7c54a9f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10524
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
-rw-r--r--tvix/store/src/bin/tvix-store.rs68
1 files changed, 44 insertions, 24 deletions
diff --git a/tvix/store/src/bin/tvix-store.rs b/tvix/store/src/bin/tvix-store.rs
index 7c3d2ec0c797..1a5c379b89af 100644
--- a/tvix/store/src/bin/tvix-store.rs
+++ b/tvix/store/src/bin/tvix-store.rs
@@ -2,17 +2,15 @@ use clap::Parser;
 use clap::Subcommand;
 
 use futures::future::try_join_all;
-use tonic::transport::Server;
-use tracing::info;
-use tracing::Level;
-
 use std::path::PathBuf;
 use std::sync::Arc;
 use tokio_listener::Listener;
 use tokio_listener::SystemOptions;
 use tokio_listener::UserOptions;
-
-use tracing_subscriber::prelude::*;
+use tonic::transport::Server;
+use tracing::info;
+use tracing::Level;
+use tracing_subscriber::fmt::writer::MakeWriterExt;
 use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
 
 use tvix_castore::proto::blob_service_server::BlobServiceServer;
@@ -29,6 +27,15 @@ use tvix_store::pathinfoservice::make_fs;
 #[cfg(feature = "fuse")]
 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")]
 use tvix_castore::fs::virtiofs::start_virtiofs_daemon;
 
@@ -177,24 +184,37 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
     // layer into the type of the registry.
     #[cfg(feature = "otlp")]
     {
-        let opentelemetry_layer = {
-            let otlp_exporter = opentelemetry_otlp::new_exporter().tonic();
-            // TODO: re-add once https://github.com/open-telemetry/opentelemetry-rust/pull/1252 is solved.
-            // let mut metadata = tonic::metadata::MetadataMap::new();
-            // metadata.insert("service.name", "tvix.store".parse()?);
-            // otlp_exporter.with_metadata(metadata),
-
-            let tracer = opentelemetry_otlp::new_pipeline()
-                .tracing()
-                .with_exporter(otlp_exporter)
-                .install_batch(opentelemetry_sdk::runtime::Tokio)?;
-
-            // Create a tracing layer with the configured tracer
-            tracing_opentelemetry::layer().with_tracer(tracer)
-        };
-
-        let subscriber = subscriber.with(opentelemetry_layer);
-        subscriber.try_init()?;
+        subscriber
+            .with({
+                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",
+                                "tvix.store",
+                            )]))
+                        } else {
+                            resources
+                        }
+                    }))
+                    .install_batch(opentelemetry_sdk::runtime::Tokio)?;
+
+                // Create a tracing layer with the configured tracer
+                tracing_opentelemetry::layer().with_tracer(tracer)
+            })
+            .try_init()?;
     }
 
     // Init the registry (when otlp is not enabled)