Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JSON Schema for both Resolved Telemetry Schema and Resolved Registry #187

Merged
merged 10 commits into from
Jun 3, 2024
Merged
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 @@
.expect("Failed to lock messages")
.push(LogMessage::Log(message.to_owned()));
}

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

Check warning on line 156 in crates/weaver_common/src/in_memory.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/in_memory.rs#L156

Added line #L156 was not covered by tests
// 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 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 @@

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

/// Mute all the messages except for the warnings and errors.
fn mute(&self);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduced this new function to fix #184 and to simplify testing.

}

/// A generic logger that can be used to log messages to the console.
Expand All @@ -55,6 +58,15 @@
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 @@
ConsoleLogger {
logger: Arc::new(Mutex::new(paris::Logger::new())),
debug_level,
mute: Arc::new(AtomicBool::new(false)),

Check warning on line 79 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L79

Added line #L79 was not covered by tests
}
}
}

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) {

Check warning on line 87 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L87

Added line #L87 was not covered by tests
_ = self
.logger
.lock()
Expand All @@ -82,6 +95,10 @@

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

Check warning on line 98 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L98

Added line #L98 was not covered by tests
return;
}

_ = self
.logger
.lock()
Expand Down Expand Up @@ -109,6 +126,10 @@

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

Check warning on line 129 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L129

Added line #L129 was not covered by tests
return;
}

_ = self
.logger
.lock()
Expand All @@ -118,6 +139,10 @@

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

Check warning on line 142 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L142

Added line #L142 was not covered by tests
return;
}

_ = self
.logger
.lock()
Expand All @@ -127,6 +152,10 @@

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

Check warning on line 155 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L155

Added line #L155 was not covered by tests
return;
}

_ = self
.logger
.lock()
Expand All @@ -136,11 +165,19 @@

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

Check warning on line 168 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L168

Added line #L168 was not covered by tests
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;

Check warning on line 178 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L177-L178

Added lines #L177 - L178 were not covered by tests
}

_ = self
.logger
.lock()
Expand All @@ -151,6 +188,10 @@

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

Check warning on line 191 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L191

Added line #L191 was not covered by tests
return;
}

_ = self
.logger
.lock()
Expand All @@ -160,18 +201,34 @@

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

Check warning on line 205 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L204-L205

Added lines #L204 - L205 were not covered by tests
}

_ = 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) {

Check warning on line 214 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L214

Added line #L214 was not covered by tests
return;
}

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

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

Check warning on line 226 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L226

Added line #L226 was not covered by tests
// 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);

Check warning on line 230 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L230

Added line #L230 was not covered by tests
}
}

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

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

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

Check warning on line 288 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L288

Added line #L288 was not covered by tests
}

/// A logger that can be used in unit or integration tests.
Expand All @@ -251,13 +311,13 @@
/// 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)

Check warning on line 314 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L314

Added line #L314 was not covered by tests
}

/// 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 @@
.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);

Check warning on line 344 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L344

Added line #L344 was not covered by tests
}

/// Logs an error message.
Expand All @@ -293,9 +351,7 @@
.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 @@
.expect("Failed to lock logger")
.log(message);
}

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

Check warning on line 424 in crates/weaver_common/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/lib.rs#L424

Added line #L424 was not covered by tests
// 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 @@

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

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

Check warning on line 83 in crates/weaver_common/src/quiet.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/quiet.rs#L83

Added line #L83 was not covered by tests
// 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
Loading