1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-16 01:06:27 +00:00

feat: add support --web.enable-otlp-receiver flag in the Prometheus/PrometheusAgent CRD (#7105)

* add otlp receiver

---------

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
Co-authored-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
dongjiang 2024-11-16 00:14:04 +08:00 committed by GitHub
parent 0b804775b8
commit 2d5b11ad21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 296 additions and 0 deletions

70
Documentation/api.md generated
View file

@ -1996,6 +1996,20 @@ For more information see <a href="https://prometheus.io/docs/prometheus/latest/q
</tr> </tr>
<tr> <tr>
<td> <td>
<code>enableOTLPReceiver</code><br/>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.</p>
<p>Note that the OTLP receiver endpoint is automatically enabled if <code>.spec.otlpConfig</code> is defined.</p>
<p>It requires Prometheus &gt;= v2.47.0.</p>
</td>
</tr>
<tr>
<td>
<code>remoteWriteReceiverMessageVersions</code><br/> <code>remoteWriteReceiverMessageVersions</code><br/>
<em> <em>
<a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion"> <a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion">
@ -6921,6 +6935,20 @@ For more information see <a href="https://prometheus.io/docs/prometheus/latest/q
</tr> </tr>
<tr> <tr>
<td> <td>
<code>enableOTLPReceiver</code><br/>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.</p>
<p>Note that the OTLP receiver endpoint is automatically enabled if <code>.spec.otlpConfig</code> is defined.</p>
<p>It requires Prometheus &gt;= v2.47.0.</p>
</td>
</tr>
<tr>
<td>
<code>remoteWriteReceiverMessageVersions</code><br/> <code>remoteWriteReceiverMessageVersions</code><br/>
<em> <em>
<a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion"> <a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion">
@ -11645,6 +11673,20 @@ For more information see <a href="https://prometheus.io/docs/prometheus/latest/q
</tr> </tr>
<tr> <tr>
<td> <td>
<code>enableOTLPReceiver</code><br/>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.</p>
<p>Note that the OTLP receiver endpoint is automatically enabled if <code>.spec.otlpConfig</code> is defined.</p>
<p>It requires Prometheus &gt;= v2.47.0.</p>
</td>
</tr>
<tr>
<td>
<code>remoteWriteReceiverMessageVersions</code><br/> <code>remoteWriteReceiverMessageVersions</code><br/>
<em> <em>
<a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion"> <a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion">
@ -18228,6 +18270,20 @@ For more information see <a href="https://prometheus.io/docs/prometheus/latest/q
</tr> </tr>
<tr> <tr>
<td> <td>
<code>enableOTLPReceiver</code><br/>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.</p>
<p>Note that the OTLP receiver endpoint is automatically enabled if <code>.spec.otlpConfig</code> is defined.</p>
<p>It requires Prometheus &gt;= v2.47.0.</p>
</td>
</tr>
<tr>
<td>
<code>remoteWriteReceiverMessageVersions</code><br/> <code>remoteWriteReceiverMessageVersions</code><br/>
<em> <em>
<a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion"> <a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion">
@ -26085,6 +26141,20 @@ For more information see <a href="https://prometheus.io/docs/prometheus/latest/q
</tr> </tr>
<tr> <tr>
<td> <td>
<code>enableOTLPReceiver</code><br/>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.</p>
<p>Note that the OTLP receiver endpoint is automatically enabled if <code>.spec.otlpConfig</code> is defined.</p>
<p>It requires Prometheus &gt;= v2.47.0.</p>
</td>
</tr>
<tr>
<td>
<code>remoteWriteReceiverMessageVersions</code><br/> <code>remoteWriteReceiverMessageVersions</code><br/>
<em> <em>
<a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion"> <a href="#monitoring.coreos.com/v1.RemoteWriteMessageVersion">

16
bundle.yaml generated
View file

@ -23853,6 +23853,14 @@ spec:
type: string type: string
type: array type: array
x-kubernetes-list-type: set x-kubernetes-list-type: set
enableOTLPReceiver:
description: |-
Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.
Note that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.
It requires Prometheus >= v2.47.0.
type: boolean
enableRemoteWriteReceiver: enableRemoteWriteReceiver:
description: |- description: |-
Enable Prometheus to be used as a receiver for the Prometheus remote Enable Prometheus to be used as a receiver for the Prometheus remote
@ -35181,6 +35189,14 @@ spec:
type: string type: string
type: array type: array
x-kubernetes-list-type: set x-kubernetes-list-type: set
enableOTLPReceiver:
description: |-
Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.
Note that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.
It requires Prometheus >= v2.47.0.
type: boolean
enableRemoteWriteReceiver: enableRemoteWriteReceiver:
description: |- description: |-
Enable Prometheus to be used as a receiver for the Prometheus remote Enable Prometheus to be used as a receiver for the Prometheus remote

View file

@ -2910,6 +2910,14 @@ spec:
type: string type: string
type: array type: array
x-kubernetes-list-type: set x-kubernetes-list-type: set
enableOTLPReceiver:
description: |-
Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.
Note that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.
It requires Prometheus >= v2.47.0.
type: boolean
enableRemoteWriteReceiver: enableRemoteWriteReceiver:
description: |- description: |-
Enable Prometheus to be used as a receiver for the Prometheus remote Enable Prometheus to be used as a receiver for the Prometheus remote

View file

@ -3605,6 +3605,14 @@ spec:
type: string type: string
type: array type: array
x-kubernetes-list-type: set x-kubernetes-list-type: set
enableOTLPReceiver:
description: |-
Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.
Note that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.
It requires Prometheus >= v2.47.0.
type: boolean
enableRemoteWriteReceiver: enableRemoteWriteReceiver:
description: |- description: |-
Enable Prometheus to be used as a receiver for the Prometheus remote Enable Prometheus to be used as a receiver for the Prometheus remote

View file

@ -2911,6 +2911,14 @@ spec:
type: string type: string
type: array type: array
x-kubernetes-list-type: set x-kubernetes-list-type: set
enableOTLPReceiver:
description: |-
Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.
Note that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.
It requires Prometheus >= v2.47.0.
type: boolean
enableRemoteWriteReceiver: enableRemoteWriteReceiver:
description: |- description: |-
Enable Prometheus to be used as a receiver for the Prometheus remote Enable Prometheus to be used as a receiver for the Prometheus remote

View file

@ -3606,6 +3606,14 @@ spec:
type: string type: string
type: array type: array
x-kubernetes-list-type: set x-kubernetes-list-type: set
enableOTLPReceiver:
description: |-
Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.
Note that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.
It requires Prometheus >= v2.47.0.
type: boolean
enableRemoteWriteReceiver: enableRemoteWriteReceiver:
description: |- description: |-
Enable Prometheus to be used as a receiver for the Prometheus remote Enable Prometheus to be used as a receiver for the Prometheus remote

View file

@ -2528,6 +2528,10 @@
"type": "array", "type": "array",
"x-kubernetes-list-type": "set" "x-kubernetes-list-type": "set"
}, },
"enableOTLPReceiver": {
"description": "Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.\n\nNote that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.\n\nIt requires Prometheus >= v2.47.0.",
"type": "boolean"
},
"enableRemoteWriteReceiver": { "enableRemoteWriteReceiver": {
"description": "Enable Prometheus to be used as a receiver for the Prometheus remote\nwrite protocol.\n\nWARNING: This is not considered an efficient way of ingesting samples.\nUse it with caution for specific low-volume use cases.\nIt is not suitable for replacing the ingestion via scraping and turning\nPrometheus into a push-based metrics collection system.\nFor more information see https://prometheus.io/docs/prometheus/latest/querying/api/#remote-write-receiver\n\nIt requires Prometheus >= v2.33.0.", "description": "Enable Prometheus to be used as a receiver for the Prometheus remote\nwrite protocol.\n\nWARNING: This is not considered an efficient way of ingesting samples.\nUse it with caution for specific low-volume use cases.\nIt is not suitable for replacing the ingestion via scraping and turning\nPrometheus into a push-based metrics collection system.\nFor more information see https://prometheus.io/docs/prometheus/latest/querying/api/#remote-write-receiver\n\nIt requires Prometheus >= v2.33.0.",
"type": "boolean" "type": "boolean"

View file

@ -3120,6 +3120,10 @@
"type": "array", "type": "array",
"x-kubernetes-list-type": "set" "x-kubernetes-list-type": "set"
}, },
"enableOTLPReceiver": {
"description": "Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.\n\nNote that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.\n\nIt requires Prometheus >= v2.47.0.",
"type": "boolean"
},
"enableRemoteWriteReceiver": { "enableRemoteWriteReceiver": {
"description": "Enable Prometheus to be used as a receiver for the Prometheus remote\nwrite protocol.\n\nWARNING: This is not considered an efficient way of ingesting samples.\nUse it with caution for specific low-volume use cases.\nIt is not suitable for replacing the ingestion via scraping and turning\nPrometheus into a push-based metrics collection system.\nFor more information see https://prometheus.io/docs/prometheus/latest/querying/api/#remote-write-receiver\n\nIt requires Prometheus >= v2.33.0.", "description": "Enable Prometheus to be used as a receiver for the Prometheus remote\nwrite protocol.\n\nWARNING: This is not considered an efficient way of ingesting samples.\nUse it with caution for specific low-volume use cases.\nIt is not suitable for replacing the ingestion via scraping and turning\nPrometheus into a push-based metrics collection system.\nFor more information see https://prometheus.io/docs/prometheus/latest/querying/api/#remote-write-receiver\n\nIt requires Prometheus >= v2.33.0.",
"type": "boolean" "type": "boolean"

View file

@ -318,6 +318,14 @@ type CommonPrometheusFields struct {
// It requires Prometheus >= v2.33.0. // It requires Prometheus >= v2.33.0.
EnableRemoteWriteReceiver bool `json:"enableRemoteWriteReceiver,omitempty"` EnableRemoteWriteReceiver bool `json:"enableRemoteWriteReceiver,omitempty"`
// Enable Prometheus to be used as a receiver for the OTLP Metrics protocol.
//
// Note that the OTLP receiver endpoint is automatically enabled if `.spec.otlpConfig` is defined.
//
// It requires Prometheus >= v2.47.0.
// +optional
EnableOTLPReceiver *bool `json:"enableOTLPReceiver,omitempty"`
// List of the protobuf message versions to accept when receiving the // List of the protobuf message versions to accept when receiving the
// remote writes. // remote writes.
// //

View file

@ -752,6 +752,11 @@ func (in *CommonPrometheusFields) DeepCopyInto(out *CommonPrometheusFields) {
(*out)[key] = val (*out)[key] = val
} }
} }
if in.EnableOTLPReceiver != nil {
in, out := &in.EnableOTLPReceiver, &out.EnableOTLPReceiver
*out = new(bool)
**out = **in
}
if in.RemoteWriteReceiverMessageVersions != nil { if in.RemoteWriteReceiverMessageVersions != nil {
in, out := &in.RemoteWriteReceiverMessageVersions, &out.RemoteWriteReceiverMessageVersions in, out := &in.RemoteWriteReceiverMessageVersions, &out.RemoteWriteReceiverMessageVersions
*out = make([]RemoteWriteMessageVersion, len(*in)) *out = make([]RemoteWriteMessageVersion, len(*in))

View file

@ -51,6 +51,7 @@ type CommonPrometheusFieldsApplyConfiguration struct {
ScrapeProtocols []monitoringv1.ScrapeProtocol `json:"scrapeProtocols,omitempty"` ScrapeProtocols []monitoringv1.ScrapeProtocol `json:"scrapeProtocols,omitempty"`
ExternalLabels map[string]string `json:"externalLabels,omitempty"` ExternalLabels map[string]string `json:"externalLabels,omitempty"`
EnableRemoteWriteReceiver *bool `json:"enableRemoteWriteReceiver,omitempty"` EnableRemoteWriteReceiver *bool `json:"enableRemoteWriteReceiver,omitempty"`
EnableOTLPReceiver *bool `json:"enableOTLPReceiver,omitempty"`
RemoteWriteReceiverMessageVersions []monitoringv1.RemoteWriteMessageVersion `json:"remoteWriteReceiverMessageVersions,omitempty"` RemoteWriteReceiverMessageVersions []monitoringv1.RemoteWriteMessageVersion `json:"remoteWriteReceiverMessageVersions,omitempty"`
EnableFeatures []monitoringv1.EnableFeature `json:"enableFeatures,omitempty"` EnableFeatures []monitoringv1.EnableFeature `json:"enableFeatures,omitempty"`
ExternalURL *string `json:"externalUrl,omitempty"` ExternalURL *string `json:"externalUrl,omitempty"`
@ -332,6 +333,14 @@ func (b *CommonPrometheusFieldsApplyConfiguration) WithEnableRemoteWriteReceiver
return b return b
} }
// WithEnableOTLPReceiver sets the EnableOTLPReceiver field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the EnableOTLPReceiver field is set to the value of the last call.
func (b *CommonPrometheusFieldsApplyConfiguration) WithEnableOTLPReceiver(value bool) *CommonPrometheusFieldsApplyConfiguration {
b.EnableOTLPReceiver = &value
return b
}
// WithRemoteWriteReceiverMessageVersions adds the given value to the RemoteWriteReceiverMessageVersions field in the declarative configuration // WithRemoteWriteReceiverMessageVersions adds the given value to the RemoteWriteReceiverMessageVersions field in the declarative configuration
// and returns the receiver, so that objects can be build by chaining "With" function invocations. // and returns the receiver, so that objects can be build by chaining "With" function invocations.
// If called multiple times, values provided by each call will be appended to the RemoteWriteReceiverMessageVersions field. // If called multiple times, values provided by each call will be appended to the RemoteWriteReceiverMessageVersions field.

View file

@ -267,6 +267,14 @@ func (b *PrometheusSpecApplyConfiguration) WithEnableRemoteWriteReceiver(value b
return b return b
} }
// WithEnableOTLPReceiver sets the EnableOTLPReceiver field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the EnableOTLPReceiver field is set to the value of the last call.
func (b *PrometheusSpecApplyConfiguration) WithEnableOTLPReceiver(value bool) *PrometheusSpecApplyConfiguration {
b.EnableOTLPReceiver = &value
return b
}
// WithRemoteWriteReceiverMessageVersions adds the given value to the RemoteWriteReceiverMessageVersions field in the declarative configuration // WithRemoteWriteReceiverMessageVersions adds the given value to the RemoteWriteReceiverMessageVersions field in the declarative configuration
// and returns the receiver, so that objects can be build by chaining "With" function invocations. // and returns the receiver, so that objects can be build by chaining "With" function invocations.
// If called multiple times, values provided by each call will be appended to the RemoteWriteReceiverMessageVersions field. // If called multiple times, values provided by each call will be appended to the RemoteWriteReceiverMessageVersions field.

View file

@ -255,6 +255,14 @@ func (b *PrometheusAgentSpecApplyConfiguration) WithEnableRemoteWriteReceiver(va
return b return b
} }
// WithEnableOTLPReceiver sets the EnableOTLPReceiver field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the EnableOTLPReceiver field is set to the value of the last call.
func (b *PrometheusAgentSpecApplyConfiguration) WithEnableOTLPReceiver(value bool) *PrometheusAgentSpecApplyConfiguration {
b.EnableOTLPReceiver = &value
return b
}
// WithRemoteWriteReceiverMessageVersions adds the given value to the RemoteWriteReceiverMessageVersions field in the declarative configuration // WithRemoteWriteReceiverMessageVersions adds the given value to the RemoteWriteReceiverMessageVersions field in the declarative configuration
// and returns the receiver, so that objects can be build by chaining "With" function invocations. // and returns the receiver, so that objects can be build by chaining "With" function invocations.
// If called multiple times, values provided by each call will be appended to the RemoteWriteReceiverMessageVersions field. // If called multiple times, values provided by each call will be appended to the RemoteWriteReceiverMessageVersions field.

View file

@ -15,6 +15,7 @@
package prometheus package prometheus
import ( import (
"strings"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -159,3 +160,125 @@ func TestBuildCommonPrometheusArgsWithRemoteWriteMessageV2(t *testing.T) {
}) })
} }
} }
func TestBuildCommonPrometheusArgsWithOTLPReceiver(t *testing.T) {
for _, tc := range []struct {
version string
enableOTLPReceiver *bool
expectedOTLPReceiverFlag bool
OTLPConfig *monitoringv1.OTLPConfig
expectedOTLPFeatureEnabled bool
}{
// OTLP receiver not supported.
{
version: "2.46.0",
enableOTLPReceiver: ptr.To(true),
expectedOTLPFeatureEnabled: false,
expectedOTLPReceiverFlag: false,
},
// OTLP receiver supported starting with v2.47.0.
{
version: "2.47.0",
enableOTLPReceiver: ptr.To(true),
expectedOTLPFeatureEnabled: true,
expectedOTLPReceiverFlag: false,
},
// OTLP receiver supported but not enabled.
{
version: "2.47.0",
enableOTLPReceiver: ptr.To(false),
expectedOTLPFeatureEnabled: false,
expectedOTLPReceiverFlag: false,
},
// OTLP receiver config supported but verison not support
{
version: "2.46.0",
enableOTLPReceiver: ptr.To(false),
OTLPConfig: &monitoringv1.OTLPConfig{
PromoteResourceAttributes: []string{"aa", "bb"},
},
expectedOTLPFeatureEnabled: false,
expectedOTLPReceiverFlag: false,
},
// OTLP receiver config supported
{
version: "2.55.0",
enableOTLPReceiver: nil,
OTLPConfig: &monitoringv1.OTLPConfig{
PromoteResourceAttributes: []string{"aa", "bb"},
},
expectedOTLPFeatureEnabled: true,
expectedOTLPReceiverFlag: false,
},
// OTLP receiver config supported with verison 3.x
{
version: "3.0.0",
enableOTLPReceiver: nil,
OTLPConfig: &monitoringv1.OTLPConfig{
PromoteResourceAttributes: []string{"aa", "bb"},
},
expectedOTLPFeatureEnabled: false,
expectedOTLPReceiverFlag: true,
},
// Test higher version from which enable-feature available.
{
version: "2.54.0",
enableOTLPReceiver: ptr.To(true),
expectedOTLPFeatureEnabled: true,
expectedOTLPReceiverFlag: false,
},
// Test higher version from which web.enable-otlp-receiver arg available.
{
version: "3.0.0",
enableOTLPReceiver: ptr.To(true),
expectedOTLPFeatureEnabled: false,
expectedOTLPReceiverFlag: true,
},
// Test higher version but not enabled.
{
version: "3.0.0",
enableOTLPReceiver: ptr.To(false),
expectedOTLPFeatureEnabled: false,
expectedOTLPReceiverFlag: false,
},
} {
t.Run("", func(t *testing.T) {
p := &monitoringv1.Prometheus{
Spec: monitoringv1.PrometheusSpec{
CommonPrometheusFields: monitoringv1.CommonPrometheusFields{
Version: tc.version,
EnableOTLPReceiver: tc.enableOTLPReceiver,
OTLP: tc.OTLPConfig,
},
},
}
cg, err := NewConfigGenerator(NewLogger(), p)
require.NoError(t, err)
args := cg.BuildCommonPrometheusArgs()
var (
argsEnabled bool
featureEnabled bool
)
for _, arg := range args {
switch arg.Name {
case "web.enable-otlp-receiver":
argsEnabled = true
case "enable-feature":
feats := strings.Split(arg.Value, ",")
for _, feat := range feats {
if feat == "otlp-write-receiver" {
featureEnabled = true
break
}
}
}
}
require.Equal(t, tc.expectedOTLPReceiverFlag, argsEnabled)
require.Equal(t, tc.expectedOTLPFeatureEnabled, featureEnabled)
})
}
}

View file

@ -1009,6 +1009,15 @@ func (cg *ConfigGenerator) BuildCommonPrometheusArgs() []monitoringv1.Argument {
} }
} }
// Turn on the OTLP receiver endpoint automatically if/when the OTLP config isn't empty.
if (cpf.EnableOTLPReceiver != nil && *cpf.EnableOTLPReceiver) || (cpf.EnableOTLPReceiver == nil && cpf.OTLP != nil) {
if cg.version.Major >= 3 {
promArgs = cg.AppendCommandlineArgument(promArgs, monitoringv1.Argument{Name: "web.enable-otlp-receiver"})
} else {
promArgs = cg.WithMinimumVersion("2.47.0").AppendCommandlineArgument(promArgs, monitoringv1.Argument{Name: "enable-feature", Value: "otlp-write-receiver"})
}
}
if len(cpf.EnableFeatures) > 0 { if len(cpf.EnableFeatures) > 0 {
efs := make([]string, len(cpf.EnableFeatures)) efs := make([]string, len(cpf.EnableFeatures))
for i := range cpf.EnableFeatures { for i := range cpf.EnableFeatures {