-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
417 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ jobs: | |
|
||
- name: Collect coverage data (including doctests) | ||
run: | | ||
cargo llvm-cov --lcov --output-path lcov.info | ||
cargo llvm-cov --lcov --output-path lcov.info --ignore-filename-regex 'init' | ||
- name: Upload coverage reports to Codecov | ||
uses: codecov/[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,4 +92,6 @@ test/ | |
node_modules/ | ||
dist/ | ||
npm-debug.log | ||
yarn-error.log | ||
yarn-error.log | ||
|
||
trace.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
|
||
[workspace] | ||
resolver = '2' | ||
members = [ | ||
'apps/back', | ||
'libs/markdown_header', | ||
] | ||
members = ['apps/back', 'libs/markdown_header', 'libs/tool_tracing'] | ||
|
||
[profile.release] | ||
lto = true | ||
|
||
[patch.crates-io] | ||
tokio-macros = { git = "https://github.com/tokio-rs/tokio", branch = "master" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,3 @@ pub mod doc_header; | |
pub mod doc_sidebar; | ||
pub mod folder_struct; | ||
pub mod page_database; | ||
pub mod project_db; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "tool_tracing" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
serde = "1.0" | ||
serde_repr = "0.1" | ||
tracing = "0.1" | ||
tracing-subscriber = { version = "0.3", features = [ | ||
"registry", | ||
"env-filter", | ||
"time", | ||
] } | ||
time = "0.3" | ||
tracing-bunyan-formatter = "0.3" | ||
opentelemetry = "0.23" | ||
opentelemetry_sdk = { version = "0.23", features = ["rt-tokio"] } | ||
opentelemetry-otlp = { version = "0.16" } | ||
tracing-opentelemetry = "0.24" | ||
serde_json = "1.0" | ||
tokio = { version = "1.39.2", features = ["rt-multi-thread", "macros"] } | ||
|
||
[lints.rust] | ||
unexpected_cfgs = { level = "warn", check-cfg = [ | ||
'cfg(coverage,coverage_nightly)', | ||
] } | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "tool_tracing", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"projectType": "library", | ||
"sourceRoot": "libs/tool_tracing/src", | ||
"targets": { | ||
"build": { | ||
"executor": "@monodon/rust:check", | ||
"outputs": ["{options.target-dir}"], | ||
"options": { | ||
"target-dir": "dist/target/tool_tracing" | ||
} | ||
}, | ||
"test": { | ||
"cache": true, | ||
"executor": "@monodon/rust:test", | ||
"outputs": ["{options.target-dir}"], | ||
"options": { | ||
"target-dir": "dist/target/tool_tracing" | ||
}, | ||
"configurations": { | ||
"production": { | ||
"release": true | ||
} | ||
} | ||
}, | ||
"lint": { | ||
"cache": true, | ||
"executor": "@monodon/rust:lint", | ||
"outputs": ["{options.target-dir}"], | ||
"options": { | ||
"target-dir": "dist/target/tool_tracing" | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#![cfg_attr(coverage_nightly, feature(coverage_attribute))] | ||
use super::tracing_kind::{Tracing, TracingKind}; | ||
use opentelemetry::KeyValue; | ||
use opentelemetry_otlp::WithExportConfig; | ||
use opentelemetry_sdk::{runtime, trace, Resource}; | ||
use std::{fs::File, sync::Arc, vec}; | ||
use time::format_description; | ||
use tracing::level_filters::LevelFilter; | ||
use tracing::subscriber; | ||
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; | ||
use tracing_opentelemetry::OpenTelemetryLayer; | ||
use tracing_subscriber::fmt::time::UtcTime; | ||
use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt; | ||
use tracing_subscriber::{fmt, EnvFilter, Layer, Registry}; | ||
|
||
#[cfg_attr(coverage_nightly, coverage(off))] | ||
pub fn init_tracing(tracing_config: Vec<Tracing>, name: String) { | ||
let mut layers = vec![]; | ||
for config in tracing_config { | ||
match config.kind { | ||
TracingKind::File => { | ||
let file = File::options() | ||
.create(true) | ||
.truncate(false) | ||
.append(true) | ||
.open("trace.log") | ||
.expect("Failed to create trace.log"); | ||
let formating_layer = | ||
BunyanFormattingLayer::new(name.clone(), Arc::new(file)).boxed(); | ||
layers.push(JsonStorageLayer.boxed()); | ||
layers.push(formating_layer); | ||
} | ||
TracingKind::Console => { | ||
let time_format = format_description::parse("[hour]:[minute]:[second]") | ||
.expect("format string should be valid!"); | ||
let timer = UtcTime::new(time_format); | ||
let env_filter = EnvFilter::builder() | ||
.with_default_directive(LevelFilter::from(config.level).into()) | ||
.from_env() | ||
.unwrap() | ||
.add_directive("serenity=error".parse().unwrap()); | ||
let terminal_out = fmt::layer() | ||
.with_thread_names(true) | ||
.with_timer(timer) | ||
.with_target(false) | ||
.with_filter(env_filter) | ||
.boxed(); | ||
layers.push(terminal_out); | ||
} | ||
TracingKind::Otel => { | ||
let endpoint = match config.additional.get("endpoint") { | ||
Some(endpoint) => endpoint.to_string(), | ||
None => "http://localhost:4317".to_string(), | ||
}; | ||
let pod_name = | ||
std::env::var("POD_NAME").unwrap_or_else(|_| "not_a_pod".to_string()); | ||
let telemetry = opentelemetry_otlp::new_pipeline() | ||
.tracing() | ||
.with_exporter( | ||
opentelemetry_otlp::new_exporter() | ||
.tonic() | ||
.with_endpoint(endpoint), | ||
) | ||
.with_trace_config(trace::config().with_resource(Resource::new(vec![ | ||
KeyValue::new("service.name", name.clone()), | ||
KeyValue::new("service.pod", pod_name.clone()), | ||
]))) | ||
.install_batch(runtime::Tokio) | ||
.expect("Failed to install opentelemetry"); | ||
let env_filter = EnvFilter::builder() | ||
.with_default_directive(LevelFilter::from(config.level).into()) | ||
.from_env() | ||
.unwrap(); | ||
let tele_layer = OpenTelemetryLayer::new(telemetry).with_filter(env_filter); | ||
layers.push(tele_layer.boxed()); | ||
} | ||
} | ||
} | ||
subscriber::set_global_default(Registry::default().with(layers)) | ||
.expect("setting default subscriber failed"); | ||
} | ||
|
||
#[cfg_attr(coverage_nightly, coverage(off))] | ||
pub fn stop_tracing(tracing_config: Vec<Tracing>, _name: String) { | ||
if tracing_config.iter().any(|x| x.kind == TracingKind::Otel) { | ||
opentelemetry::global::shutdown_tracer_provider(); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::level::VerboseLevel; | ||
|
||
use super::*; | ||
use std::fs::remove_file; | ||
|
||
#[tokio::test] | ||
async fn test_init_tracing() { | ||
let mut tracing_config = vec![ | ||
Tracing { | ||
kind: TracingKind::File, | ||
level: VerboseLevel::DEBUG, | ||
additional: Default::default(), | ||
name: "test1".to_string(), | ||
}, | ||
Tracing { | ||
kind: TracingKind::Console, | ||
level: VerboseLevel::INFO, | ||
additional: Default::default(), | ||
name: "test2".to_string(), | ||
}, | ||
Tracing { | ||
kind: TracingKind::Otel, | ||
level: VerboseLevel::DEBUG, | ||
additional: Default::default(), | ||
name: "test3".to_string(), | ||
}, | ||
]; | ||
tracing_config[2] | ||
.additional | ||
.insert("endpoint".to_string(), "http://localhost:4317".to_string()); | ||
init_tracing(tracing_config.clone(), "test".to_string()); | ||
tracing::info!("test part of test_init_tracing"); | ||
tracing::error!("test part of test_init_tracing"); | ||
tokio::spawn(async { | ||
stop_tracing(tracing_config, "test".to_string()); | ||
}); | ||
remove_file("trace.log").unwrap(); | ||
} | ||
} |
Oops, something went wrong.