8000 feat(health): wire final provisioning reports through libazureinit-kvp by peytonr18 · Pull Request #363 · Azure/azure-init · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion libazureinit-kvp/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ fn validate_report_quoting(value: &str) -> Result<(), DecodeError> {

impl ProvisioningReport {
/// Encodes the report for storage.
pub(crate) fn encode(&self) -> String {
pub fn encode(&self) -> String {
let mut data = Vec::with_capacity(7 + self.extra.len());

data.push(format!("result={}", self.result));
Expand Down
3 changes: 1 addition & 2 deletions libazureinit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "MIT"
description = "A common library for provisioning Linux VMs on Azure."

[dependencies]
libazureinit-kvp = { path = "../libazureinit-kvp", version = "0.1.0" }
reqwest = { version = "0.13.1", default-features = false, features = ["blocking", "json"] }
serde = {version = "1.0.163", features = ["derive"]}
thiserror = "2.0.3"
Expand All @@ -27,8 +28,6 @@ regex = "1"
lazy_static = "1.4"
figment = { version = "0.10", features = ["toml"] }
uuid = { version = "1.3", features = ["v4"] }
chrono = { version = "0.4", features = ["serde"] }
csv = "1"
zeroize = "1.8"

[dev-dependencies]
Expand Down
45 changes: 20 additions & 25 deletions libazureinit/src/error.rs
8000
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::health::encode_report;
use libazureinit_kvp::{ProvisioningReport, ReportPpsType};
use std::collections::HashMap;

/// Set of error codes that can be used by libazureinit.
Expand Down Expand Up @@ -156,27 +156,22 @@ impl Error {
map
}

/// Formats the error and its context as a pipe-delimited key-value string suitable for health endpoint reporting.
/// Constructs a failure report with the error's reason and supporting data.
///
/// Includes the result, reason, agent, supporting data, and standard fields such as
/// `vm_id`, `timestamp`, and documentation URL.
pub fn as_encoded_report(&self, vm_id: &str) -> String {
let agent = format!("Azure-Init/{}", env!("CARGO_PKG_VERSION"));
let timestamp = chrono::Utc::now();

let mut data = vec![
"result=error".to_string(),
format!("reason={}", self.reason()),
format!("agent={agent}"),
];
for (k, v) in self.supporting_data() {
data.push(format!("{k}={v}"));
/// Construct once and reuse for KVP and HTTP delivery to preserve the same
/// timestamp and context in both transports.
pub fn as_provisioning_report(&self, vm_id: &str) -> ProvisioningReport {
let mut report = ProvisioningReport::failure(
format!("Azure-Init/{}", env!("CARGO_PKG_VERSION")),
vm_id,
self.reason(),
ReportPpsType::None,
)
.with_documentation_url(Self::DOCUMENTATION_URL);
for (key, value) in self.supporting_data() {
report = report.with_extra(key, value);
}
data.push("pps_type=None".to_string());
data.push(format!("vm_id={vm_id}"));
data.push(format!("timestamp={}", timestamp.to_rfc3339()));
data.push(format!("documentation_url={}", Self::DOCUMENTATION_URL));
encode_report(&data)
report
}
}

Expand All @@ -185,12 +180,12 @@ mod tests {
use super::*;

#[test]
fn test_load_sshd_config_failure_as_encoded_report() {
fn test_load_sshd_config_failure_as_provisioning_report() {
let vm_id = "00000000-0000-0000-0000-000000000000";
let err = Error::LoadSshdConfig {
details: "bad config".to_string(),
};
let encoded = err.as_encoded_report(vm_id);
let encoded = err.as_provisioning_report(vm_id).encode();
assert!(encoded.contains("reason=failed to load sshd config"));
assert!(encoded.contains("details=bad config"));
assert!(encoded.contains(&format!("vm_id={}", vm_id)));
Expand All @@ -210,19 +205,19 @@ mod tests {
endpoint: "http://example.com".to_string(),
status: reqwest::StatusCode::NOT_FOUND,
};
let encoded = err.as_encoded_report(vm_id);
let encoded = err.as_provisioning_report(vm_id).encode();
assert!(encoded.contains("endpoint=http://example.com"));
assert!(encoded.contains("status=404"));
assert!(encoded.contains(&format!("vm_id={}", vm_id)));
}

#[test]
fn test_as_encoded_report_for_unhandled_error() {
fn test_as_provisioning_report_for_unhandled_error() {
let vm_id = "00000000-0000-0000-0000-000000000000";
let err = Error::UnhandledError {
details: "test_unhandled_exception".to_string(),
};
let encoded = err.as_encoded_report(vm_id);
let encoded = err.as_provisioning_report(vm_id).encode();

assert!(encoded.contains("reason=unhandled error"));
assert!(encoded.contains("error=unhandled error"));
Expand Down
2 changes: 1 addition & 1 deletion libazureinit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub mod config;
pub use config::{HostnameProvisioner, PasswordProvisioner, UserProvisioner};
pub mod error;
pub mod health;
pub(crate) mod http;
pub mod imds;
pub mod media;
pub mod wireserver;

mod provision;
pub use provision::{
Expand Down
Loading
Loading
0