Repository: open-telemetry/opentelemetry-operator
Pull request #4000 (2025-05-15 21:23:56)

Allow to configure ScraperProtocols in prometheus common config

bogdandrutu: @swiatekm @iblancasa this PR is now ready to final review and merge, thank you.

Patch:
diff --git a/.chloggen/allow-config.yaml b/.chloggen/allow-config.yaml
new file mode 100755
index 00000000..4983bd9e
--- /dev/null
+++ b/.chloggen/allow-config.yaml
@@ -0,0 +1,16 @@
+# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
+change_type: enhancement
+
+# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
+component: otel-allocator
+
+# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
+note: Allow to configure ScraperProtocols in prometheus common config
+
+# One or more tracking issues related to the change
+issues: [4000]
+
+# (Optional) One or more lines of additional information to render under the primary note.
+# These lines will be padded with 2 spaces and then inserted directly into the document.
+# Use pipe (|) for multiline entries.
+subtext:
diff --git a/cmd/otel-allocator/internal/config/config.go b/cmd/otel-allocator/internal/config/config.go
index aa4fafcf..9cd974cf 100644
--- a/cmd/otel-allocator/internal/config/config.go
+++ b/cmd/otel-allocator/internal/config/config.go
@@ -18,6 +18,7 @@ import (
 
 	"github.com/go-logr/logr"
 	"github.com/go-viper/mapstructure/v2"
+	monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
 	"github.com/prometheus/common/model"
 	promconfig "github.com/prometheus/prometheus/config"
 	_ "github.com/prometheus/prometheus/discovery/install"
@@ -48,6 +49,13 @@ var (
 	DefaultKubeConfigFilePath string = filepath.Join(homedir.HomeDir(), ".kube", "config")
 )
 
+var defaultScrapeProtocolsCR = []monitoringv1.ScrapeProtocol{
+	monitoringv1.OpenMetricsText1_0_0,
+	monitoringv1.OpenMetricsText0_0_1,
+	monitoringv1.PrometheusText1_0_0,
+	monitoringv1.PrometheusText0_0_4,
+}
+
 // logger which discards all messages written to it. Replace this with slog.DiscardHandler after we require Go 1.24.
 var NopLogger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.Level(math.MaxInt)}))
 
@@ -68,18 +76,19 @@ type Config struct {
 }
 
 type PrometheusCRConfig struct {
-	Enabled                         bool                  `yaml:"enabled,omitempty"`
-	AllowNamespaces                 []string              `yaml:"allow_namespaces,omitempty"`
-	DenyNamespaces                  []string              `yaml:"deny_namespaces,omitempty"`
-	PodMonitorSelector              *metav1.LabelSelector `yaml:"pod_monitor_selector,omitempty"`
-	PodMonitorNamespaceSelector     *metav1.LabelSelector `yaml:"pod_monitor_namespace_selector,omitempty"`
-	ServiceMonitorSelector          *metav1.LabelSelector `yaml:"service_monitor_selector,omitempty"`
-	ServiceMonitorNamespaceSelector *metav1.LabelSelector `yaml:"service_monitor_namespace_selector,omitempty"`
-	ScrapeConfigSelector            *metav1.LabelSelector `yaml:"scrape_config_selector,omitempty"`
-	ScrapeConfigNamespaceSelector   *metav1.LabelSelector `yaml:"scrape_config_namespace_selector,omitempty"`
-	ProbeSelector                   *metav1.LabelSelector `yaml:"probe_selector,omitempty"`
-	ProbeNamespaceSelector          *metav1.LabelSelector `yaml:"probe_namespace_selector,omitempty"`
-	ScrapeInterval                  model.Duration        `yaml:"scrape_interval,omitempty"`
+	Enabled                         bool                          `yaml:"enabled,omitempty"`
+	AllowNamespaces                 []string                      `yaml:"allow_namespaces,omitempty"`
+	DenyNamespaces                  []string                      `yaml:"deny_namespaces,omitempty"`
+	PodMonitorSelector              *metav1.LabelSelector         `yaml:"pod_monitor_selector,omitempty"`
+	PodMonitorNamespaceSelector     *metav1.LabelSelector         `yaml:"pod_monitor_namespace_selector,omitempty"`
+	ServiceMonitorSelector          *metav1.LabelSelector         `yaml:"service_monitor_selector,omitempty"`
+	ServiceMonitorNamespaceSelector *metav1.LabelSelector         `yaml:"service_monitor_namespace_selector,omitempty"`
+	ScrapeConfigSelector            *metav1.LabelSelector         `yaml:"scrape_config_selector,omitempty"`
+	ScrapeConfigNamespaceSelector   *metav1.LabelSelector         `yaml:"scrape_config_namespace_selector,omitempty"`
+	ProbeSelector                   *metav1.LabelSelector         `yaml:"probe_selector,omitempty"`
+	ProbeNamespaceSelector          *metav1.LabelSelector         `yaml:"probe_namespace_selector,omitempty"`
+	ScrapeProtocols                 []monitoringv1.ScrapeProtocol `yaml:"scrape_protocols,omitempty"`
+	ScrapeInterval                  model.Duration                `yaml:"scrape_interval,omitempty"`
 }
 
 type HTTPSServerConfig struct {
@@ -322,6 +331,7 @@ func CreateDefaultConfig() Config {
 			PodMonitorNamespaceSelector:     &metav1.LabelSelector{},
 			ScrapeConfigNamespaceSelector:   &metav1.LabelSelector{},
 			ProbeNamespaceSelector:          &metav1.LabelSelector{},
+			ScrapeProtocols:                 defaultScrapeProtocolsCR,
 		},
 		CollectorNotReadyGracePeriod: DefaultCollectorNotReadyGracePeriod,
 	}
diff --git a/cmd/otel-allocator/internal/watcher/promOperator.go b/cmd/otel-allocator/internal/watcher/promOperator.go
index 96a3cbae..4ea16f7b 100644
--- a/cmd/otel-allocator/internal/watcher/promOperator.go
+++ b/cmd/otel-allocator/internal/watcher/promOperator.go
@@ -39,12 +39,6 @@ const (
 	minEventInterval = time.Second * 5
 )
 
-var DefaultScrapeProtocols = []monitoringv1.ScrapeProtocol{
-	monitoringv1.OpenMetricsText1_0_0,
-	monitoringv1.OpenMetricsText0_0_1,
-	monitoringv1.PrometheusText0_0_4,
-}
-
 func NewPrometheusCRWatcher(ctx context.Context, logger logr.Logger, cfg allocatorconfig.Config) (*PrometheusCRWatcher, error) {
 	promLogger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn}))
 	slogger := slog.New(logr.ToSlogHandler(logger))
@@ -89,7 +83,7 @@ func NewPrometheusCRWatcher(ctx context.Context, logger logr.Logger, cfg allocat
 				ProbeNamespaceSelector:          cfg.PrometheusCR.ProbeNamespaceSelector,
 				ServiceDiscoveryRole:            &serviceDiscoveryRole,
 				Version:                         "2.55.1", // fix Prometheus version 2 to avoid generating incompatible config
-				ScrapeProtocols:                 DefaultScrapeProtocols,
+				ScrapeProtocols:                 cfg.PrometheusCR.ScrapeProtocols,
 			},
 			EvaluationInterval: monitoringv1.Duration("30s"),
 		},


Test patch:
diff --git a/cmd/otel-allocator/internal/config/config_test.go b/cmd/otel-allocator/internal/config/config_test.go
index 2925d6a9..381f9070 100644
--- a/cmd/otel-allocator/internal/config/config_test.go
+++ b/cmd/otel-allocator/internal/config/config_test.go
@@ -55,6 +55,7 @@ func TestLoadFromFile(t *testing.T) {
 					PodMonitorNamespaceSelector:     &metav1.LabelSelector{},
 					ScrapeConfigNamespaceSelector:   &metav1.LabelSelector{},
 					ProbeNamespaceSelector:          &metav1.LabelSelector{},
+					ScrapeProtocols:                 defaultScrapeProtocolsCR,
 				},
 				CollectorNotReadyGracePeriod: 30 * time.Second,
 				HTTPS: HTTPSServerConfig{
@@ -135,6 +136,7 @@ func TestLoadFromFile(t *testing.T) {
 					PodMonitorNamespaceSelector:     &metav1.LabelSelector{},
 					ScrapeConfigNamespaceSelector:   &metav1.LabelSelector{},
 					ProbeNamespaceSelector:          &metav1.LabelSelector{},
+					ScrapeProtocols:                 defaultScrapeProtocolsCR,
 				},
 				CollectorNotReadyGracePeriod: 30 * time.Second,
 				HTTPS: HTTPSServerConfig{
@@ -231,6 +233,7 @@ func TestLoadFromFile(t *testing.T) {
 					PodMonitorNamespaceSelector:     &metav1.LabelSelector{},
 					ScrapeConfigNamespaceSelector:   &metav1.LabelSelector{},
 					ProbeNamespaceSelector:          &metav1.LabelSelector{},
+					ScrapeProtocols:                 defaultScrapeProtocolsCR,
 					ScrapeInterval:                  DefaultCRScrapeInterval,
 				},
 				HTTPS: HTTPSServerConfig{
@@ -311,6 +314,7 @@ func TestLoadFromFile(t *testing.T) {
 					PodMonitorNamespaceSelector:     &metav1.LabelSelector{},
 					ScrapeConfigNamespaceSelector:   &metav1.LabelSelector{},
 					ProbeNamespaceSelector:          &metav1.LabelSelector{},
+					ScrapeProtocols:                 defaultScrapeProtocolsCR,
 					ScrapeInterval:                  DefaultCRScrapeInterval,
 				},
 				HTTPS: HTTPSServerConfig{
@@ -415,6 +419,7 @@ func TestLoadFromFile(t *testing.T) {
 					PodMonitorNamespaceSelector:     &metav1.LabelSelector{},
 					ScrapeConfigNamespaceSelector:   &metav1.LabelSelector{},
 					ProbeNamespaceSelector:          &metav1.LabelSelector{},
+					ScrapeProtocols:                 defaultScrapeProtocolsCR,
 					ScrapeInterval:                  DefaultCRScrapeInterval,
 				},
 				HTTPS: HTTPSServerConfig{
@@ -519,6 +524,7 @@ func TestLoadFromFile(t *testing.T) {
 					PodMonitorNamespaceSelector:     &metav1.LabelSelector{},
 					ScrapeConfigNamespaceSelector:   &metav1.LabelSelector{},
 					ProbeNamespaceSelector:          &metav1.LabelSelector{},
+					ScrapeProtocols:                 defaultScrapeProtocolsCR,
 					ScrapeInterval:                  DefaultCRScrapeInterval,
 				},
 				HTTPS: HTTPSServerConfig{