mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
5.9 KiB
198 lines
5.9 KiB
10 years ago
|
// 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.
|
||
|
|
||
8 years ago
|
package zookeeper
|
||
10 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
8 years ago
|
"net"
|
||
9 years ago
|
"strconv"
|
||
10 years ago
|
"time"
|
||
|
|
||
9 years ago
|
"github.com/prometheus/common/model"
|
||
10 years ago
|
"github.com/samuel/go-zookeeper/zk"
|
||
9 years ago
|
"golang.org/x/net/context"
|
||
10 years ago
|
|
||
|
"github.com/prometheus/prometheus/config"
|
||
9 years ago
|
"github.com/prometheus/prometheus/util/strutil"
|
||
9 years ago
|
"github.com/prometheus/prometheus/util/treecache"
|
||
10 years ago
|
)
|
||
|
|
||
8 years ago
|
type ZookeeperDiscovery struct {
|
||
|
conn *zk.Conn
|
||
10 years ago
|
|
||
8 years ago
|
sources map[string]*config.TargetGroup
|
||
10 years ago
|
|
||
9 years ago
|
updates chan treecache.ZookeeperTreeCacheEvent
|
||
|
treeCaches []*treecache.ZookeeperTreeCache
|
||
8 years ago
|
|
||
|
parse func(data []byte, path string) (model.LabelSet, error)
|
||
|
}
|
||
|
|
||
|
// NewNerveDiscovery returns a new NerveDiscovery for the given config.
|
||
|
func NewNerveDiscovery(conf *config.NerveSDConfig) *ZookeeperDiscovery {
|
||
8 years ago
|
return NewDiscovery(conf.Servers, time.Duration(conf.Timeout), conf.Paths, parseNerveMember)
|
||
10 years ago
|
}
|
||
|
|
||
|
// NewServersetDiscovery returns a new ServersetDiscovery for the given config.
|
||
8 years ago
|
func NewServersetDiscovery(conf *config.ServersetSDConfig) *ZookeeperDiscovery {
|
||
8 years ago
|
return NewDiscovery(conf.Servers, time.Duration(conf.Timeout), conf.Paths, parseServersetMember)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// NewDiscovery returns a new discovery along Zookeeper parses with
|
||
8 years ago
|
// the given parse function.
|
||
8 years ago
|
func NewDiscovery(
|
||
8 years ago
|
srvs []string,
|
||
|
timeout time.Duration,
|
||
|
paths []string,
|
||
|
pf func(data []byte, path string) (model.LabelSet, error),
|
||
|
) *ZookeeperDiscovery {
|
||
|
conn, _, err := zk.Connect(srvs, time.Duration(timeout))
|
||
9 years ago
|
conn.SetLogger(treecache.ZookeeperLogger{})
|
||
10 years ago
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
9 years ago
|
updates := make(chan treecache.ZookeeperTreeCacheEvent)
|
||
8 years ago
|
sd := &ZookeeperDiscovery{
|
||
10 years ago
|
conn: conn,
|
||
|
updates: updates,
|
||
|
sources: map[string]*config.TargetGroup{},
|
||
8 years ago
|
parse: pf,
|
||
10 years ago
|
}
|
||
8 years ago
|
for _, path := range paths {
|
||
9 years ago
|
sd.treeCaches = append(sd.treeCaches, treecache.NewZookeeperTreeCache(conn, path, updates))
|
||
9 years ago
|
}
|
||
10 years ago
|
return sd
|
||
|
}
|
||
|
|
||
8 years ago
|
// Run implements the TargetProvider interface.
|
||
|
func (sd *ZookeeperDiscovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
||
|
defer func() {
|
||
|
for _, tc := range sd.treeCaches {
|
||
|
tc.Stop()
|
||
|
}
|
||
|
// Drain event channel in case the treecache leaks goroutines otherwise.
|
||
|
for range sd.updates {
|
||
10 years ago
|
}
|
||
8 years ago
|
sd.conn.Close()
|
||
|
}()
|
||
|
|
||
|
for {
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
case event := <-sd.updates:
|
||
|
tg := &config.TargetGroup{
|
||
|
Source: event.Path,
|
||
|
}
|
||
|
if event.Data != nil {
|
||
|
labelSet, err := sd.parse(*event.Data, event.Path)
|
||
|
if err == nil {
|
||
|
tg.Targets = []model.LabelSet{labelSet}
|
||
|
sd.sources[event.Path] = tg
|
||
|
} else {
|
||
|
delete(sd.sources, event.Path)
|
||
|
}
|
||
10 years ago
|
} else {
|
||
|
delete(sd.sources, event.Path)
|
||
|
}
|
||
8 years ago
|
select {
|
||
|
case <-ctx.Done():
|
||
|
return
|
||
|
case ch <- []*config.TargetGroup{tg}:
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
const (
|
||
|
serversetLabelPrefix = model.MetaLabelPrefix + "serverset_"
|
||
|
serversetStatusLabel = serversetLabelPrefix + "status"
|
||
|
serversetPathLabel = serversetLabelPrefix + "path"
|
||
|
serversetEndpointLabelPrefix = serversetLabelPrefix + "endpoint"
|
||
|
serversetShardLabel = serversetLabelPrefix + "shard"
|
||
|
)
|
||
9 years ago
|
|
||
8 years ago
|
type serversetMember struct {
|
||
|
ServiceEndpoint serversetEndpoint
|
||
|
AdditionalEndpoints map[string]serversetEndpoint
|
||
|
Status string `json:"status"`
|
||
|
Shard int `json:"shard"`
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
type serversetEndpoint struct {
|
||
|
Host string
|
||
|
Port int
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func parseServersetMember(data []byte, path string) (model.LabelSet, error) {
|
||
10 years ago
|
member := serversetMember{}
|
||
9 years ago
|
|
||
|
if err := json.Unmarshal(data, &member); err != nil {
|
||
10 years ago
|
return nil, fmt.Errorf("error unmarshaling serverset member %q: %s", path, err)
|
||
|
}
|
||
|
|
||
9 years ago
|
labels := model.LabelSet{}
|
||
|
labels[serversetPathLabel] = model.LabelValue(path)
|
||
|
labels[model.AddressLabel] = model.LabelValue(
|
||
8 years ago
|
net.JoinHostPort(member.ServiceEndpoint.Host, fmt.Sprintf("%d", member.ServiceEndpoint.Port)))
|
||
10 years ago
|
|
||
9 years ago
|
labels[serversetEndpointLabelPrefix+"_host"] = model.LabelValue(member.ServiceEndpoint.Host)
|
||
|
labels[serversetEndpointLabelPrefix+"_port"] = model.LabelValue(fmt.Sprintf("%d", member.ServiceEndpoint.Port))
|
||
10 years ago
|
|
||
|
for name, endpoint := range member.AdditionalEndpoints {
|
||
9 years ago
|
cleanName := model.LabelName(strutil.SanitizeLabelName(name))
|
||
|
labels[serversetEndpointLabelPrefix+"_host_"+cleanName] = model.LabelValue(
|
||
10 years ago
|
endpoint.Host)
|
||
9 years ago
|
labels[serversetEndpointLabelPrefix+"_port_"+cleanName] = model.LabelValue(
|
||
10 years ago
|
fmt.Sprintf("%d", endpoint.Port))
|
||
|
|
||
|
}
|
||
|
|
||
9 years ago
|
labels[serversetStatusLabel] = model.LabelValue(member.Status)
|
||
9 years ago
|
labels[serversetShardLabel] = model.LabelValue(strconv.Itoa(member.Shard))
|
||
10 years ago
|
|
||
8 years ago
|
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
|
||
10 years ago
|
}
|