2018-05-30 12:14:34 +00:00
|
|
|
// Copyright 2018 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 adapter
|
|
|
|
|
|
|
|
// NOTE: you do not need to edit this file when implementing a custom sd.
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-09-16 21:39:03 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2019-08-22 09:49:45 +00:00
|
|
|
"sort"
|
2018-09-18 09:08:38 +00:00
|
|
|
|
2021-06-11 16:17:59 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2018-11-30 09:32:17 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-22 09:00:08 +00:00
|
|
|
|
2018-09-18 09:08:38 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2018-05-30 12:14:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type customSD struct {
|
|
|
|
Targets []string `json:"targets"`
|
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
}
|
|
|
|
|
2018-11-30 09:32:17 +00:00
|
|
|
func fingerprint(group *targetgroup.Group) model.Fingerprint {
|
|
|
|
groupFingerprint := model.LabelSet{}.Fingerprint()
|
|
|
|
for _, targets := range group.Targets {
|
|
|
|
groupFingerprint ^= targets.Fingerprint()
|
|
|
|
}
|
|
|
|
groupFingerprint ^= group.Labels.Fingerprint()
|
|
|
|
return groupFingerprint
|
|
|
|
}
|
|
|
|
|
2018-05-30 12:14:34 +00:00
|
|
|
// Adapter runs an unknown service discovery implementation and converts its target groups
|
|
|
|
// to JSON and writes to a file for file_sd.
|
|
|
|
type Adapter struct {
|
|
|
|
ctx context.Context
|
|
|
|
disc discovery.Discoverer
|
|
|
|
groups map[string]*customSD
|
|
|
|
manager *discovery.Manager
|
|
|
|
output string
|
|
|
|
name string
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapToArray(m map[string]*customSD) []customSD {
|
|
|
|
arr := make([]customSD, 0, len(m))
|
|
|
|
for _, v := range m {
|
|
|
|
arr = append(arr, *v)
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2018-09-16 21:39:03 +00:00
|
|
|
func generateTargetGroups(allTargetGroups map[string][]*targetgroup.Group) map[string]*customSD {
|
|
|
|
groups := make(map[string]*customSD)
|
2018-05-30 12:14:34 +00:00
|
|
|
for k, sdTargetGroups := range allTargetGroups {
|
2018-11-30 09:32:17 +00:00
|
|
|
for _, group := range sdTargetGroups {
|
2018-05-30 12:14:34 +00:00
|
|
|
newTargets := make([]string, 0)
|
|
|
|
newLabels := make(map[string]string)
|
|
|
|
|
|
|
|
for _, targets := range group.Targets {
|
|
|
|
for _, target := range targets {
|
|
|
|
newTargets = append(newTargets, string(target))
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 09:49:45 +00:00
|
|
|
sort.Strings(newTargets)
|
2018-05-30 12:14:34 +00:00
|
|
|
for name, value := range group.Labels {
|
|
|
|
newLabels[string(name)] = string(value)
|
|
|
|
}
|
2018-11-30 09:32:17 +00:00
|
|
|
|
|
|
|
sdGroup := customSD{
|
2018-05-30 12:14:34 +00:00
|
|
|
Targets: newTargets,
|
|
|
|
Labels: newLabels,
|
|
|
|
}
|
2018-11-30 09:32:17 +00:00
|
|
|
// Make a unique key, including group's fingerprint, in case the sd_type (map key) and group.Source is not unique.
|
|
|
|
groupFingerprint := fingerprint(group)
|
|
|
|
key := fmt.Sprintf("%s:%s:%s", k, group.Source, groupFingerprint.String())
|
|
|
|
groups[key] = &sdGroup
|
2018-05-30 12:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-16 21:39:03 +00:00
|
|
|
|
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses incoming target groups updates. If the update contains changes to the target groups
|
|
|
|
// Adapter already knows about, or new target groups, we Marshal to JSON and write to file.
|
|
|
|
func (a *Adapter) refreshTargetGroups(allTargetGroups map[string][]*targetgroup.Group) {
|
|
|
|
tempGroups := generateTargetGroups(allTargetGroups)
|
|
|
|
|
2018-05-30 12:14:34 +00:00
|
|
|
if !reflect.DeepEqual(a.groups, tempGroups) {
|
|
|
|
a.groups = tempGroups
|
|
|
|
err := a.writeOutput()
|
|
|
|
if err != nil {
|
|
|
|
level.Error(log.With(a.logger, "component", "sd-adapter")).Log("err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes JSON formatted targets to output file.
|
|
|
|
func (a *Adapter) writeOutput() error {
|
|
|
|
arr := mapToArray(a.groups)
|
|
|
|
b, _ := json.MarshalIndent(arr, "", " ")
|
|
|
|
|
|
|
|
dir, _ := filepath.Split(a.output)
|
2022-04-27 09:24:36 +00:00
|
|
|
tmpfile, err := os.CreateTemp(dir, "sd-adapter")
|
2018-05-30 12:14:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tmpfile.Close()
|
|
|
|
|
|
|
|
_, err = tmpfile.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-29 09:41:31 +00:00
|
|
|
// Close the file immediately for platforms (eg. Windows) that cannot move
|
|
|
|
// a file while a process is holding a file handle.
|
|
|
|
tmpfile.Close()
|
2018-05-30 12:14:34 +00:00
|
|
|
err = os.Rename(tmpfile.Name(), a.output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adapter) runCustomSD(ctx context.Context) {
|
|
|
|
updates := a.manager.SyncCh()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case allTargetGroups, ok := <-updates:
|
|
|
|
// Handle the case that a target provider exits and closes the channel
|
|
|
|
// before the context is done.
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2018-09-16 21:39:03 +00:00
|
|
|
a.refreshTargetGroups(allTargetGroups)
|
2018-05-30 12:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts a Discovery Manager and the custom service discovery implementation.
|
|
|
|
func (a *Adapter) Run() {
|
|
|
|
go a.manager.Run()
|
|
|
|
a.manager.StartCustomProvider(a.ctx, a.name, a.disc)
|
|
|
|
go a.runCustomSD(a.ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdapter creates a new instance of Adapter.
|
2021-10-22 08:06:44 +00:00
|
|
|
func NewAdapter(ctx context.Context, file, name string, d discovery.Discoverer, logger log.Logger) *Adapter {
|
2018-05-30 12:14:34 +00:00
|
|
|
return &Adapter{
|
|
|
|
ctx: ctx,
|
|
|
|
disc: d,
|
|
|
|
groups: make(map[string]*customSD),
|
|
|
|
manager: discovery.NewManager(ctx, logger),
|
|
|
|
output: file,
|
|
|
|
name: name,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|