Skip to content

Commit e2652bc

Browse files
target-allocator: Add support for scrape-classes
Signed-off-by: Christian Ciach <christian.ciach@gmail.com>
1 parent c5dd81b commit e2652bc

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

cmd/otel-allocator/internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type PrometheusCRConfig struct {
8989
ProbeNamespaceSelector *metav1.LabelSelector `yaml:"probe_namespace_selector,omitempty"`
9090
ScrapeProtocols []monitoringv1.ScrapeProtocol `yaml:"scrape_protocols,omitempty"`
9191
ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
92+
ScrapeClasses []monitoringv1.ScrapeClass `yaml:"scrape_classes,omitempty"`
9293
}
9394

9495
type HTTPSServerConfig struct {

cmd/otel-allocator/internal/watcher/promOperator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func NewPrometheusCRWatcher(
8484
ServiceDiscoveryRole: &serviceDiscoveryRole,
8585
Version: "2.55.1", // fix Prometheus version 2 to avoid generating incompatible config
8686
ScrapeProtocols: cfg.PrometheusCR.ScrapeProtocols,
87+
ScrapeClasses: cfg.PrometheusCR.ScrapeClasses,
8788
},
8889
EvaluationInterval: monitoringv1.Duration("30s"),
8990
},

cmd/otel-allocator/internal/watcher/promOperator_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func TestLoadConfig(t *testing.T) {
4444
name string
4545
serviceMonitors []*monitoringv1.ServiceMonitor
4646
podMonitors []*monitoringv1.PodMonitor
47+
scrapeClasses []*monitoringv1.ScrapeClass
4748
scrapeConfigs []*promv1alpha1.ScrapeConfig
4849
probes []*monitoringv1.Probe
4950
want *promconfig.Config
@@ -84,6 +85,7 @@ func TestLoadConfig(t *testing.T) {
8485
},
8586
},
8687
},
88+
8789
cfg: allocatorconfig.Config{
8890
PrometheusCR: allocatorconfig.PrometheusCRConfig{
8991
ServiceMonitorSelector: &metav1.LabelSelector{},
@@ -919,6 +921,68 @@ func TestLoadConfig(t *testing.T) {
919921
},
920922
},
921923
},
924+
{
925+
name: "pod selector with scrape class",
926+
podMonitors: []*monitoringv1.PodMonitor{
927+
{
928+
ObjectMeta: metav1.ObjectMeta{
929+
Name: "simple",
930+
Namespace: namespace,
931+
},
932+
Spec: monitoringv1.PodMonitorSpec{
933+
JobLabel: "test",
934+
ScrapeClassName: ptr.To("attach-node-metadata"),
935+
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
936+
{
937+
Port: &portName,
938+
},
939+
},
940+
},
941+
},
942+
},
943+
cfg: allocatorconfig.Config{
944+
PrometheusCR: allocatorconfig.PrometheusCRConfig{
945+
PodMonitorSelector: &metav1.LabelSelector{},
946+
ScrapeClasses: []monitoringv1.ScrapeClass{
947+
{
948+
Name: "attach-node-metadata",
949+
AttachMetadata: &monitoringv1.AttachMetadata{
950+
Node: ptr.To(true),
951+
},
952+
},
953+
},
954+
},
955+
},
956+
want: &promconfig.Config{
957+
ScrapeConfigs: []*promconfig.ScrapeConfig{
958+
{
959+
JobName: "podMonitor/test/simple/0",
960+
ScrapeInterval: model.Duration(30 * time.Second),
961+
ScrapeProtocols: promconfig.DefaultScrapeProtocols,
962+
ScrapeTimeout: model.Duration(10 * time.Second),
963+
HonorTimestamps: true,
964+
HonorLabels: false,
965+
Scheme: "http",
966+
MetricsPath: "/metrics",
967+
ServiceDiscoveryConfigs: []discovery.Config{
968+
&kubeDiscovery.SDConfig{
969+
Role: "pod",
970+
NamespaceDiscovery: kubeDiscovery.NamespaceDiscovery{
971+
Names: []string{namespace},
972+
IncludeOwnNamespace: false,
973+
},
974+
HTTPClientConfig: config.DefaultHTTPClientConfig,
975+
AttachMetadata: kubeDiscovery.AttachMetadataConfig{
976+
Node: true, // Added by scrape-class!
977+
},
978+
},
979+
},
980+
HTTPClientConfig: config.DefaultHTTPClientConfig,
981+
EnableCompression: true,
982+
},
983+
},
984+
},
985+
},
922986
}
923987
for _, tt := range tests {
924988
t.Run(tt.name, func(t *testing.T) {
@@ -1244,6 +1308,7 @@ func getTestPrometheusCRWatcher(
12441308
ProbeNamespaceSelector: cfg.PrometheusCR.ProbeNamespaceSelector,
12451309
ScrapeConfigSelector: cfg.PrometheusCR.ScrapeConfigSelector,
12461310
ScrapeConfigNamespaceSelector: cfg.PrometheusCR.ScrapeConfigNamespaceSelector,
1311+
ScrapeClasses: cfg.PrometheusCR.ScrapeClasses,
12471312
ServiceDiscoveryRole: &serviceDiscoveryRole,
12481313
Version: "2.55.1",
12491314
},

0 commit comments

Comments
 (0)