Skip to content

Commit

Permalink
Merge pull request #123 from lquerel/prepare-public-api
Browse files Browse the repository at this point in the history
refactor(semconv): Add more unit tests, fix inconsistencies
  • Loading branch information
lquerel authored Apr 22, 2024
2 parents 30f7b46 + 88abdb7 commit 06383dc
Show file tree
Hide file tree
Showing 110 changed files with 1,853 additions and 9,836 deletions.
1,168 changes: 59 additions & 1,109 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ anyhow = "1.0.81"

# Features definition =========================================================
[features]
experimental = ["tantivy", "weaver_schema", "weaver_template"]
experimental = []

# Crate definitions ===========================================================
[[bin]]
Expand All @@ -51,20 +51,14 @@ name = "weaver"
# local crates dependencies
weaver_common = { path = "crates/weaver_common" }
weaver_resolver = { path = "crates/weaver_resolver" }
weaver_template = { path = "crates/weaver_template", optional = true }
weaver_semconv = { path = "crates/weaver_semconv" }
weaver_resolved_schema = { path = "crates/weaver_resolved_schema" }
weaver_semconv_gen = { path = "crates/weaver_semconv_gen" }
weaver_schema = { path = "crates/weaver_schema", optional = true }
weaver_cache = { path = "crates/weaver_cache" }
weaver_forge = { path = "crates/weaver_forge" }
weaver_checker = { path = "crates/weaver_checker" }

clap = { version = "4.5.4", features = ["derive"] }
crossterm = "0.27.0"
ratatui = "0.26.1"
tui-textarea = "0.4.0"
tantivy = { version = "0.22.0", optional = true }

# workspace dependencies
serde.workspace = true
Expand Down
23 changes: 20 additions & 3 deletions crates/weaver_checker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

//! This crate integrates a general purpose policy engine with the Weaver
//! project. The project `regorus` is the policy engine used in this crate to
//! evaluate policies.
#![doc = include_str!("../README.md")]

use crate::violation::Violation;
use crate::Error::CompoundError;
Expand Down Expand Up @@ -48,6 +46,15 @@ pub enum Error {
error: String,
},

/// A policy violation error.
#[error("Policy violation: {violation}, provenance: {provenance}")]
PolicyViolation {
/// The provenance of the violation (URL or path).
provenance: String,
/// The violation.
violation: Violation,
},

/// A container for multiple errors.
#[error("{:?}", Error::format_errors(.0))]
CompoundError(Vec<Error>),
Expand Down Expand Up @@ -76,6 +83,16 @@ impl Error {
}
}

/// Handles a list of errors and returns a compound error if the list is not
/// empty or () if the list is empty.
pub fn handle_errors(errors: Vec<Error>) -> Result<(), Error> {
if errors.is_empty() {
Ok(())
} else {
Err(CompoundError(errors))
}
}

/// The policy engine.
#[derive(Clone, Default)]
pub struct Engine {
Expand Down
34 changes: 34 additions & 0 deletions crates/weaver_common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ pub trait WeaverError {
/// A trait for types that can cleanly exit the application if an error
/// is encountered.
pub trait ExitIfError<T, E> {
/// Processes the `Result` and panics if it is an `Err`.
/// If `Ok`, the contained value is returned.
///
/// # Arguments
/// * `self` - The `Result` to process.
/// * `logger` - An object implementing the `Logger` trait used to log any
/// errors.
///
/// # Returns
/// The contained value if the result is `Ok`.
/// Panics if the result is `Err`.
fn panic_if_error(self, logger: impl Logger) -> T;

/// Processes the `Result` and exits the application if it is an `Err`.
/// If `Ok`, the contained value is returned.
///
Expand Down Expand Up @@ -55,6 +68,27 @@ pub trait ExitIfError<T, E> {
/// Provides default implementations of the `ExitIfError` trait for any
/// `Result<T, E>` where `E` implements `WeaverError`.
impl<T, E: WeaverError> ExitIfError<T, E> for Result<T, E> {
/// Processes the `Result` and panics if it is an `Err`.
/// If `Ok`, the contained value is returned.
///
/// # Arguments
/// * `self` - The `Result` to process.
/// * `logger` - An object implementing the `Logger` trait used to log any
/// errors.
///
/// # Returns
/// The contained value if the result is `Ok`.
/// Panics if the result is `Err`.
fn panic_if_error(self, logger: impl Logger) -> T {
match self {
Ok(value) => value,
Err(e) => {
e.errors().iter().for_each(|msg| logger.error(msg));
panic!("One or several errors occurred (see above)");
}
}
}

/// Processes the `Result` and exits the application if it is an `Err`.
/// If `Ok`, the contained value is returned.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/weaver_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

//! A generic logger that can be used to log messages to the console.
#![doc = include_str!("../README.md")]

pub mod error;
pub mod quiet;
Expand Down
2 changes: 1 addition & 1 deletion crates/weaver_forge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ The following filters are available:
in the `acronyms` section of the `weaver.yaml` configuration file.
- `split_ids`: Splits a string by '.' creating a list of nested ids.
- `flatten`: Converts a List of Lists into a single list with all elements.
e.g. [[a,b],[c]] => [a,b,c]
e.g. \[\[a,b\],\[c\]\] => \[a,b,c\]


> Note 1: This project uses the [convert_case](https://crates.io/crates/convert_case)
Expand Down
8 changes: 3 additions & 5 deletions crates/weaver_forge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

//! This crate extends the MiniJinja template engine to add helper functions
//! and filters for working with semantic convention registries and telemetry
//! schemas.
#![doc = include_str!("../README.md")]

use std::fmt::{Debug, Display, Formatter};
use std::fs;
Expand Down Expand Up @@ -470,7 +468,7 @@ mod tests {
use weaver_common::TestLogger;
use weaver_diff::diff_output;
use weaver_resolver::SchemaResolver;
use weaver_semconv::SemConvRegistry;
use weaver_semconv::registry::SemConvRegistry;

use crate::debug::print_dedup_errors;
use crate::filter::Filter;
Expand Down Expand Up @@ -614,7 +612,7 @@ mod tests {
});

let registry_id = "default";
let mut registry = SemConvRegistry::try_from_path(registry_id, "data/*.yaml")
let mut registry = SemConvRegistry::try_from_path_pattern(registry_id, "data/*.yaml")
.expect("Failed to load registry");
let schema = SchemaResolver::resolve_semantic_convention_registry(&mut registry)
.expect("Failed to resolve registry");
Expand Down
6 changes: 0 additions & 6 deletions crates/weaver_resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ workspace = true
weaver_common = { path = "../weaver_common" }
weaver_diff = { path = "../weaver_diff" }
weaver_semconv = { path = "../weaver_semconv" }
weaver_schema = { path = "../weaver_schema" }
weaver_version = { path = "../weaver_version" }
weaver_cache = { path = "../weaver_cache" }
weaver_resolved_schema = { path = "../weaver_resolved_schema" }
weaver_checker = { path = "../weaver_checker" }

regex.workspace = true
thiserror.workspace = true
rayon.workspace = true
serde.workspace = true
serde_json.workspace = true
walkdir.workspace = true

url = "2.5.0"

[dev-dependencies]
glob = "0.3.1"
2 changes: 0 additions & 2 deletions crates/weaver_resolver/allowed-external-types.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ allowed_external_types = [
"weaver_semconv::*",
"weaver_common::*",
"weaver_cache::Cache",
"weaver_schema::*",
"weaver_version::*",
"weaver_checker::*"
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,60 @@
"name": "server.port",
"type": "string",
"brief": "Server port",
"examples": [
"80",
"443"
],
"requirement_level": "recommended"
},
{
"name": "network.protocol.name",
"type": "string",
"brief": "Network protocol name",
"examples": [
"http",
"spdy"
],
"requirement_level": "recommended"
},
{
"name": "network.protocol.version",
"type": "string",
"brief": "Network protocol version",
"examples": [
"1.0",
"2.0"
],
"requirement_level": "recommended"
},
{
"name": "network.type",
"type": "string",
"brief": "Network type",
"examples": [
"ipv4",
"ipv6"
],
"requirement_level": "recommended"
},
{
"name": "server.port",
"type": "string",
"brief": "Server port",
"requirement_level": "opt_in"
},
{
"name": "network.protocol.name",
"type": "string",
"brief": "Network protocol name",
"examples": [
"http",
"spdy"
"80",
"443"
],
"requirement_level": "recommended"
"requirement_level": "opt_in"
},
{
"name": "network.protocol.version",
"type": "string",
"brief": "Network protocol version",
"examples": [
"1.0",
"2.0"
],
"requirement_level": "opt_in"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
"type": "attribute_group",
"brief": "Top level group",
"attributes": [
1,
3,
4,
5,
6
5
],
"lineage": {
"source_file": "data/registry-test-lineage-0/registry/groups.yaml",
Expand All @@ -42,6 +42,7 @@
"source_group": "registry.network",
"inherited_fields": [
"brief",
"examples",
"note"
],
"locally_overridden_fields": [
Expand All @@ -52,6 +53,7 @@
"source_group": "registry.network",
"inherited_fields": [
"brief",
"examples",
"note",
"requirement_level"
]
Expand All @@ -60,6 +62,7 @@
"source_group": "registry.server",
"inherited_fields": [
"brief",
"examples",
"note"
],
"locally_overridden_fields": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ groups:
- id: server.port
type: string
brief: Server port
examples: [ '80', '443' ]

- id: registry.network
type: attribute_group
Expand All @@ -14,12 +15,15 @@ groups:
- id: network.protocol.name
type: string
brief: Network protocol name
examples: [ 'http', 'spdy' ]
- id: network.protocol.version
type: string
brief: Network protocol version
examples: [ '1.0', '2.0' ]
- id: network.type
type: string
brief: Network type
examples: [ 'ipv4', 'ipv6' ]

- id: top.level.group
type: attribute_group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"name": "server.port",
"type": "string",
"brief": "Server port",
"examples": [
"8080"
],
"requirement_level": "recommended"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"source_group": "intermediate.level",
"inherited_fields": [
"brief",
"examples",
"note",
"requirement_level"
]
Expand All @@ -36,6 +37,7 @@
"source_group": "base.level",
"inherited_fields": [
"brief",
"examples",
"note",
"requirement_level"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ groups:
attributes:
- id: server.port
type: string
brief: Server port
brief: Server port
examples: ['8080']
Loading

0 comments on commit 06383dc

Please sign in to comment.