Skip to content

Commit

Permalink
Merge pull request #187 from lquerel/resolved-telemetry-schema
Browse files Browse the repository at this point in the history
Generate JSON Schema for both Resolved Telemetry Schema and Resolved Registry
  • Loading branch information
lquerel authored Jun 3, 2024
2 parents 45a5338 + 9df9a42 commit 7c6e66b
Show file tree
Hide file tree
Showing 53 changed files with 821 additions and 434 deletions.
45 changes: 43 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ thiserror = "1.0.58"
ureq = "2.9.7"
regex = "1.10.3"
rayon = "1.10.0"
ordered-float = { version = "4.2.0", features = ["serde"] }
ordered-float = { version = "4.2.0", features = ["serde", "schemars"] }
walkdir = "2.5.0"
anyhow = "1.0.83"
itertools = "0.12.1"
globset = { version = "0.4.14", features = ["serde1"] }
miette = { version = "7.2.0", features = ["fancy", "serde"] }
include_dir = "0.7.3"
tempdir = "0.3.7"
schemars = "0.8.21"

# Features definition =========================================================
[features]
Expand Down Expand Up @@ -83,6 +84,7 @@ walkdir.workspace = true
include_dir.workspace = true
thiserror.workspace = true
miette.workspace = true
schemars.workspace = true

rayon = "1.10.0"

Expand Down
4 changes: 2 additions & 2 deletions crates/weaver_codegen_test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use weaver_cache::Cache;
use weaver_common::in_memory::LogMessage;
use weaver_common::{in_memory, Logger};
use weaver_forge::file_loader::FileSystemFileLoader;
use weaver_forge::registry::TemplateRegistry;
use weaver_forge::registry::ResolvedRegistry;
use weaver_forge::{OutputDirective, TemplateEngine};
use weaver_resolver::SchemaResolver;
use weaver_semconv::path::RegistryPath;
Expand Down Expand Up @@ -51,7 +51,7 @@ fn main() {
let loader = FileSystemFileLoader::try_new(TEMPLATES_PATH.into(), TARGET)
.unwrap_or_else(|e| process_error(&logger, e));
let engine = TemplateEngine::try_new(loader).unwrap_or_else(|e| process_error(&logger, e));
let template_registry = TemplateRegistry::try_from_resolved_registry(
let template_registry = ResolvedRegistry::try_from_resolved_registry(
schema
.registry(REGISTRY_ID)
.expect("Failed to get the registry from the resolved schema"),
Expand Down
5 changes: 5 additions & 0 deletions crates/weaver_common/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,9 @@ impl crate::Logger for Logger {
.expect("Failed to lock messages")
.push(LogMessage::Log(message.to_owned()));
}

/// Mute all the messages except for the warnings and errors.
fn mute(&self) {
// We do not mute the logger in this implementation.
}
}
81 changes: 71 additions & 10 deletions crates/weaver_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod error;
pub mod in_memory;
pub mod quiet;

use std::sync::atomic::AtomicUsize;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

/// A trait that defines the interface of a logger.
Expand Down Expand Up @@ -47,6 +47,9 @@ pub trait Logger {

/// Logs a message without icon.
fn log(&self, message: &str);

/// Mute all the messages except for the warnings and errors.
fn mute(&self);
}

/// A generic logger that can be used to log messages to the console.
Expand All @@ -55,6 +58,15 @@ pub trait Logger {
pub struct ConsoleLogger {
logger: Arc<Mutex<paris::Logger<'static>>>,
debug_level: u8,
/// Mute all the messages except for the warnings and errors.
/// This flag is used to dynamically mute the logger.
///
/// Ordering logic:
/// - Ordering::Acquire in load: Ensures that when a thread reads the muted flag, it sees all
/// preceding writes to that flag by other threads.
/// - Ordering::Release in store: Ensures that when a thread sets the muted flag, the store
/// operation is visible to other threads that subsequently perform an acquire load.
mute: Arc<AtomicBool>,
}

impl ConsoleLogger {
Expand All @@ -64,14 +76,15 @@ impl ConsoleLogger {
ConsoleLogger {
logger: Arc::new(Mutex::new(paris::Logger::new())),
debug_level,
mute: Arc::new(AtomicBool::new(false)),
}
}
}

impl Logger for ConsoleLogger {
/// Logs an trace message (only with debug enabled).
fn trace(&self, message: &str) {
if self.debug_level > 0 {
if self.debug_level > 0 && !self.mute.load(Ordering::Acquire) {
_ = self
.logger
.lock()
Expand All @@ -82,6 +95,10 @@ impl Logger for ConsoleLogger {

/// Logs an info message.
fn info(&self, message: &str) {
if self.mute.load(Ordering::Acquire) {
return;
}

_ = self
.logger
.lock()
Expand Down Expand Up @@ -109,6 +126,10 @@ impl Logger for ConsoleLogger {

/// Logs a success message.
fn success(&self, message: &str) {
if self.mute.load(Ordering::Acquire) {
return;
}

_ = self
.logger
.lock()
Expand All @@ -118,6 +139,10 @@ impl Logger for ConsoleLogger {

/// Logs a newline.
fn newline(&self, count: usize) {
if self.mute.load(Ordering::Acquire) {
return;
}

_ = self
.logger
.lock()
Expand All @@ -127,6 +152,10 @@ impl Logger for ConsoleLogger {

/// Indents the logger.
fn indent(&self, count: usize) {
if self.mute.load(Ordering::Acquire) {
return;
}

_ = self
.logger
.lock()
Expand All @@ -136,11 +165,19 @@ impl Logger for ConsoleLogger {

/// Stops a loading message.
fn done(&self) {
if self.mute.load(Ordering::Acquire) {
return;
}

_ = self.logger.lock().expect("Failed to lock logger").done();
}

/// Adds a style to the logger.
fn add_style(&self, name: &str, styles: Vec<&'static str>) -> &Self {
if self.mute.load(Ordering::Acquire) {
return self;
}

_ = self
.logger
.lock()
Expand All @@ -151,6 +188,10 @@ impl Logger for ConsoleLogger {

/// Logs a loading message with a spinner.
fn loading(&self, message: &str) {
if self.mute.load(Ordering::Acquire) {
return;
}

_ = self
.logger
.lock()
Expand All @@ -160,18 +201,34 @@ impl Logger for ConsoleLogger {

/// Forces the logger to not print a newline for the next message.
fn same(&self) -> &Self {
if self.mute.load(Ordering::Acquire) {
return self;
}

_ = self.logger.lock().expect("Failed to lock logger").same();
self
}

/// Logs a message without icon.
fn log(&self, message: &str) {
if self.mute.load(Ordering::Acquire) {
return;
}

_ = self
.logger
.lock()
.expect("Failed to lock logger")
.log(message);
}

/// Mute all the messages except for the warnings and errors.
fn mute(&self) {
// Ordering::Release:
// Ensures that when a thread sets the muted flag, the store operation is visible to other
// threads that subsequently perform an acquire load.
self.mute.store(true, Ordering::Release);
}
}

/// A logger that does not log anything.
Expand Down Expand Up @@ -226,6 +283,9 @@ impl Logger for NullLogger {

/// Logs a message without icon.
fn log(&self, _: &str) {}

/// Mute all the messages except for the warnings and errors.
fn mute(&self) {}
}

/// A logger that can be used in unit or integration tests.
Expand All @@ -251,13 +311,13 @@ impl TestLogger {
/// Returns the number of warning messages logged.
#[must_use]
pub fn warn_count(&self) -> usize {
self.warn_count.load(std::sync::atomic::Ordering::Relaxed)
self.warn_count.load(Ordering::Relaxed)
}

/// Returns the number of error messages logged.
#[must_use]
pub fn error_count(&self) -> usize {
self.error_count.load(std::sync::atomic::Ordering::Relaxed)
self.error_count.load(Ordering::Relaxed)
}
}

Expand All @@ -281,9 +341,7 @@ impl Logger for TestLogger {
.lock()
.expect("Failed to lock logger")
.warn(message);
_ = self
.warn_count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
_ = self.warn_count.fetch_add(1, Ordering::Relaxed);
}

/// Logs an error message.
Expand All @@ -293,9 +351,7 @@ impl Logger for TestLogger {
.lock()
.expect("Failed to lock logger")
.error(message);
_ = self
.error_count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
_ = self.error_count.fetch_add(1, Ordering::Relaxed);
}

/// Logs a success message.
Expand Down Expand Up @@ -363,4 +419,9 @@ impl Logger for TestLogger {
.expect("Failed to lock logger")
.log(message);
}

/// Mute all the messages except for the warnings and errors.
fn mute(&self) {
// We do not need to mute the logger in the tests.
}
}
5 changes: 5 additions & 0 deletions crates/weaver_common/src/quiet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ impl Logger for QuietLogger {

/// Logs a message without icon.
fn log(&self, _message: &str) {}

/// Mute all the messages except for the warnings and errors.
fn mute(&self) {
// Do nothing
}
}
1 change: 1 addition & 0 deletions crates/weaver_forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ walkdir.workspace = true
globset.workspace = true
miette.workspace = true
include_dir.workspace = true
schemars.workspace = true

[dev-dependencies]
opentelemetry = { version = "0.22.0", features = ["trace", "metrics", "logs", "otel_unstable"] }
Expand Down
Loading

0 comments on commit 7c6e66b

Please sign in to comment.