2016-06-28 06:15:37 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-11-21 06:51:36 -08:00
|
|
|
package gce
|
2016-06-28 06:15:37 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2016-06-28 06:15:37 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2016-10-17 00:45:43 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
2016-06-28 06:15:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-10-07 23:40:12 -07:00
|
|
|
gceLabel = model.MetaLabelPrefix + "gce_"
|
|
|
|
gceLabelProject = gceLabel + "project"
|
|
|
|
gceLabelZone = gceLabel + "zone"
|
|
|
|
gceLabelNetwork = gceLabel + "network"
|
|
|
|
gceLabelSubnetwork = gceLabel + "subnetwork"
|
|
|
|
gceLabelPublicIP = gceLabel + "public_ip"
|
|
|
|
gceLabelPrivateIP = gceLabel + "private_ip"
|
|
|
|
gceLabelInstanceName = gceLabel + "instance_name"
|
|
|
|
gceLabelInstanceStatus = gceLabel + "instance_status"
|
|
|
|
gceLabelTags = gceLabel + "tags"
|
2016-10-17 00:45:43 -07:00
|
|
|
gceLabelMetadata = gceLabel + "metadata_"
|
2016-06-28 06:15:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-10-26 02:03:35 -07:00
|
|
|
gceSDRefreshFailuresCount = prometheus.NewCounter(
|
2016-06-28 06:15:37 -07:00
|
|
|
prometheus.CounterOpts{
|
2016-11-21 06:51:36 -08:00
|
|
|
Name: "prometheus_sd_gce_refresh_failures_total",
|
|
|
|
Help: "The number of GCE-SD refresh failures.",
|
2016-06-28 06:15:37 -07:00
|
|
|
})
|
2016-10-26 02:03:35 -07:00
|
|
|
gceSDRefreshDuration = prometheus.NewSummary(
|
2016-06-28 06:15:37 -07:00
|
|
|
prometheus.SummaryOpts{
|
2016-11-21 06:51:36 -08:00
|
|
|
Name: "prometheus_sd_gce_refresh_duration",
|
|
|
|
Help: "The duration of a GCE-SD refresh in seconds.",
|
2016-06-28 06:15:37 -07:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-10-26 02:03:35 -07:00
|
|
|
prometheus.MustRegister(gceSDRefreshFailuresCount)
|
|
|
|
prometheus.MustRegister(gceSDRefreshDuration)
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
// Discovery periodically performs GCE-SD requests. It implements
|
2016-06-28 06:15:37 -07:00
|
|
|
// the TargetProvider interface.
|
2017-03-16 16:29:47 -07:00
|
|
|
type Discovery struct {
|
2016-06-28 06:15:37 -07:00
|
|
|
project string
|
|
|
|
zone string
|
|
|
|
filter string
|
|
|
|
client *http.Client
|
|
|
|
svc *compute.Service
|
|
|
|
isvc *compute.InstancesService
|
|
|
|
interval time.Duration
|
|
|
|
port int
|
|
|
|
tagSeparator string
|
2017-04-28 08:12:38 -07:00
|
|
|
logger log.Logger
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
// NewDiscovery returns a new Discovery which periodically refreshes its targets.
|
2017-04-28 08:12:38 -07:00
|
|
|
func NewDiscovery(conf *config.GCESDConfig, logger log.Logger) (*Discovery, error) {
|
2017-08-11 11:45:52 -07:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2017-03-16 16:29:47 -07:00
|
|
|
gd := &Discovery{
|
2016-06-28 06:15:37 -07:00
|
|
|
project: conf.Project,
|
|
|
|
zone: conf.Zone,
|
|
|
|
filter: conf.Filter,
|
|
|
|
interval: time.Duration(conf.RefreshInterval),
|
|
|
|
port: conf.Port,
|
|
|
|
tagSeparator: conf.TagSeparator,
|
2017-04-28 08:12:38 -07:00
|
|
|
logger: logger,
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
var err error
|
|
|
|
gd.client, err = google.DefaultClient(oauth2.NoContext, compute.ComputeReadonlyScope)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error setting up communication with GCE service: %s", err)
|
|
|
|
}
|
|
|
|
gd.svc, err = compute.New(gd.client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error setting up communication with GCE service: %s", err)
|
|
|
|
}
|
|
|
|
gd.isvc = compute.NewInstancesService(gd.svc)
|
|
|
|
return gd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2017-03-16 16:29:47 -07:00
|
|
|
func (d *Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
2016-06-28 06:15:37 -07:00
|
|
|
// Get an initial set right away.
|
2017-03-16 16:29:47 -07:00
|
|
|
tg, err := d.refresh()
|
2016-06-28 06:15:37 -07:00
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(d.logger).Log("msg", "Refresh failed", "err", err)
|
2016-06-28 06:15:37 -07:00
|
|
|
} else {
|
2016-11-18 01:55:29 -08:00
|
|
|
select {
|
|
|
|
case ch <- []*config.TargetGroup{tg}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
ticker := time.NewTicker(d.interval)
|
2016-06-28 06:15:37 -07:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2017-03-16 16:29:47 -07:00
|
|
|
tg, err := d.refresh()
|
2016-06-28 06:15:37 -07:00
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(d.logger).Log("msg", "Refresh failed", "err", err)
|
2016-11-18 01:55:29 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case ch <- []*config.TargetGroup{tg}:
|
|
|
|
case <-ctx.Done():
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
func (d *Discovery) refresh() (tg *config.TargetGroup, err error) {
|
2016-06-28 06:15:37 -07:00
|
|
|
t0 := time.Now()
|
|
|
|
defer func() {
|
2016-10-26 02:03:35 -07:00
|
|
|
gceSDRefreshDuration.Observe(time.Since(t0).Seconds())
|
2016-06-28 06:15:37 -07:00
|
|
|
if err != nil {
|
2016-10-26 02:03:35 -07:00
|
|
|
gceSDRefreshFailuresCount.Inc()
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
tg = &config.TargetGroup{
|
2017-03-16 16:29:47 -07:00
|
|
|
Source: fmt.Sprintf("GCE_%s_%s", d.project, d.zone),
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
ilc := d.isvc.List(d.project, d.zone)
|
|
|
|
if len(d.filter) > 0 {
|
|
|
|
ilc = ilc.Filter(d.filter)
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
err = ilc.Pages(nil, func(l *compute.InstanceList) error {
|
|
|
|
for _, inst := range l.Items {
|
|
|
|
if len(inst.NetworkInterfaces) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
labels := model.LabelSet{
|
2017-03-16 16:29:47 -07:00
|
|
|
gceLabelProject: model.LabelValue(d.project),
|
2016-10-07 23:40:12 -07:00
|
|
|
gceLabelZone: model.LabelValue(inst.Zone),
|
|
|
|
gceLabelInstanceName: model.LabelValue(inst.Name),
|
|
|
|
gceLabelInstanceStatus: model.LabelValue(inst.Status),
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
priIface := inst.NetworkInterfaces[0]
|
|
|
|
labels[gceLabelNetwork] = model.LabelValue(priIface.Network)
|
|
|
|
labels[gceLabelSubnetwork] = model.LabelValue(priIface.Subnetwork)
|
|
|
|
labels[gceLabelPrivateIP] = model.LabelValue(priIface.NetworkIP)
|
2017-03-16 16:29:47 -07:00
|
|
|
addr := fmt.Sprintf("%s:%d", priIface.NetworkIP, d.port)
|
2016-06-28 06:15:37 -07:00
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
|
|
|
|
2016-10-17 00:45:43 -07:00
|
|
|
// Tags in GCE are usually only used for networking rules.
|
2016-06-28 06:15:37 -07:00
|
|
|
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.
|
2017-03-16 16:29:47 -07:00
|
|
|
tags := d.tagSeparator + strings.Join(inst.Tags.Items, d.tagSeparator) + d.tagSeparator
|
2016-06-28 06:15:37 -07:00
|
|
|
labels[gceLabelTags] = model.LabelValue(tags)
|
|
|
|
}
|
|
|
|
|
2016-10-17 00:45:43 -07:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 06:15:37 -07:00
|
|
|
if len(priIface.AccessConfigs) > 0 {
|
|
|
|
ac := priIface.AccessConfigs[0]
|
|
|
|
if ac.Type == "ONE_TO_ONE_NAT" {
|
|
|
|
labels[gceLabelPublicIP] = model.LabelValue(ac.NatIP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, labels)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-10-26 02:03:35 -07:00
|
|
|
return tg, fmt.Errorf("error retrieving refresh targets from gce: %s", err)
|
2016-06-28 06:15:37 -07:00
|
|
|
}
|
|
|
|
return tg, nil
|
|
|
|
}
|