Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add joblabel customization #2776

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/make-job-label-customizable.yaml
Original file line number Diff line number Diff line change
@@ -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: collector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds the joblabel field to the observability spec to customize the ServiceMonitor's job label

# One or more tracking issues related to the change
issues: [2775]

# (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:
4 changes: 4 additions & 0 deletions apis/v1alpha1/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error
Observability: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
EnableMetrics: copy.Spec.Observability.Metrics.EnableMetrics,
JobLabel: copy.Spec.Observability.Metrics.JobLabel,
DisablePrometheusAnnotations: copy.Spec.Observability.Metrics.DisablePrometheusAnnotations,
},
},
Expand Down Expand Up @@ -174,6 +175,7 @@ func tov1beta1TA(in OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedde
Observability: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
EnableMetrics: in.Observability.Metrics.EnableMetrics,
JobLabel: in.Observability.Metrics.JobLabel,
DisablePrometheusAnnotations: in.Observability.Metrics.DisablePrometheusAnnotations,
},
},
Expand Down Expand Up @@ -316,6 +318,7 @@ func tov1alpha1(in v1beta1.OpenTelemetryCollector) (*OpenTelemetryCollector, err
Observability: ObservabilitySpec{
Metrics: MetricsConfigSpec{
EnableMetrics: copy.Spec.Observability.Metrics.EnableMetrics,
JobLabel: copy.Spec.Observability.Metrics.JobLabel,
DisablePrometheusAnnotations: copy.Spec.Observability.Metrics.DisablePrometheusAnnotations,
},
},
Expand Down Expand Up @@ -419,6 +422,7 @@ func tov1alpha1TA(in v1beta1.TargetAllocatorEmbedded) OpenTelemetryTargetAllocat
Observability: ObservabilitySpec{
Metrics: MetricsConfigSpec{
EnableMetrics: in.Observability.Metrics.EnableMetrics,
JobLabel: in.Observability.Metrics.JobLabel,
DisablePrometheusAnnotations: in.Observability.Metrics.DisablePrometheusAnnotations,
},
},
Expand Down
8 changes: 8 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,14 @@ type MetricsConfigSpec struct {
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Create ServiceMonitors for OpenTelemetry Collector"
EnableMetrics bool `json:"enableMetrics,omitempty"`
// JobLabel specifies the label to use for the job in the ServiceMonitor or PodMonitor(for sidecar mode).
// The operator.observability.prometheus feature gate must be enabled to use this feature.
// EnableMetrics must be true to set this.
//
// +optional
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Job Label for generated ServiceMonitors"
JobLabel string `json:"jobLabel,omitempty"`
// DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations
// ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
//
Expand Down
8 changes: 8 additions & 0 deletions apis/v1beta1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ type MetricsConfigSpec struct {
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Create ServiceMonitors for OpenTelemetry Collector"
EnableMetrics bool `json:"enableMetrics,omitempty"`
// JobLabel specifies the label to use for the job in the ServiceMonitor or PodMonitor(for sidecar mode).
// The operator.observability.prometheus feature gate must be enabled to use this feature.
// EnableMetrics must be true to set this.
//
// +optional
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Job Label for generated ServiceMonitors"
JobLabel string `json:"jobLabel,omitempty"`
// DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations
// ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ metadata:
categories: Logging & Tracing,Monitoring
certified: "false"
containerImage: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
createdAt: "2024-03-11T13:32:19Z"
createdAt: "2024-03-21T17:09:59Z"
description: Provides the OpenTelemetry components, including the Collector
operators.operatorframework.io/builder: operator-sdk-v1.29.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
Expand Down Expand Up @@ -175,6 +175,12 @@ spec:
this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: observability.metrics.enableMetrics
- description: JobLabel specifies the label to use for the job in the ServiceMonitor
or PodMonitor(for sidecar mode). The operator.observability.prometheus feature
gate must be enabled to use this feature. EnableMetrics must be true to
set this.
displayName: Job Label for generated ServiceMonitors
path: observability.metrics.jobLabel
- description: ObservabilitySpec defines how telemetry data gets handled.
displayName: Observability
path: targetAllocator.observability
Expand All @@ -187,6 +193,12 @@ spec:
this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: targetAllocator.observability.metrics.enableMetrics
- description: JobLabel specifies the label to use for the job in the ServiceMonitor
or PodMonitor(for sidecar mode). The operator.observability.prometheus feature
gate must be enabled to use this feature. EnableMetrics must be true to
set this.
displayName: Job Label for generated ServiceMonitors
path: targetAllocator.observability.metrics.jobLabel
version: v1alpha1
- description: OpenTelemetryCollector is the Schema for the opentelemetrycollectors
API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podAnnotations:
Expand Down Expand Up @@ -2933,6 +2935,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podDisruptionBudget:
Expand Down Expand Up @@ -6518,6 +6522,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podAnnotations:
Expand Down Expand Up @@ -7227,6 +7233,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podDisruptionBudget:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podAnnotations:
Expand Down Expand Up @@ -2919,6 +2921,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podDisruptionBudget:
Expand Down Expand Up @@ -6504,6 +6508,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podAnnotations:
Expand Down Expand Up @@ -7213,6 +7219,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podDisruptionBudget:
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/opentelemetry.io_targetallocators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,8 @@ spec:
type: boolean
enableMetrics:
type: boolean
jobLabel:
type: string
type: object
type: object
podAnnotations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ spec:
this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: observability.metrics.enableMetrics
- description: JobLabel specifies the label to use for the job in the ServiceMonitor
or PodMonitor(for sidecar mode). The operator.observability.prometheus feature
gate must be enabled to use this feature. EnableMetrics must be true to
set this.
displayName: Job Label for generated ServiceMonitors
path: observability.metrics.jobLabel
- description: ObservabilitySpec defines how telemetry data gets handled.
displayName: Observability
path: targetAllocator.observability
Expand All @@ -149,6 +155,12 @@ spec:
this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: targetAllocator.observability.metrics.enableMetrics
- description: JobLabel specifies the label to use for the job in the ServiceMonitor
or PodMonitor(for sidecar mode). The operator.observability.prometheus feature
gate must be enabled to use this feature. EnableMetrics must be true to
set this.
displayName: Job Label for generated ServiceMonitors
path: targetAllocator.observability.metrics.jobLabel
version: v1alpha1
description: |-
OpenTelemetry is a collection of tools, APIs, and SDKs. You use it to instrument, generate, collect, and export telemetry data (metrics, logs, and traces) for analysis in order to understand your software's performance and behavior.
Expand Down
28 changes: 28 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17809,6 +17809,13 @@ Resource Types:
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>jobLabel</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down Expand Up @@ -20315,6 +20322,13 @@ Resource Types:
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>jobLabel</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down Expand Up @@ -33688,6 +33702,13 @@ Resource Types:
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>jobLabel</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down Expand Up @@ -36195,6 +36216,13 @@ Resource Types:
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>jobLabel</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down
6 changes: 5 additions & 1 deletion internal/manifests/collector/podmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) {
name := naming.PodMonitor(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, nil)
selectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector)
jobLabel := params.OtelCol.Spec.Observability.Metrics.JobLabel
if len(jobLabel) == 0 {
jobLabel = "app.kubernetes.io/instance"
}
pm = monitoringv1.PodMonitor{
ObjectMeta: metav1.ObjectMeta{
Namespace: params.OtelCol.Namespace,
Name: name,
Labels: labels,
},
Spec: monitoringv1.PodMonitorSpec{
JobLabel: "app.kubernetes.io/instance",
JobLabel: jobLabel,
PodTargetLabels: []string{"app.kubernetes.io/name", "app.kubernetes.io/instance", "app.kubernetes.io/managed-by"},
NamespaceSelector: monitoringv1.NamespaceSelector{
MatchNames: []string{params.OtelCol.Namespace},
Expand Down
3 changes: 3 additions & 0 deletions internal/manifests/collector/podmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestDesiredPodMonitors(t *testing.T) {
assert.Nil(t, actual)

params.OtelCol.Spec.Observability.Metrics.EnableMetrics = true
params.OtelCol.Spec.Observability.Metrics.JobLabel = "app.kubernetes.io/name"
actual, err = PodMonitor(params)
assert.NoError(t, err)
assert.NotNil(t, actual)
Expand All @@ -49,6 +50,7 @@ func TestDesiredPodMonitors(t *testing.T) {
"app.kubernetes.io/part-of": "opentelemetry",
}
assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels)
assert.Equal(t, "app.kubernetes.io/name", actual.Spec.JobLabel)
}

func TestDesiredPodMonitorsWithPrometheus(t *testing.T) {
Expand All @@ -71,4 +73,5 @@ func TestDesiredPodMonitorsWithPrometheus(t *testing.T) {
"app.kubernetes.io/component": "opentelemetry-collector",
}
assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels)
assert.Equal(t, "app.kubernetes.io/instance", actual.Spec.JobLabel)
}
1 change: 1 addition & 0 deletions internal/manifests/collector/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro
Labels: labels,
},
Spec: monitoringv1.ServiceMonitorSpec{
JobLabel: params.OtelCol.Spec.Observability.Metrics.JobLabel,
Endpoints: append([]monitoringv1.Endpoint{
{
Port: "monitoring",
Expand Down
2 changes: 2 additions & 0 deletions internal/manifests/collector/servicemonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestDesiredServiceMonitors(t *testing.T) {
assert.Nil(t, actual)

params.OtelCol.Spec.Observability.Metrics.EnableMetrics = true
params.OtelCol.Spec.Observability.Metrics.JobLabel = "app.kubernetes.io/name"
actual, err = ServiceMonitor(params)
assert.NoError(t, err)
assert.NotNil(t, actual)
Expand All @@ -43,6 +44,7 @@ func TestDesiredServiceMonitors(t *testing.T) {
"operator.opentelemetry.io/collector-monitoring-service": "Exists",
}
assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels)
assert.Equal(t, "app.kubernetes.io/name", actual.Spec.JobLabel)
}

func TestDesiredServiceMonitorsWithPrometheus(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: v1
kind: Pod
metadata:
annotations:
sidecar.opentelemetry.io/inject: "true"
labels:
app: pod-with-sidecar
namespace: create-pm-prometheus
spec:
containers:
- name: myapp
- name: otc-container
env:
- name: POD_NAME
- name: OTEL_CONFIG
- name: OTEL_RESOURCE_ATTRIBUTES_POD_NAME
- name: OTEL_RESOURCE_ATTRIBUTES_POD_UID
- name: OTEL_RESOURCE_ATTRIBUTES_NODE_NAME
- name: OTEL_RESOURCE_ATTRIBUTES
status:
phase: Running
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
labels:
app.kubernetes.io/instance: create-pm-prometheus.simplest
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/name: simplest-collector
name: simplest-collector
namespace: create-pm-prometheus
spec:
jobLabel: "app.kubernetes.io/name"
podMetricsEndpoints:
- port: monitoring
- port: prometheus-dev
- port: prometheus-prod
namespaceSelector:
matchNames:
- create-pm-prometheus
selector:
matchLabels:
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/instance: create-pm-prometheus.simplest

Loading
Loading