Skip to content

Commit

Permalink
Add names for silo, project and instance to instance metadata for sta…
Browse files Browse the repository at this point in the history
…tistics tracking
  • Loading branch information
bnaecker committed Jan 29, 2024
1 parent 582e98e commit 2f31f6d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
27 changes: 17 additions & 10 deletions nexus/src/app/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,17 +1148,24 @@ impl super::Nexus {

// Construct instance metadata used to track its statistics.
//
// This current means fetching the current silo ID, since we have all
// the other metadata already.
let silo_id = self
.current_silo_lookup(opctx)?
.lookup_for(authz::Action::Read)
.await?
.0
.id();
// This requires another fetch on the silo and project, to extract their
// IDs and names.
let (.., db_project) = self
.project_lookup(
opctx,
params::ProjectSelector {
project: NameOrId::Id(db_instance.project_id),
},
)?
.fetch()
.await?;
let (_, db_silo) = self.current_silo_lookup(opctx)?.fetch().await?;
let metadata = sled_agent_client::types::InstanceMetadata {
silo_id,
project_id: db_instance.project_id,
silo_id: db_silo.id(),
silo_name: db_silo.name().to_string(),
project_id: db_project.id(),
project_name: db_project.name().to_string(),
instance_name: db_instance.name().to_string(),
};

// Ask the sled agent to begin the state change. Then update the
Expand Down
14 changes: 13 additions & 1 deletion openapi/sled-agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -4754,18 +4754,30 @@
"description": "Metadata used to track statistics about an instance.",
"type": "object",
"properties": {
"instance_name": {
"type": "string"
},
"project_id": {
"type": "string",
"format": "uuid"
},
"project_name": {
"type": "string"
},
"silo_id": {
"type": "string",
"format": "uuid"
},
"silo_name": {
"type": "string"
}
},
"required": [
"instance_name",
"project_id",
"silo_id"
"project_name",
"silo_id",
"silo_name"
]
},
"InstanceMigrationSourceParams": {
Expand Down
53 changes: 42 additions & 11 deletions oximeter/instruments/src/kstat/virtual_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,16 @@ use kstat_types::*;
pub struct VirtualMachine {
/// The silo to which the instance belongs.
pub silo_id: Uuid,
/// The name of the silo to which the instance belongs.
pub silo_name: String,
/// The project to which the instance belongs.
pub project_id: Uuid,
/// The name of the project to which the instance belongs.
pub project_name: String,
/// The ID of the instance.
pub instance_id: Uuid,
/// The name of the instance.
pub instance_name: String,

// This field is not published as part of the target field definitions. It
// is needed because the hypervisor currently creates kstats for each vCPU,
Expand All @@ -114,18 +120,35 @@ impl Target for VirtualMachine {
}

fn field_names(&self) -> &'static [&'static str] {
&["silo_id", "project_id", "instance_id"]
&[
"silo_id",
"silo_name",
"project_id",
"project_name",
"instance_id",
"instance_name",
]
}

fn field_types(&self) -> Vec<FieldType> {
vec![FieldType::Uuid, FieldType::Uuid, FieldType::Uuid]
vec![
FieldType::Uuid,
FieldType::String,
FieldType::Uuid,
FieldType::String,
FieldType::Uuid,
FieldType::String,
]
}

fn field_values(&self) -> Vec<FieldValue> {
vec![
self.silo_id.into(),
self.silo_name.clone().into(),
self.project_id.into(),
self.project_name.clone().into(),
self.instance_id.into(),
self.instance_name.clone().into(),
]
}
}
Expand Down Expand Up @@ -319,12 +342,17 @@ mod tests {
use oximeter::Datum;
use oximeter::FieldValue;

const TEST_VM: VirtualMachine = VirtualMachine {
silo_id: uuid::uuid!("6a4bd4b6-e9aa-44d1-b616-399d48baa173"),
project_id: uuid::uuid!("7b61df02-0794-4b37-93bc-89f03c7289ca"),
instance_id: uuid::uuid!("96d6ec78-543a-4188-830e-37e2a0eeff16"),
n_vcpus: 4,
};
fn test_virtual_machine() -> VirtualMachine {
VirtualMachine {
silo_id: uuid::uuid!("6a4bd4b6-e9aa-44d1-b616-399d48baa173"),
silo_name: String::from("silo-name"),
project_id: uuid::uuid!("7b61df02-0794-4b37-93bc-89f03c7289ca"),
project_name: String::from("project-name"),
instance_id: uuid::uuid!("96d6ec78-543a-4188-830e-37e2a0eeff16"),
instance_name: String::from("instance-name"),
n_vcpus: 4,
}
}

fn test_usage() -> VcpuUsage {
VcpuUsage {
Expand All @@ -337,7 +365,9 @@ mod tests {
#[test]
fn test_no_schema_changes() {
let mut set = SchemaSet::default();
assert!(set.insert_checked(&TEST_VM, &test_usage()).is_none());
assert!(set
.insert_checked(&test_virtual_machine(), &test_usage())
.is_none());
const PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/output/virtual-machine-schema.json",
Expand Down Expand Up @@ -394,8 +424,9 @@ mod tests {
fn test_produce_vcpu_usage() {
let (ks, data) = vcpu_state_kstats();
let kstats = [(Utc::now(), ks, data)];
let samples = produce_vcpu_usage(&TEST_VM, kstats.iter())
.expect("Should have produced samples");
let samples =
produce_vcpu_usage(&test_virtual_machine(), kstats.iter())
.expect("Should have produced samples");
assert_eq!(
samples.len(),
2,
Expand Down
17 changes: 16 additions & 1 deletion oximeter/instruments/tests/output/virtual-machine-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,31 @@
"field_type": "uuid",
"source": "target"
},
{
"name": "instance_name",
"field_type": "string",
"source": "target"
},
{
"name": "project_id",
"field_type": "uuid",
"source": "target"
},
{
"name": "project_name",
"field_type": "string",
"source": "target"
},
{
"name": "silo_id",
"field_type": "uuid",
"source": "target"
},
{
"name": "silo_name",
"field_type": "string",
"source": "target"
},
{
"name": "state",
"field_type": "string",
Expand All @@ -29,6 +44,6 @@
}
],
"datum_type": "cumulative_u64",
"created": "2024-01-25T20:08:38.763553812Z"
"created": "2024-01-29T19:51:22.103250716Z"
}
}
3 changes: 3 additions & 0 deletions sled-agent/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ pub struct InstanceHardware {
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
pub struct InstanceMetadata {
pub silo_id: Uuid,
pub silo_name: String,
pub project_id: Uuid,
pub project_name: String,
pub instance_name: String,
}

/// The body of a request to ensure that a instance and VMM are known to a sled
Expand Down

0 comments on commit 2f31f6d

Please sign in to comment.