2015-06-09 04:25:30 -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 zookeeper
|
2015-06-09 04:25:30 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-09-05 05:40:28 -07:00
|
|
|
"net"
|
2015-08-26 07:53:37 -07:00
|
|
|
"strconv"
|
2015-06-09 04:25:30 -07:00
|
|
|
"time"
|
|
|
|
|
2015-08-22 00:42:45 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-06-09 04:25:30 -07:00
|
|
|
"github.com/samuel/go-zookeeper/zk"
|
2016-02-18 08:26:27 -08:00
|
|
|
"golang.org/x/net/context"
|
2015-06-09 04:25:30 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-07-18 14:23:58 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
2016-01-09 15:34:32 -08:00
|
|
|
"github.com/prometheus/prometheus/util/treecache"
|
2015-06-09 04:25:30 -07:00
|
|
|
)
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
// Discovery implements the TargetProvider interface for discovering
|
|
|
|
// targets from Zookeeper.
|
|
|
|
type Discovery struct {
|
2016-11-18 02:20:28 -08:00
|
|
|
conn *zk.Conn
|
2015-06-09 04:25:30 -07:00
|
|
|
|
2016-11-18 02:20:28 -08:00
|
|
|
sources map[string]*config.TargetGroup
|
2015-06-09 04:25:30 -07:00
|
|
|
|
2016-01-09 15:34:32 -08:00
|
|
|
updates chan treecache.ZookeeperTreeCacheEvent
|
|
|
|
treeCaches []*treecache.ZookeeperTreeCache
|
2016-11-18 02:20:28 -08:00
|
|
|
|
|
|
|
parse func(data []byte, path string) (model.LabelSet, error)
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
// NewNerveDiscovery returns a new Discovery for the given Nerve config.
|
|
|
|
func NewNerveDiscovery(conf *config.NerveSDConfig) *Discovery {
|
2016-11-21 06:51:36 -08:00
|
|
|
return NewDiscovery(conf.Servers, time.Duration(conf.Timeout), conf.Paths, parseNerveMember)
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
// NewServersetDiscovery returns a new Discovery for the given serverset config.
|
|
|
|
func NewServersetDiscovery(conf *config.ServersetSDConfig) *Discovery {
|
2016-11-21 06:51:36 -08:00
|
|
|
return NewDiscovery(conf.Servers, time.Duration(conf.Timeout), conf.Paths, parseServersetMember)
|
2016-11-18 02:20:28 -08:00
|
|
|
}
|
|
|
|
|
2016-11-21 06:51:36 -08:00
|
|
|
// NewDiscovery returns a new discovery along Zookeeper parses with
|
2016-11-18 02:20:28 -08:00
|
|
|
// the given parse function.
|
2016-11-21 06:51:36 -08:00
|
|
|
func NewDiscovery(
|
2016-11-18 02:20:28 -08:00
|
|
|
srvs []string,
|
|
|
|
timeout time.Duration,
|
|
|
|
paths []string,
|
|
|
|
pf func(data []byte, path string) (model.LabelSet, error),
|
2017-03-16 16:29:47 -07:00
|
|
|
) *Discovery {
|
|
|
|
conn, _, err := zk.Connect(srvs, timeout)
|
2016-01-09 15:34:32 -08:00
|
|
|
conn.SetLogger(treecache.ZookeeperLogger{})
|
2015-06-09 04:25:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-09 15:34:32 -08:00
|
|
|
updates := make(chan treecache.ZookeeperTreeCacheEvent)
|
2017-03-16 16:29:47 -07:00
|
|
|
sd := &Discovery{
|
2015-06-09 04:25:30 -07:00
|
|
|
conn: conn,
|
|
|
|
updates: updates,
|
|
|
|
sources: map[string]*config.TargetGroup{},
|
2016-11-18 02:20:28 -08:00
|
|
|
parse: pf,
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
2016-11-18 02:20:28 -08:00
|
|
|
for _, path := range paths {
|
2016-01-09 15:34:32 -08:00
|
|
|
sd.treeCaches = append(sd.treeCaches, treecache.NewZookeeperTreeCache(conn, path, updates))
|
2015-10-06 06:47:34 -07:00
|
|
|
}
|
2015-06-09 04:25:30 -07:00
|
|
|
return sd
|
|
|
|
}
|
|
|
|
|
2016-11-18 02:20:28 -08:00
|
|
|
// 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-11-18 02:20:28 -08:00
|
|
|
defer func() {
|
2017-03-16 16:29:47 -07:00
|
|
|
for _, tc := range d.treeCaches {
|
2016-11-18 02:20:28 -08:00
|
|
|
tc.Stop()
|
|
|
|
}
|
|
|
|
// Drain event channel in case the treecache leaks goroutines otherwise.
|
2017-03-16 16:29:47 -07:00
|
|
|
for range d.updates {
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
2017-03-16 16:29:47 -07:00
|
|
|
d.conn.Close()
|
2016-11-18 02:20:28 -08:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-05-02 16:21:37 -07:00
|
|
|
return
|
2017-03-16 16:29:47 -07:00
|
|
|
case event := <-d.updates:
|
2016-11-18 02:20:28 -08:00
|
|
|
tg := &config.TargetGroup{
|
|
|
|
Source: event.Path,
|
|
|
|
}
|
|
|
|
if event.Data != nil {
|
2017-03-16 16:29:47 -07:00
|
|
|
labelSet, err := d.parse(*event.Data, event.Path)
|
2016-11-18 02:20:28 -08:00
|
|
|
if err == nil {
|
|
|
|
tg.Targets = []model.LabelSet{labelSet}
|
2017-03-16 16:29:47 -07:00
|
|
|
d.sources[event.Path] = tg
|
2016-11-18 02:20:28 -08:00
|
|
|
} else {
|
2017-03-16 16:29:47 -07:00
|
|
|
delete(d.sources, event.Path)
|
2016-11-18 02:20:28 -08:00
|
|
|
}
|
2015-06-09 04:25:30 -07:00
|
|
|
} else {
|
2017-03-16 16:29:47 -07:00
|
|
|
delete(d.sources, event.Path)
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
2016-11-18 02:20:28 -08:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case ch <- []*config.TargetGroup{tg}:
|
|
|
|
}
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 02:20:28 -08:00
|
|
|
const (
|
|
|
|
serversetLabelPrefix = model.MetaLabelPrefix + "serverset_"
|
|
|
|
serversetStatusLabel = serversetLabelPrefix + "status"
|
|
|
|
serversetPathLabel = serversetLabelPrefix + "path"
|
|
|
|
serversetEndpointLabelPrefix = serversetLabelPrefix + "endpoint"
|
|
|
|
serversetShardLabel = serversetLabelPrefix + "shard"
|
|
|
|
)
|
2016-02-18 08:26:27 -08:00
|
|
|
|
2016-11-18 02:20:28 -08:00
|
|
|
type serversetMember struct {
|
|
|
|
ServiceEndpoint serversetEndpoint
|
|
|
|
AdditionalEndpoints map[string]serversetEndpoint
|
|
|
|
Status string `json:"status"`
|
|
|
|
Shard int `json:"shard"`
|
|
|
|
}
|
2015-06-09 04:25:30 -07:00
|
|
|
|
2016-11-18 02:20:28 -08:00
|
|
|
type serversetEndpoint struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
|
|
|
|
2016-11-18 02:20:28 -08:00
|
|
|
func parseServersetMember(data []byte, path string) (model.LabelSet, error) {
|
2015-06-09 04:25:30 -07:00
|
|
|
member := serversetMember{}
|
2016-02-18 08:26:27 -08:00
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &member); err != nil {
|
2015-06-09 04:25:30 -07:00
|
|
|
return nil, fmt.Errorf("error unmarshaling serverset member %q: %s", path, err)
|
|
|
|
}
|
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
labels := model.LabelSet{}
|
|
|
|
labels[serversetPathLabel] = model.LabelValue(path)
|
|
|
|
labels[model.AddressLabel] = model.LabelValue(
|
2016-09-05 05:40:28 -07:00
|
|
|
net.JoinHostPort(member.ServiceEndpoint.Host, fmt.Sprintf("%d", member.ServiceEndpoint.Port)))
|
2015-06-09 04:25:30 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
labels[serversetEndpointLabelPrefix+"_host"] = model.LabelValue(member.ServiceEndpoint.Host)
|
|
|
|
labels[serversetEndpointLabelPrefix+"_port"] = model.LabelValue(fmt.Sprintf("%d", member.ServiceEndpoint.Port))
|
2015-06-09 04:25:30 -07:00
|
|
|
|
|
|
|
for name, endpoint := range member.AdditionalEndpoints {
|
2015-08-20 08:18:46 -07:00
|
|
|
cleanName := model.LabelName(strutil.SanitizeLabelName(name))
|
|
|
|
labels[serversetEndpointLabelPrefix+"_host_"+cleanName] = model.LabelValue(
|
2015-06-09 04:25:30 -07:00
|
|
|
endpoint.Host)
|
2015-08-20 08:18:46 -07:00
|
|
|
labels[serversetEndpointLabelPrefix+"_port_"+cleanName] = model.LabelValue(
|
2015-06-09 04:25:30 -07:00
|
|
|
fmt.Sprintf("%d", endpoint.Port))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
labels[serversetStatusLabel] = model.LabelValue(member.Status)
|
2015-08-26 07:53:37 -07:00
|
|
|
labels[serversetShardLabel] = model.LabelValue(strconv.Itoa(member.Shard))
|
2015-06-09 04:25:30 -07:00
|
|
|
|
2016-11-18 02:20:28 -08:00
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
nerveLabelPrefix = model.MetaLabelPrefix + "nerve_"
|
|
|
|
nervePathLabel = nerveLabelPrefix + "path"
|
|
|
|
nerveEndpointLabelPrefix = nerveLabelPrefix + "endpoint"
|
|
|
|
)
|
|
|
|
|
|
|
|
type nerveMember struct {
|
|
|
|
Host string `json:"host"`
|
|
|
|
Port int `json:"port"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNerveMember(data []byte, path string) (model.LabelSet, error) {
|
|
|
|
member := nerveMember{}
|
|
|
|
err := json.Unmarshal(data, &member)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error unmarshaling nerve member %q: %s", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := model.LabelSet{}
|
|
|
|
labels[nervePathLabel] = model.LabelValue(path)
|
|
|
|
labels[model.AddressLabel] = model.LabelValue(
|
|
|
|
net.JoinHostPort(member.Host, fmt.Sprintf("%d", member.Port)))
|
|
|
|
|
|
|
|
labels[nerveEndpointLabelPrefix+"_host"] = model.LabelValue(member.Host)
|
|
|
|
labels[nerveEndpointLabelPrefix+"_port"] = model.LabelValue(fmt.Sprintf("%d", member.Port))
|
|
|
|
labels[nerveEndpointLabelPrefix+"_name"] = model.LabelValue(member.Name)
|
|
|
|
|
|
|
|
return labels, nil
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|