forked from open-telemetry/weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry_repo.rs
More file actions
200 lines (180 loc) · 7.13 KB
/
registry_repo.rs
File metadata and controls
200 lines (180 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-License-Identifier: Apache-2.0
//! A Semantic Convention Repository abstraction for OTel Weaver.
use std::default::Default;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::manifest::RegistryManifest;
use crate::Error;
use weaver_common::vdir::{VirtualDirectory, VirtualDirectoryPath};
use weaver_common::{get_path_type, log_info};
/// The name of the registry manifest file.
pub const REGISTRY_MANIFEST: &str = "registry_manifest.yaml";
/// A semantic convention registry repository that can be:
/// - A definition repository, which is one of:
/// - A simple wrapper around a local directory
/// - Initialized from a Git repository
/// - Initialized from a Git archive
/// - A published repository, which is a manifest file
/// that denotes where to find aspects of the registry.
#[derive(Default, Debug, Clone)]
pub struct RegistryRepo {
// A unique identifier for the registry (e.g. main, baseline, etc.)
id: Arc<str>,
// A virtual directory containing the registry.
registry: VirtualDirectory,
// The registry manifest definition.
manifest: Option<RegistryManifest>,
}
impl RegistryRepo {
/// Creates a new `RegistryRepo` from a `RegistryPath` object that
/// specifies the location of the registry.
pub fn try_new(
registry_id_if_no_manifest: &str,
registry_path: &VirtualDirectoryPath,
) -> Result<Self, Error> {
let mut registry_repo = Self {
id: Arc::from(registry_id_if_no_manifest),
registry: VirtualDirectory::try_new(registry_path)
.map_err(Error::VirtualDirectoryError)?,
manifest: None,
};
if let Some(manifest) = registry_repo.manifest_path() {
let registry_manifest = RegistryManifest::try_from_file(manifest)?;
registry_repo.id = Arc::from(registry_manifest.name.as_str());
registry_repo.manifest = Some(registry_manifest);
}
Ok(registry_repo)
}
/// Returns the unique identifier for the registry.
#[must_use]
pub fn id(&self) -> Arc<str> {
self.id.clone()
}
/// Returns the local path to the semconv registry.
#[must_use]
pub fn path(&self) -> &Path {
self.registry.path()
}
/// Returns the registry path textual representation.
#[must_use]
pub fn registry_path_repr(&self) -> &str {
self.registry.vdir_path_str()
}
/// Returns the registry manifest specified in the registry repo.
#[must_use]
pub fn manifest(&self) -> Option<&RegistryManifest> {
self.manifest.as_ref()
}
/// Returns the resolved schema URL, if available in the manifest.
#[must_use]
pub fn resolved_schema_url(&self) -> Option<VirtualDirectoryPath> {
let manifest = self.manifest.as_ref()?;
let resolved_url: &str = manifest.resolved_schema_url.as_ref()?;
match get_path_type(resolved_url) {
weaver_common::PathType::RelativePath => {
let vdir_was_manifest_file = self.manifest_path()?.is_file();
Some(self.registry.vdir_path().map_sub_folder(|path| {
if vdir_was_manifest_file {
match Path::new(&path).parent() {
Some(parent) => format!("{}", parent.join(resolved_url).display()),
None => format!(""),
}
} else {
format!("{}", Path::new(&path).join(resolved_url).display())
}
}))
}
_ => resolved_url.try_into().ok(),
}
}
/// Returns the path to the `registry_manifest.yaml` file (if any).
#[must_use]
pub fn manifest_path(&self) -> Option<PathBuf> {
// First check to see if we're pointing at a manifest.
if self.registry.path().is_file() {
// The VirtualDirectory *is* the registry.
return Some(self.registry.path().to_path_buf());
}
let manifest_path = self.registry.path().join(REGISTRY_MANIFEST);
if manifest_path.exists() {
log_info(format!(
"Found registry manifest: {}",
manifest_path.display()
));
Some(manifest_path)
} else {
log_info(format!(
"No registry manifest found: {}",
manifest_path.display()
));
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use weaver_common::vdir::VirtualDirectoryPath;
fn count_yaml_files(repo_path: &Path) -> usize {
let count = walkdir::WalkDir::new(repo_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "yaml"))
.count();
count
}
#[test]
fn test_semconv_registry_local_repo() {
// A RegistryRepo created from a local folder.
let registry_path = VirtualDirectoryPath::LocalFolder {
path: "../../crates/weaver_codegen_test/semconv_registry".to_owned(),
};
let repo = RegistryRepo::try_new("main", ®istry_path).unwrap();
let repo_path = repo.path().to_path_buf();
assert!(repo_path.exists());
assert!(
count_yaml_files(&repo_path) > 0,
"There should be at least one `.yaml` file in the repo"
);
// Simulate a RegistryRepo going out of scope.
drop(repo);
// The local folder should not be deleted.
assert!(repo_path.exists());
}
#[test]
fn test_resolved_registry_path() {
// A RegistryRepo created from a local folder.
let registry_path = VirtualDirectoryPath::LocalFolder {
path: "tests/published_repository/resolved/1.0.0".to_owned(),
};
let repo =
RegistryRepo::try_new("main", ®istry_path).expect("Failed to load test repository.");
let Some(manifest) = repo.manifest() else {
panic!("Did not resolve manifest for repo: {repo:?}");
};
assert_eq!(manifest.name, "resolved");
let Some(resolved_path) = repo.resolved_schema_url() else {
panic!(
"Should find a resolved schema path from manfiest in {}",
repo.registry_path_repr()
);
};
assert_eq!(
"tests/published_repository/resolved/resolved_1.0.0.yaml",
format!("{resolved_path}")
);
// Now make sure a different repository with full URL works too.
let registry_path = VirtualDirectoryPath::LocalFolder {
path: "tests/published_repository/resolved/2.0.0".to_owned(),
};
let repo =
RegistryRepo::try_new("main", ®istry_path).expect("Failed to load test repository.");
let Some(resolved_path) = repo.resolved_schema_url() else {
panic!(
"Should find a resolved schema path from manfiest in {}",
repo.registry_path_repr()
);
};
assert_eq!("https://github.com/open-telemetry/weaver.git\\creates/weaver_semconv/tests/published_respository/resolved/resolved_2.0.0", format!("{resolved_path}"));
}
}