mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-21 11:48:53 +00:00
Merge pull request #1194 from metalmatze/statictargets-fix
Fix StaticTargets generation and add unit test
This commit is contained in:
commit
7c84fdb17d
6 changed files with 85 additions and 10 deletions
Documentation
example/prometheus-operator-crd
pkg
|
@ -152,7 +152,7 @@ Endpoint defines a scrapeable endpoint serving Prometheus metrics.
|
|||
| honorLabels | HonorLabels chooses the metric's labels on collisions with target labels. | bool | false |
|
||||
| basicAuth | BasicAuth allow an endpoint to authenticate over basic authentication More info: https://prometheus.io/docs/operating/configuration/#endpoints | *[BasicAuth](#basicauth) | false |
|
||||
| metricRelabelings | MetricRelabelConfigs to apply to samples before ingestion. | []*[RelabelConfig](#relabelconfig) | false |
|
||||
| staticTargets | StaticTargets with targets to scrape. | []string | false |
|
||||
| staticTargets | StaticTargets with targets to scrape. This is an experimental feature, it may change in any upcoming release in a breaking way. | []string | false |
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
|
||||
|
|
|
@ -141,7 +141,9 @@ spec:
|
|||
description: Timeout after which the scrape is ended
|
||||
type: string
|
||||
staticTargets:
|
||||
description: StaticTargets with targets to scrape.
|
||||
description: StaticTargets with targets to scrape. This is an
|
||||
experimental feature, it may change in any upcoming release
|
||||
in a breaking way.
|
||||
items:
|
||||
type: string
|
||||
type: array
|
||||
|
|
|
@ -541,7 +541,7 @@ func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenA
|
|||
},
|
||||
"staticTargets": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "StaticTargets with targets to scrape.",
|
||||
Description: "StaticTargets with targets to scrape. This is an experimental feature, it may change in any upcoming release in a breaking way.",
|
||||
Type: []string{"array"},
|
||||
Items: &spec.SchemaOrArray{
|
||||
Schema: &spec.Schema{
|
||||
|
|
|
@ -332,7 +332,7 @@ type Endpoint struct {
|
|||
BasicAuth *BasicAuth `json:"basicAuth,omitempty"`
|
||||
// MetricRelabelConfigs to apply to samples before ingestion.
|
||||
MetricRelabelConfigs []*RelabelConfig `json:"metricRelabelings,omitempty"`
|
||||
// StaticTargets with targets to scrape.
|
||||
// StaticTargets with targets to scrape. This is an experimental feature, it may change in any upcoming release in a breaking way.
|
||||
StaticTargets []string `json:"staticTargets,omitempty"`
|
||||
}
|
||||
|
||||
|
|
|
@ -214,9 +214,8 @@ func generateServiceMonitorConfig(version semver.Version, m *v1.ServiceMonitor,
|
|||
if ep.StaticTargets != nil {
|
||||
cfg = append(cfg, yaml.MapItem{
|
||||
Key: "static_configs",
|
||||
Value: yaml.MapItem{
|
||||
Key: "targets",
|
||||
Value: ep.StaticTargets,
|
||||
Value: []yaml.MapSlice{
|
||||
yaml.MapSlice{{Key: "targets", Value: ep.StaticTargets}},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -18,13 +18,13 @@ import (
|
|||
"bytes"
|
||||
"testing"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
"github.com/blang/semver"
|
||||
monitoringv1 "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1"
|
||||
"gopkg.in/yaml.v2"
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
|
||||
monitoringv1 "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1"
|
||||
)
|
||||
|
||||
func TestConfigGeneration(t *testing.T) {
|
||||
|
@ -147,6 +147,80 @@ alerting:
|
|||
}
|
||||
}
|
||||
|
||||
func TestStaticTargets(t *testing.T) {
|
||||
ep := monitoringv1.Endpoint{
|
||||
Port: "web",
|
||||
Interval: "30s",
|
||||
Path: "/federate",
|
||||
HonorLabels: true,
|
||||
Params: map[string][]string{"metrics[]": {"{__name__=~\"job:.*\"}"}},
|
||||
StaticTargets: []string{
|
||||
"source-prometheus-1:9090",
|
||||
"source-prometheus-2:9090",
|
||||
"source-prometheus-3:9090",
|
||||
},
|
||||
}
|
||||
|
||||
sm := &monitoringv1.ServiceMonitor{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "statictargetsmonitor",
|
||||
Namespace: "default",
|
||||
Labels: map[string]string{
|
||||
"group": "group1",
|
||||
},
|
||||
},
|
||||
Spec: monitoringv1.ServiceMonitorSpec{
|
||||
Endpoints: []monitoringv1.Endpoint{ep},
|
||||
},
|
||||
}
|
||||
|
||||
smConfig := generateServiceMonitorConfig(semver.Version{}, sm, ep, 0, nil)
|
||||
s, err := yaml.Marshal(smConfig)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := `job_name: default/statictargetsmonitor/0
|
||||
honor_labels: true
|
||||
scrape_interval: 30s
|
||||
metrics_path: /federate
|
||||
params:
|
||||
metrics[]:
|
||||
- '{__name__=~"job:.*"}'
|
||||
static_configs:
|
||||
- targets:
|
||||
- source-prometheus-1:9090
|
||||
- source-prometheus-2:9090
|
||||
- source-prometheus-3:9090
|
||||
relabel_configs:
|
||||
- action: keep
|
||||
source_labels:
|
||||
- __meta_kubernetes_endpoint_port_name
|
||||
regex: web
|
||||
- source_labels:
|
||||
- __meta_kubernetes_namespace
|
||||
target_label: namespace
|
||||
- source_labels:
|
||||
- __meta_kubernetes_pod_name
|
||||
target_label: pod
|
||||
- source_labels:
|
||||
- __meta_kubernetes_service_name
|
||||
target_label: service
|
||||
- source_labels:
|
||||
- __meta_kubernetes_service_name
|
||||
target_label: job
|
||||
replacement: ${1}
|
||||
- target_label: endpoint
|
||||
replacement: web
|
||||
`
|
||||
|
||||
result := string(s)
|
||||
|
||||
if expected != result {
|
||||
t.Fatalf("Unexpected result.\n\nGot:\n\n%s\n\nExpected:\n\n%s\n\n", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func generateTestConfig(version string) ([]byte, error) {
|
||||
replicas := int32(1)
|
||||
return generateConfig(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue