Skip to content

Commit

Permalink
init tracing
Browse files Browse the repository at this point in the history
Signed-off-by: Ziy1-Tan <[email protected]>
  • Loading branch information
Ziy1-Tan committed Aug 11, 2024
1 parent 9f9cffe commit ca1da2d
Show file tree
Hide file tree
Showing 12 changed files with 1,044 additions and 178 deletions.
6 changes: 5 additions & 1 deletion vmm/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ async-trait = "0.1"
regex = "1.5.6"

tracing = "0.1.40"
tracing-subscriber = {version = "0.3.18", features = ["registry", "fmt", "std", "env-filter"] }
tracing-opentelemetry = "0.21.0"
tracing-subscriber = { version = "0.3.18", features = ["json", "env-filter"] }

opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
opentelemetry-otlp = "0.13.0"

[build-dependencies]
ttrpc-codegen = "0.4"
1 change: 1 addition & 0 deletions vmm/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use containerd_sandbox::data::Io;
pub mod api;
pub mod mount;
pub mod storage;
pub mod tracer;

pub const KUASAR_STATE_DIR: &str = "/run/kuasar/state";

Expand Down
52 changes: 52 additions & 0 deletions vmm/common/src/tracer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2024 The Kuasar Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use std::str::FromStr;

use opentelemetry::sdk::trace::Tracer;
use opentelemetry::sdk::{trace, Resource};
use opentelemetry_otlp::WithExportConfig;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;

const DEFAULT_JAEGER_ENDPOINT: &str = "http://localhost:14268/api/traces";

pub fn create_otlp_tracer(
otlp_service_name: &str,
otlp_endpoint: Option<String>,
) -> anyhow::Result<Tracer> {
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(otlp_endpoint.unwrap_or(DEFAULT_JAEGER_ENDPOINT.to_string())),
)
.with_trace_config(trace::config().with_resource(Resource::new(vec![
opentelemetry::KeyValue::new("service.name", otlp_service_name.to_string()),
])))
.install_batch(opentelemetry::runtime::Tokio)?;

Ok(tracer)
}

pub fn create_logger_filter(level: &str) -> anyhow::Result<EnvFilter> {
let log_level = LevelFilter::from_str(&level)?;
let filter = EnvFilter::from_default_env()
.add_directive(format!("containerd_sandbox={:?}", log_level).parse()?)
.add_directive(format!("vmm_sandboxer={:?}", log_level).parse()?);
Ok(filter)
}
Loading

0 comments on commit ca1da2d

Please sign in to comment.