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

Add support of snapshot in vSphere virtualmachine metricset #40683

Merged
merged 14 commits into from
Sep 6, 2024

Conversation

ishleenk17
Copy link
Contributor

@ishleenk17 ishleenk17 commented Sep 3, 2024

Add support of adding snapshot details to the Virtualmachine object in Vsphere.
It caters to the snapshot and any childsnapshots as well.

Relates: https://github.com/elastic/obs-integration-team/issues/34

@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Sep 3, 2024
Copy link
Contributor

mergify bot commented Sep 3, 2024

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @ishleenk17? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

@ycombinator ycombinator added the Team:Obs-InfraObs Label for the Observability Infrastructure Monitoring team label Sep 4, 2024
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Sep 4, 2024
@ishleenk17
Copy link
Contributor Author

/test

@ishleenk17 ishleenk17 marked this pull request as ready for review September 4, 2024 12:09
@ishleenk17 ishleenk17 requested a review from a team as a code owner September 4, 2024 12:09
@niraj-elastic
Copy link
Contributor

@ishleenk17 please add entry in ascii.next file.

@niraj-elastic
Copy link
Contributor

Please update metricbeat/module/vsphere/virtualmachine/_meta/data.json as well.

@ishleenk17
Copy link
Contributor Author

Please update metricbeat/module/vsphere/virtualmachine/_meta/data.json as well.

There is no way to test this, due to unavailability of snapshots. Hence its not updated.
An option is to put the dummy data in the sample.

@niraj-elastic
Copy link
Contributor

Please update metricbeat/module/vsphere/virtualmachine/_meta/data.json as well.

There is no way to test this, due to unavailability of snapshots. Hence its not updated. An option is to put the dummy data in the sample.

I understand, But the main prepose of data.json is to give idea about event that is going to be produced. I think we should add dummy fields because it would capture structure of our event.

@shmsr
Copy link
Member

shmsr commented Sep 5, 2024

Please update metricbeat/module/vsphere/virtualmachine/_meta/data.json as well.

There is no way to test this, due to unavailability of snapshots. Hence its not updated. An option is to put the dummy data in the sample.

I understand, But the main prepose of data.json is to give idea about event that is going to be produced. I think we should add dummy fields because it would capture structure of our event.

I think it is possible to write a mock (see example) but given the strict timeline we can take it up in future to properly generate the data.json. For now, we can go with dummy.

@shmsr
Copy link
Member

shmsr commented Sep 5, 2024

diff --git a/metricbeat/module/vsphere/virtualmachine/data.go b/metricbeat/module/vsphere/virtualmachine/data.go
index 69d0bd1df5..bc29c1f8ef 100644
--- a/metricbeat/module/vsphere/virtualmachine/data.go
+++ b/metricbeat/module/vsphere/virtualmachine/data.go
@@ -72,9 +72,9 @@ func (m *MetricSet) mapEvent(data VMData) mapstr.M {
 	if len(data.DatastoreNames) > 0 {
 		event["datastore.names"] = data.DatastoreNames
 	}
-	if len(data.Snapshots) > 0 {
+	if n := len(data.Snapshots); n > 0 {
 		event["snapshot.info"] = data.Snapshots
-		event["snapshot.count"] = len(data.Snapshots)
+		event["snapshot.count"] = n
 	}
 
 	return event
diff --git a/metricbeat/module/vsphere/virtualmachine/virtualmachine.go b/metricbeat/module/vsphere/virtualmachine/virtualmachine.go
index 81052e1eed..071c4f6cfb 100644
--- a/metricbeat/module/vsphere/virtualmachine/virtualmachine.go
+++ b/metricbeat/module/vsphere/virtualmachine/virtualmachine.go
@@ -61,6 +61,10 @@ type VMData struct {
 }
 
 type VMSnapshotData struct {
+	ID          int32
+	State       string
+	Quiesced    bool
+	Age         time.Duration
 	Name        string
 	Description string
 	CreateTime  time.Time
@@ -286,9 +290,12 @@ func getHostSystem(ctx context.Context, c *vim25.Client, ref types.ManagedObject
 }
 
 func fetchSnapshots(snapshotTree []types.VirtualMachineSnapshotTree) []VMSnapshotData {
-	var snapshots []VMSnapshotData
+	snapshots := make([]VMSnapshotData, 0, len(snapshotTree))
 	for _, snapshot := range snapshotTree {
 		snapshots = append(snapshots, VMSnapshotData{
+			ID:          snapshot.Id,
+			State:       string(snapshot.State),
+			Quiesced:    snapshot.Quiesced,
 			Name:        snapshot.Name,
 			Description: snapshot.Description,
 			CreateTime:  snapshot.CreateTime,

Code added looks good. Just have a couple of things to suggest as you can see from the patch:

  • IMO, adding ID and State makes sense for Snapshot as ID is the unique identifier for the Snapshot and State gives the info if the snapshot is powered on/off or suspended. Quiesced seems also important but not sure how much:

Quiesced definition:

// Flag to indicate whether or not the snapshot was created with
// the "quiesce" option, ensuring a consistent state of the file system.
  • Also, I did some safe preallocation for the slice; fewer allocations now than before.

Copy link
Member

@shmsr shmsr left a comment

Choose a reason for hiding this comment

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

Left one comment to address. Everything else looks good. So, approving!

@ishleenk17
Copy link
Contributor Author

  • IMO, adding ID and State makes sense for Snapshot as ID is the unique identifier for the Snapshot and State gives the info if the snapshot is powered on/off or suspended. Quiesced seems also important but not sure how much:

State and ID could be helpful, so adding those. Quiesced is not required IMO.

CHANGELOG.next.asciidoc Outdated Show resolved Hide resolved
@ishleenk17 ishleenk17 changed the title Add support of snapshot in virtualmachine Add support of snapshot in vSphere virtualmachine metricset Sep 5, 2024
Copy link
Contributor

@niraj-elastic niraj-elastic left a comment

Choose a reason for hiding this comment

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

LGTM

@ishleenk17 ishleenk17 merged commit 7f29f60 into elastic:main Sep 6, 2024
29 checks passed
@niraj-elastic
Copy link
Contributor

niraj-elastic commented Sep 6, 2024

@ishleenk17 I was working on alerts and i found that the way in which we define snapshot field, does not work. It will create field with name snapshot.info also since name in objects are defined with capital letters the dynamic fields will also be capital. For example
"snapshot.info": { "ID": "xxxx" }

We have to change it.

@shmsr
Copy link
Member

shmsr commented Sep 6, 2024

@ishleenk17 I was working on alerts and i found that the way in which we define snapshot field, does not work. It will create field with name snapshot.info also since name in objects are defined with capital letters the dynamic fields will also be capital. For example "snapshot.info": { "ID": "xxxx" }

We have to change it.

I agree with this point to make it small-case. But I'm curious; if field names are not in small-case, alerts don't work?

@ishleenk17 ishleenk17 added the backport-8.15 Automated backport to the 8.15 branch with mergify label Sep 13, 2024
mergify bot pushed a commit that referenced this pull request Sep 13, 2024
* Add support of snapshot in virtualmachine

* mage update and add fields.yml

* Update data_test.go

* address comments, mage update

* Lint error

* Add createtime

* Update CHANGELOG.next.asciidoc

* Add state and id for snapshot

* update changelog

* lint change

* lint change

(cherry picked from commit 7f29f60)

# Conflicts:
#	metricbeat/docs/fields.asciidoc
#	metricbeat/module/vsphere/fields.go
#	metricbeat/module/vsphere/virtualmachine/_meta/data.json
#	metricbeat/module/vsphere/virtualmachine/_meta/fields.yml
#	metricbeat/module/vsphere/virtualmachine/data.go
#	metricbeat/module/vsphere/virtualmachine/data_test.go
#	metricbeat/module/vsphere/virtualmachine/virtualmachine.go
ishleenk17 added a commit that referenced this pull request Sep 14, 2024
* Add support of snapshot in virtualmachine

* mage update and add fields.yml

* Update data_test.go

* address comments, mage update

* Lint error

* Add createtime

* Update CHANGELOG.next.asciidoc

* Add state and id for snapshot

* update changelog

* lint change

* lint change
ishleenk17 added a commit that referenced this pull request Sep 14, 2024
…hine metricset (#40830)

* Add support of snapshot in vSphere virtualmachine metricset (#40683)

* Add support of snapshot in virtualmachine

* mage update and add fields.yml

* Update data_test.go

* address comments, mage update

* Lint error

* Add createtime

* Update CHANGELOG.next.asciidoc

* Add state and id for snapshot

* update changelog

* lint change

* lint change

* Update CHANGELOG.next.asciidoc

* Update CHANGELOG.next.asciidoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.15 Automated backport to the 8.15 branch with mergify enhancement Team:Obs-InfraObs Label for the Observability Infrastructure Monitoring team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants