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.
|
|
|
|
|
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-08-26 07:53:37 -07:00
|
|
|
"strconv"
|
2015-06-09 04:25:30 -07:00
|
|
|
"sync"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-08-20 08:18:46 -07:00
|
|
|
serversetLabelPrefix = model.MetaLabelPrefix + "serverset_"
|
2015-06-09 04:25:30 -07:00
|
|
|
serversetStatusLabel = serversetLabelPrefix + "status"
|
|
|
|
serversetPathLabel = serversetLabelPrefix + "path"
|
|
|
|
serversetEndpointLabelPrefix = serversetLabelPrefix + "endpoint"
|
2015-08-26 07:53:37 -07:00
|
|
|
serversetShardLabel = serversetLabelPrefix + "shard"
|
2015-06-09 04:25:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type serversetMember struct {
|
|
|
|
ServiceEndpoint serversetEndpoint
|
|
|
|
AdditionalEndpoints map[string]serversetEndpoint
|
|
|
|
Status string `json:"status"`
|
2015-08-26 07:53:37 -07:00
|
|
|
Shard int `json:"shard"`
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type serversetEndpoint struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServersetDiscovery retrieves target information from a Serverset server
|
|
|
|
// and updates them via watches.
|
|
|
|
type ServersetDiscovery struct {
|
2015-10-06 06:47:34 -07:00
|
|
|
conf *config.ServersetSDConfig
|
|
|
|
conn *zk.Conn
|
|
|
|
mu sync.RWMutex
|
|
|
|
sources map[string]*config.TargetGroup
|
2016-02-18 08:26:27 -08:00
|
|
|
sdUpdates *chan<- []*config.TargetGroup
|
2016-01-09 15:34:32 -08:00
|
|
|
updates chan treecache.ZookeeperTreeCacheEvent
|
|
|
|
treeCaches []*treecache.ZookeeperTreeCache
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServersetDiscovery returns a new ServersetDiscovery for the given config.
|
|
|
|
func NewServersetDiscovery(conf *config.ServersetSDConfig) *ServersetDiscovery {
|
|
|
|
conn, _, err := zk.Connect(conf.Servers, time.Duration(conf.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)
|
2015-06-09 04:25:30 -07:00
|
|
|
sd := &ServersetDiscovery{
|
|
|
|
conf: conf,
|
|
|
|
conn: conn,
|
|
|
|
updates: updates,
|
|
|
|
sources: map[string]*config.TargetGroup{},
|
|
|
|
}
|
|
|
|
go sd.processUpdates()
|
2015-10-06 06:47:34 -07:00
|
|
|
for _, path := range conf.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *ServersetDiscovery) processUpdates() {
|
|
|
|
defer sd.conn.Close()
|
|
|
|
for event := range sd.updates {
|
|
|
|
tg := &config.TargetGroup{
|
2015-08-07 04:18:19 -07:00
|
|
|
Source: event.Path,
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
|
|
|
sd.mu.Lock()
|
|
|
|
if event.Data != nil {
|
|
|
|
labelSet, err := parseServersetMember(*event.Data, event.Path)
|
|
|
|
if err == nil {
|
2015-08-20 08:18:46 -07:00
|
|
|
tg.Targets = []model.LabelSet{*labelSet}
|
2015-06-09 04:25:30 -07:00
|
|
|
sd.sources[event.Path] = tg
|
|
|
|
} else {
|
|
|
|
delete(sd.sources, event.Path)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete(sd.sources, event.Path)
|
|
|
|
}
|
|
|
|
sd.mu.Unlock()
|
|
|
|
if sd.sdUpdates != nil {
|
2016-02-18 08:26:27 -08:00
|
|
|
*sd.sdUpdates <- []*config.TargetGroup{tg}
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sd.sdUpdates != nil {
|
|
|
|
close(*sd.sdUpdates)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2016-02-18 08:26:27 -08:00
|
|
|
func (sd *ServersetDiscovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
2015-06-09 04:25:30 -07:00
|
|
|
// Send on everything we have seen so far.
|
|
|
|
sd.mu.Lock()
|
2016-02-18 08:26:27 -08:00
|
|
|
|
|
|
|
all := make([]*config.TargetGroup, 0, len(sd.sources))
|
|
|
|
|
|
|
|
for _, tg := range sd.sources {
|
|
|
|
all = append(all, tg)
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
2016-02-18 08:26:27 -08:00
|
|
|
ch <- all
|
|
|
|
|
2015-06-09 04:25:30 -07:00
|
|
|
// Tell processUpdates to send future updates.
|
|
|
|
sd.sdUpdates = &ch
|
|
|
|
sd.mu.Unlock()
|
|
|
|
|
2016-02-18 08:26:27 -08:00
|
|
|
<-ctx.Done()
|
2015-10-06 06:47:34 -07:00
|
|
|
for _, tc := range sd.treeCaches {
|
|
|
|
tc.Stop()
|
|
|
|
}
|
2015-06-09 04:25:30 -07:00
|
|
|
}
|
|
|
|
|
2015-08-20 08:18:46 -07: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(
|
2015-06-09 04:25:30 -07:00
|
|
|
fmt.Sprintf("%s:%d", member.ServiceEndpoint.Host, member.ServiceEndpoint.Port))
|
|
|
|
|
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
|
|
|
|
|
|
|
return &labels, nil
|
|
|
|
}
|