From bfa70996161835100a460a752abbd56d8e620f4d Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Mon, 17 Oct 2016 09:45:43 +0200 Subject: [PATCH] Report GCE instance metdata (#2084) * Report GCE instance metdata * Fix spelling acording to code review guidelines * Address review comments --- retrieval/discovery/gce.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/retrieval/discovery/gce.go b/retrieval/discovery/gce.go index 7d20c5701..16359ae31 100644 --- a/retrieval/discovery/gce.go +++ b/retrieval/discovery/gce.go @@ -29,6 +29,7 @@ import ( "golang.org/x/oauth2/google" "github.com/prometheus/prometheus/config" + "github.com/prometheus/prometheus/util/strutil" ) const ( @@ -42,6 +43,7 @@ const ( gceLabelInstanceName = gceLabel + "instance_name" gceLabelInstanceStatus = gceLabel + "instance_status" gceLabelTags = gceLabel + "tags" + gceLabelMetadata = gceLabel + "metadata_" // Constants for instrumentation. namespace = "prometheus" @@ -177,6 +179,7 @@ func (gd *GCEDiscovery) refresh() (tg *config.TargetGroup, err error) { addr := fmt.Sprintf("%s:%d", priIface.NetworkIP, gd.port) labels[model.AddressLabel] = model.LabelValue(addr) + // Tags in GCE are usually only used for networking rules. if inst.Tags != nil && len(inst.Tags.Items) > 0 { // We surround the separated list with the separator as well. This way regular expressions // in relabeling rules don't have to consider tag positions. @@ -184,6 +187,18 @@ func (gd *GCEDiscovery) refresh() (tg *config.TargetGroup, err error) { labels[gceLabelTags] = model.LabelValue(tags) } + // GCE metadata are key-value pairs for user supplied attributes. + if inst.Metadata != nil { + for _, i := range inst.Metadata.Items { + // Protect against occasional nil pointers. + if i.Value == nil { + continue + } + name := strutil.SanitizeLabelName(i.Key) + labels[gceLabelMetadata+model.LabelName(name)] = model.LabelValue(*i.Value) + } + } + if len(priIface.AccessConfigs) > 0 { ac := priIface.AccessConfigs[0] if ac.Type == "ONE_TO_ONE_NAT" {