2021-06-01 12:04:58 +00:00
|
|
|
// Copyright 2021 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 puppetdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-11-03 12:15:51 +00:00
|
|
|
"errors"
|
2021-06-01 12:04:58 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-09-10 01:41:53 +00:00
|
|
|
"log/slog"
|
2021-06-01 12:04:58 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-02-12 23:58:27 +00:00
|
|
|
"github.com/grafana/regexp"
|
2023-10-23 13:55:36 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-06-01 12:04:58 +00:00
|
|
|
"github.com/prometheus/common/config"
|
|
|
|
"github.com/prometheus/common/model"
|
2024-09-10 01:41:53 +00:00
|
|
|
"github.com/prometheus/common/promslog"
|
2021-06-01 12:04:58 +00:00
|
|
|
"github.com/prometheus/common/version"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery"
|
|
|
|
"github.com/prometheus/prometheus/discovery/refresh"
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pdbLabel = model.MetaLabelPrefix + "puppetdb_"
|
2022-09-01 15:05:54 +00:00
|
|
|
pdbLabelQuery = pdbLabel + "query"
|
2021-06-01 12:04:58 +00:00
|
|
|
pdbLabelCertname = pdbLabel + "certname"
|
|
|
|
pdbLabelResource = pdbLabel + "resource"
|
|
|
|
pdbLabelType = pdbLabel + "type"
|
|
|
|
pdbLabelTitle = pdbLabel + "title"
|
|
|
|
pdbLabelExported = pdbLabel + "exported"
|
|
|
|
pdbLabelTags = pdbLabel + "tags"
|
|
|
|
pdbLabelFile = pdbLabel + "file"
|
|
|
|
pdbLabelEnvironment = pdbLabel + "environment"
|
|
|
|
pdbLabelParameter = pdbLabel + "parameter_"
|
|
|
|
separator = ","
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultSDConfig is the default PuppetDB SD configuration.
|
|
|
|
DefaultSDConfig = SDConfig{
|
|
|
|
RefreshInterval: model.Duration(60 * time.Second),
|
|
|
|
Port: 80,
|
|
|
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
|
|
|
}
|
|
|
|
matchContentType = regexp.MustCompile(`^(?i:application\/json(;\s*charset=("utf-8"|utf-8))?)$`)
|
|
|
|
userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
discovery.RegisterConfig(&SDConfig{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SDConfig is the configuration for PuppetDB based discovery.
|
|
|
|
type SDConfig struct {
|
|
|
|
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
|
|
|
|
URL string `yaml:"url"`
|
|
|
|
Query string `yaml:"query"`
|
|
|
|
IncludeParameters bool `yaml:"include_parameters"`
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
}
|
|
|
|
|
2024-01-23 15:53:55 +00:00
|
|
|
// NewDiscovererMetrics implements discovery.Config.
|
|
|
|
func (*SDConfig) NewDiscovererMetrics(reg prometheus.Registerer, rmi discovery.RefreshMetricsInstantiator) discovery.DiscovererMetrics {
|
|
|
|
return &puppetdbMetrics{
|
|
|
|
refreshMetrics: rmi,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 12:04:58 +00:00
|
|
|
// Name returns the name of the Config.
|
|
|
|
func (*SDConfig) Name() string { return "puppetdb" }
|
|
|
|
|
|
|
|
// NewDiscoverer returns a Discoverer for the Config.
|
|
|
|
func (c *SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
|
2024-01-23 15:53:55 +00:00
|
|
|
return NewDiscovery(c, opts.Logger, opts.Metrics)
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirectory joins any relative file paths with dir.
|
|
|
|
func (c *SDConfig) SetDirectory(dir string) {
|
|
|
|
c.HTTPClientConfig.SetDirectory(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = DefaultSDConfig
|
|
|
|
type plain SDConfig
|
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.URL == "" {
|
2024-11-03 12:15:51 +00:00
|
|
|
return errors.New("URL is missing")
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
|
|
|
parsedURL, err := url.Parse(c.URL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
|
2024-11-03 12:15:51 +00:00
|
|
|
return errors.New("URL scheme must be 'http' or 'https'")
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
|
|
|
if parsedURL.Host == "" {
|
2024-11-03 12:15:51 +00:00
|
|
|
return errors.New("host is missing in URL")
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
|
|
|
if c.Query == "" {
|
2024-11-03 12:15:51 +00:00
|
|
|
return errors.New("query missing")
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
2023-09-08 11:51:17 +00:00
|
|
|
return c.HTTPClientConfig.Validate()
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Discovery provides service discovery functionality based
|
|
|
|
// on PuppetDB resources.
|
|
|
|
type Discovery struct {
|
|
|
|
*refresh.Discovery
|
|
|
|
url string
|
|
|
|
query string
|
|
|
|
port int
|
|
|
|
includeParameters bool
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiscovery returns a new PuppetDB discovery for the given config.
|
2024-09-10 01:41:53 +00:00
|
|
|
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
|
2024-01-23 15:53:55 +00:00
|
|
|
m, ok := metrics.(*puppetdbMetrics)
|
|
|
|
if !ok {
|
2024-11-03 12:15:51 +00:00
|
|
|
return nil, errors.New("invalid discovery metrics type")
|
2024-01-23 15:53:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:04:58 +00:00
|
|
|
if logger == nil {
|
2024-09-10 01:41:53 +00:00
|
|
|
logger = promslog.NewNopLogger()
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 21:16:12 +00:00
|
|
|
client, err := config.NewClientFromConfig(conf.HTTPClientConfig, "http")
|
2021-06-01 12:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client.Timeout = time.Duration(conf.RefreshInterval)
|
|
|
|
|
|
|
|
u, err := url.Parse(conf.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u.Path = path.Join(u.Path, "pdb/query/v4")
|
|
|
|
|
|
|
|
d := &Discovery{
|
|
|
|
url: u.String(),
|
|
|
|
port: conf.Port,
|
|
|
|
query: conf.Query,
|
|
|
|
includeParameters: conf.IncludeParameters,
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Discovery = refresh.NewDiscovery(
|
2023-10-23 13:55:36 +00:00
|
|
|
refresh.Options{
|
2024-01-23 15:53:55 +00:00
|
|
|
Logger: logger,
|
2024-01-23 19:24:13 +00:00
|
|
|
Mech: "puppetdb",
|
2024-01-23 15:53:55 +00:00
|
|
|
Interval: time.Duration(conf.RefreshInterval),
|
|
|
|
RefreshF: d.refresh,
|
|
|
|
MetricsInstantiator: m.refreshMetrics,
|
2023-10-23 13:55:36 +00:00
|
|
|
},
|
2021-06-01 12:04:58 +00:00
|
|
|
)
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
|
|
|
body := struct {
|
|
|
|
Query string `json:"query"`
|
|
|
|
}{d.query}
|
|
|
|
bodyBytes, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-08 19:26:23 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, d.url, bytes.NewBuffer(bodyBytes))
|
2021-06-01 12:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", userAgent)
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := d.client.Do(req.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
2022-04-27 09:24:36 +00:00
|
|
|
io.Copy(io.Discard, resp.Body)
|
2021-06-01 12:04:58 +00:00
|
|
|
resp.Body.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2022-06-03 11:47:14 +00:00
|
|
|
return nil, fmt.Errorf("server returned HTTP status %s", resp.Status)
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ct := resp.Header.Get("Content-Type"); !matchContentType.MatchString(ct) {
|
2022-06-03 11:47:14 +00:00
|
|
|
return nil, fmt.Errorf("unsupported content type %s", resp.Header.Get("Content-Type"))
|
2021-06-01 12:04:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 09:24:36 +00:00
|
|
|
b, err := io.ReadAll(resp.Body)
|
2021-06-01 12:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resources []Resource
|
|
|
|
|
|
|
|
if err := json.Unmarshal(b, &resources); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tg := &targetgroup.Group{
|
|
|
|
// Use a pseudo-URL as source.
|
|
|
|
Source: d.url + "?query=" + d.query,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resource := range resources {
|
|
|
|
labels := model.LabelSet{
|
2022-09-01 15:05:54 +00:00
|
|
|
pdbLabelQuery: model.LabelValue(d.query),
|
2021-06-01 12:04:58 +00:00
|
|
|
pdbLabelCertname: model.LabelValue(resource.Certname),
|
|
|
|
pdbLabelResource: model.LabelValue(resource.Resource),
|
|
|
|
pdbLabelType: model.LabelValue(resource.Type),
|
|
|
|
pdbLabelTitle: model.LabelValue(resource.Title),
|
2024-05-13 15:36:19 +00:00
|
|
|
pdbLabelExported: model.LabelValue(strconv.FormatBool(resource.Exported)),
|
2021-06-01 12:04:58 +00:00
|
|
|
pdbLabelFile: model.LabelValue(resource.File),
|
|
|
|
pdbLabelEnvironment: model.LabelValue(resource.Environment),
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := net.JoinHostPort(resource.Certname, strconv.FormatUint(uint64(d.port), 10))
|
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
|
|
|
|
|
|
|
if len(resource.Tags) > 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.
|
|
|
|
tags := separator + strings.Join(resource.Tags, separator) + separator
|
|
|
|
labels[pdbLabelTags] = model.LabelValue(tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parameters are not included by default. This should only be enabled
|
|
|
|
// on select resources as it might expose secrets on the Prometheus UI
|
|
|
|
// for certain resources.
|
|
|
|
if d.includeParameters {
|
|
|
|
for k, v := range resource.Parameters.toLabels() {
|
|
|
|
labels[pdbLabelParameter+k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tg.Targets = append(tg.Targets, labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*targetgroup.Group{tg}, nil
|
|
|
|
}
|