Report GCE instance metdata (#2084)

* Report GCE instance metdata

* Fix spelling acording to code review guidelines

* Address review comments
This commit is contained in:
Dominik Schulz 2016-10-17 09:45:43 +02:00 committed by Fabian Reinartz
parent 50f8e35c54
commit bfa7099616

View file

@ -29,6 +29,7 @@ import (
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/util/strutil"
) )
const ( const (
@ -42,6 +43,7 @@ const (
gceLabelInstanceName = gceLabel + "instance_name" gceLabelInstanceName = gceLabel + "instance_name"
gceLabelInstanceStatus = gceLabel + "instance_status" gceLabelInstanceStatus = gceLabel + "instance_status"
gceLabelTags = gceLabel + "tags" gceLabelTags = gceLabel + "tags"
gceLabelMetadata = gceLabel + "metadata_"
// Constants for instrumentation. // Constants for instrumentation.
namespace = "prometheus" namespace = "prometheus"
@ -177,6 +179,7 @@ func (gd *GCEDiscovery) refresh() (tg *config.TargetGroup, err error) {
addr := fmt.Sprintf("%s:%d", priIface.NetworkIP, gd.port) addr := fmt.Sprintf("%s:%d", priIface.NetworkIP, gd.port)
labels[model.AddressLabel] = model.LabelValue(addr) 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 { if inst.Tags != nil && len(inst.Tags.Items) > 0 {
// We surround the separated list with the separator as well. This way regular expressions // We surround the separated list with the separator as well. This way regular expressions
// in relabeling rules don't have to consider tag positions. // 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) 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 { if len(priIface.AccessConfigs) > 0 {
ac := priIface.AccessConfigs[0] ac := priIface.AccessConfigs[0]
if ac.Type == "ONE_TO_ONE_NAT" { if ac.Type == "ONE_TO_ONE_NAT" {