discovery/openstack: Include instance image ID in labels

Adds a new label to include the ID of the image that an instance is
using. This can be used for example to filter a job to only include
instances using a certain image as that image includes some exporter.

Sometimes the image information isn't available, such as when the image
is private and the user doesn't have the roles required to see it. In
those cases we just don't set the label, as the rest of the information
from the discovery provider can still be used.

Signed-off-by: Taavi Väänänen <hi@taavi.wtf>
This commit is contained in:
Taavi Väänänen 2023-06-30 23:12:24 +03:00
parent 47b613a3cb
commit 0f4c27e2bf
No known key found for this signature in database
GPG key ID: EF242F709F912FBE
3 changed files with 12 additions and 2 deletions

View file

@ -36,6 +36,7 @@ const (
openstackLabelAddressPool = openstackLabelPrefix + "address_pool"
openstackLabelInstanceFlavor = openstackLabelPrefix + "instance_flavor"
openstackLabelInstanceID = openstackLabelPrefix + "instance_id"
openstackLabelInstanceImage = openstackLabelPrefix + "instance_image"
openstackLabelInstanceName = openstackLabelPrefix + "instance_name"
openstackLabelInstanceStatus = openstackLabelPrefix + "instance_status"
openstackLabelPrivateIP = openstackLabelPrefix + "private_ip"
@ -144,12 +145,18 @@ func (i *InstanceDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group,
openstackLabelUserID: model.LabelValue(s.UserID),
}
id, ok := s.Flavor["id"].(string)
flavorId, ok := s.Flavor["id"].(string)
if !ok {
level.Warn(i.logger).Log("msg", "Invalid type for flavor id, expected string")
continue
}
labels[openstackLabelInstanceFlavor] = model.LabelValue(id)
labels[openstackLabelInstanceFlavor] = model.LabelValue(flavorId)
imageId, ok := s.Image["id"].(string)
if ok {
labels[openstackLabelInstanceImage] = model.LabelValue(imageId)
}
for k, v := range s.Metadata {
name := strutil.SanitizeLabelName(k)
labels[openstackLabelTagPrefix+model.LabelName(name)] = model.LabelValue(v)

View file

@ -73,6 +73,7 @@ func TestOpenstackSDInstanceRefresh(t *testing.T) {
"__address__": model.LabelValue("10.0.0.32:0"),
"__meta_openstack_instance_flavor": model.LabelValue("1"),
"__meta_openstack_instance_id": model.LabelValue("ef079b0c-e610-4dfb-b1aa-b49f07ac48e5"),
"__meta_openstack_instance_image": model.LabelValue("f90f6034-2570-4974-8351-6b49732ef2eb"),
"__meta_openstack_instance_status": model.LabelValue("ACTIVE"),
"__meta_openstack_instance_name": model.LabelValue("herp"),
"__meta_openstack_private_ip": model.LabelValue("10.0.0.32"),
@ -85,6 +86,7 @@ func TestOpenstackSDInstanceRefresh(t *testing.T) {
"__address__": model.LabelValue("10.0.0.31:0"),
"__meta_openstack_instance_flavor": model.LabelValue("1"),
"__meta_openstack_instance_id": model.LabelValue("9e5476bd-a4ec-4653-93d6-72c93aa682ba"),
"__meta_openstack_instance_image": model.LabelValue("f90f6034-2570-4974-8351-6b49732ef2eb"),
"__meta_openstack_instance_status": model.LabelValue("ACTIVE"),
"__meta_openstack_instance_name": model.LabelValue("derp"),
"__meta_openstack_private_ip": model.LabelValue("10.0.0.31"),

View file

@ -1276,6 +1276,7 @@ The following meta labels are available on targets during [relabeling](#relabel_
* `__meta_openstack_address_pool`: the pool of the private IP.
* `__meta_openstack_instance_flavor`: the flavor of the OpenStack instance.
* `__meta_openstack_instance_id`: the OpenStack instance ID.
* `__meta_openstack_instance_image`: the ID of the image the OpenStack instance is using.
* `__meta_openstack_instance_name`: the OpenStack instance name.
* `__meta_openstack_instance_status`: the status of the OpenStack instance.
* `__meta_openstack_private_ip`: the private IP of the OpenStack instance.