Skip to content

Commit

Permalink
feat: creation libs tool_tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
batleforc committed Aug 1, 2024
1 parent 1405824 commit 7e4900f
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ test/
node_modules/
dist/
npm-debug.log
yarn-error.log
yarn-error.log

trace.log
8 changes: 4 additions & 4 deletions Cargo.toml
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" }
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ cargo llvm-cov --workspace --open
- [Git](https://git-scm.com/downloads) => Version control (obviously)
- [Cocogitto](https://docs.cocogitto.io/) => Git hooks and conventional commit

### Create new rust lib

```bash
nx generate @monodon/rust:library <lib-name>
```

## Nx - Front

### Start the application
Expand Down
1 change: 0 additions & 1 deletion apps/back/src/markdown/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ pub mod doc_header;
pub mod doc_sidebar;
pub mod folder_struct;
pub mod page_database;
pub mod project_db;
1 change: 0 additions & 1 deletion apps/back/src/markdown/project_db.rs

This file was deleted.

28 changes: 28 additions & 0 deletions libs/tool_tracing/Cargo.toml
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
37 changes: 37 additions & 0 deletions libs/tool_tracing/project.json
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": []
}
130 changes: 130 additions & 0 deletions libs/tool_tracing/src/init.rs
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();
}
}
Loading

0 comments on commit 7e4900f

Please sign in to comment.