Skip to content

Commit

Permalink
Add PuppetDB service discovery
Browse files Browse the repository at this point in the history
We have been Puppet user for 10 years and we are users of
https://github.com/camptocamp/prometheus-puppetdb-sd

However, that file_sd implementation contains business logic and
assumptions around e.g. the modules which you are using.

This pull request adds a simple PuppetDB service discovery, which will
enable more use cases than the upstream sd.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
  • Loading branch information
roidelapluie committed Sep 16, 2021
1 parent 447d940 commit 8920024
Show file tree
Hide file tree
Showing 13 changed files with 786 additions and 0 deletions.
45 changes: 45 additions & 0 deletions config/config_test.go
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/prometheus/prometheus/discovery/marathon"
"github.com/prometheus/prometheus/discovery/moby"
"github.com/prometheus/prometheus/discovery/openstack"
"github.com/prometheus/prometheus/discovery/puppetdb"
"github.com/prometheus/prometheus/discovery/scaleway"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/discovery/triton"
Expand Down Expand Up @@ -790,6 +791,34 @@ var expectedConf = &Config{
}},
},
},
{
JobName: "service-puppetdb",

HonorTimestamps: true,
ScrapeInterval: model.Duration(15 * time.Second),
ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout,

MetricsPath: DefaultScrapeConfig.MetricsPath,
Scheme: DefaultScrapeConfig.Scheme,
HTTPClientConfig: config.DefaultHTTPClientConfig,

ServiceDiscoveryConfigs: discovery.Configs{&puppetdb.SDConfig{
URL: "https://puppetserver/",
Query: "resources { type = \"Package\" and title = \"httpd\" }",
IncludeParameters: true,
Port: 80,
RefreshInterval: model.Duration(60 * time.Second),
HTTPClientConfig: config.HTTPClientConfig{
FollowRedirects: true,
TLSConfig: config.TLSConfig{
CAFile: "testdata/valid_ca_file",
CertFile: "testdata/valid_cert_file",
KeyFile: "testdata/valid_key_file",
},
},
},
},
},
{
JobName: "hetzner",
HonorTimestamps: true,
Expand Down Expand Up @@ -1262,6 +1291,22 @@ var expectedErrors = []struct {
filename: "empty_static_config.bad.yml",
errMsg: "empty or null section in static_configs",
},
{
filename: "puppetdb_no_query.bad.yml",
errMsg: "query missing",
},
{
filename: "puppetdb_no_url.bad.yml",
errMsg: "url missing",
},
{
filename: "puppetdb_bad_url.bad.yml",
errMsg: "host is missing in URL",
},
{
filename: "puppetdb_no_scheme.bad.yml",
errMsg: "url scheme must be http or https",
},
{
filename: "hetzner_role.bad.yml",
errMsg: "unknown role",
Expand Down
12 changes: 12 additions & 0 deletions config/testdata/conf.good.yml
Expand Up @@ -307,6 +307,18 @@ scrape_configs:
cert_file: valid_cert_file
key_file: valid_key_file

- job_name: service-puppetdb
puppetdb_sd_configs:
- url: https://puppetserver/
query: 'resources { type = "Package" and title = "httpd" }'
include_parameters: true
port: 80
refresh_interval: 1m
tls_config:
ca_file: valid_ca_file
cert_file: valid_cert_file
key_file: valid_key_file

- job_name: hetzner
hetzner_sd_configs:
- role: hcloud
Expand Down
4 changes: 4 additions & 0 deletions config/testdata/puppetdb_bad_url.bad.yml
@@ -0,0 +1,4 @@
scrape_configs:
- puppetdb_sd_configs:
- url: http://
query: 'resources { type = "Package" and title = "httpd" }'
3 changes: 3 additions & 0 deletions config/testdata/puppetdb_no_query.bad.yml
@@ -0,0 +1,3 @@
scrape_configs:
- puppetdb_sd_configs:
- url: http://puppetserver/
4 changes: 4 additions & 0 deletions config/testdata/puppetdb_no_scheme.bad.yml
@@ -0,0 +1,4 @@
scrape_configs:
- puppetdb_sd_configs:
- url: ftp://puppet
query: 'resources { type = "Package" and title = "httpd" }'
3 changes: 3 additions & 0 deletions config/testdata/puppetdb_no_url.bad.yml
@@ -0,0 +1,3 @@
scrape_configs:
- puppetdb_sd_configs:
- query: 'resources { type = "Package" and title = "httpd" }'
1 change: 1 addition & 0 deletions discovery/install/install.go
Expand Up @@ -31,6 +31,7 @@ import (
_ "github.com/prometheus/prometheus/discovery/marathon" // register marathon
_ "github.com/prometheus/prometheus/discovery/moby" // register moby
_ "github.com/prometheus/prometheus/discovery/openstack" // register openstack
_ "github.com/prometheus/prometheus/discovery/puppetdb" // register puppetdb
_ "github.com/prometheus/prometheus/discovery/scaleway" // register scaleway
_ "github.com/prometheus/prometheus/discovery/triton" // register triton
_ "github.com/prometheus/prometheus/discovery/xds" // register xds
Expand Down
49 changes: 49 additions & 0 deletions discovery/puppetdb/fixtures/vhosts.json
@@ -0,0 +1,49 @@
[
{
"certname": "edinburgh.example.com",
"environment": "prod",
"exported": false,
"file": "/etc/puppetlabs/code/environments/prod/modules/upstream/apache/manifests/init.pp",
"line": 384,
"parameters": {
"access_log": true,
"access_log_file": "ssl_access_log",
"additional_includes": [ ],
"directoryindex": "",
"docroot": "/var/www/html",
"ensure": "absent",
"options": [
"Indexes",
"FollowSymLinks",
"MultiViews"
],
"php_flags": { },
"labels": {
"alias": "edinburgh"
},
"scriptaliases": [
{
"alias": "/cgi-bin",
"path": "/var/www/cgi-bin"
}
]
},
"resource": "49af83866dc5a1518968b68e58a25319107afe11",
"tags": [
"roles::hypervisor",
"apache",
"apache::vhost",
"class",
"default-ssl",
"profile_hypervisor",
"vhost",
"profile_apache",
"hypervisor",
"__node_regexp__edinburgh",
"roles",
"node"
],
"title": "default-ssl",
"type": "Apache::Vhost"
}
]

0 comments on commit 8920024

Please sign in to comment.