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

Added PodMonitor when using sidecar mode #2404

Merged
16 changes: 16 additions & 0 deletions .chloggen/podmonitor-otel-sidecar-mode.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. operator, target allocator, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Create PodMonitor when deploying collector in sidecar mode and Prometheus exporters are used.

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

# (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:
1 change: 1 addition & 0 deletions controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er

if featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
builder.Owns(&monitoringv1.ServiceMonitor{})
builder.Owns(&monitoringv1.PodMonitor{})
}

return builder.Complete(r)
Expand Down
6 changes: 5 additions & 1 deletion internal/manifests/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func Build(params manifests.Params) ([]client.Object, error) {
manifests.Factory(Ingress),
}...)
if params.OtelCol.Spec.Observability.Metrics.EnableMetrics && featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
manifestFactories = append(manifestFactories, manifests.Factory(ServiceMonitor))
if params.OtelCol.Spec.Mode == v1alpha1.ModeSidecar {
manifestFactories = append(manifestFactories, manifests.Factory(PodMonitor))
} else {
manifestFactories = append(manifestFactories, manifests.Factory(ServiceMonitor))
}
}
for _, factory := range manifestFactories {
res, err := factory(params)
Expand Down
101 changes: 101 additions & 0 deletions internal/manifests/collector/podmonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"fmt"
"strings"

"github.com/go-logr/logr"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

// ServiceMonitor returns the service monitor for the given instance.
func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) {
if !params.OtelCol.Spec.Observability.Metrics.EnableMetrics {
params.Log.V(2).Info("Metrics disabled for this OTEL Collector",
"params.OtelCol.name", params.OtelCol.Name,
"params.OtelCol.namespace", params.OtelCol.Namespace,
)
return nil, nil
}
var pm monitoringv1.PodMonitor

if params.OtelCol.Spec.Mode != v1alpha1.ModeSidecar {
return nil, nil
}

pm = monitoringv1.PodMonitor{
ObjectMeta: metav1.ObjectMeta{
Namespace: params.OtelCol.Namespace,
Name: naming.PodMonitor(params.OtelCol.Name),
Labels: map[string]string{
"app.kubernetes.io/name": naming.PodMonitor(params.OtelCol.Name),
"app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name),
"app.kubernetes.io/managed-by": "opentelemetry-operator",
},
},
Spec: monitoringv1.PodMonitorSpec{
JobLabel: "app.kubernetes.io/instance",
PodTargetLabels: []string{"app.kubernetes.io/name", "app.kubernetes.io/instance", "app.kubernetes.io/managed-by"},
NamespaceSelector: monitoringv1.NamespaceSelector{
MatchNames: []string{params.OtelCol.Namespace},
},
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/managed-by": "opentelemetry-operator",
"app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name),
},
},
PodMetricsEndpoints: append(
[]monitoringv1.PodMetricsEndpoint{
{
Port: "monitoring",
},
}, metricsEndpointsFromConfig(params.Log, params.OtelCol)...),
},
}

return &pm, nil
}

func metricsEndpointsFromConfig(logger logr.Logger, otelcol v1alpha1.OpenTelemetryCollector) []monitoringv1.PodMetricsEndpoint {
config, err := adapters.ConfigFromString(otelcol.Spec.Config)
if err != nil {
logger.V(2).Error(err, "Error while parsing the configuration")
return []monitoringv1.PodMetricsEndpoint{}
}
exporterPorts, err := adapters.ConfigToComponentPorts(logger, adapters.ComponentTypeExporter, config)
if err != nil {
logger.Error(err, "couldn't build endpoints to podMonitors from configuration")
return []monitoringv1.PodMetricsEndpoint{}
}
swiatekm marked this conversation as resolved.
Show resolved Hide resolved
metricsEndpoints := []monitoringv1.PodMetricsEndpoint{}
for _, port := range exporterPorts {
if strings.Contains(port.Name, "prometheus") {
e := monitoringv1.PodMetricsEndpoint{
Port: port.Name,
}
metricsEndpoints = append(metricsEndpoints, e)
}
}
return metricsEndpoints
}
59 changes: 59 additions & 0 deletions internal/manifests/collector/podmonitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"fmt"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"

"github.com/stretchr/testify/assert"

"testing"
)

func sidecarParams() manifests.Params {
return paramsWithMode(v1alpha1.ModeSidecar)
}

func TestDesiredPodMonitors(t *testing.T) {
params := sidecarParams()

actual, err := PodMonitor(params)
assert.NoError(t, err)
assert.Nil(t, actual)

params.OtelCol.Spec.Observability.Metrics.EnableMetrics = true
actual, err = PodMonitor(params)
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, fmt.Sprintf("%s-collector", params.OtelCol.Name), actual.Name)
assert.Equal(t, params.OtelCol.Namespace, actual.Namespace)
assert.Equal(t, "monitoring", actual.Spec.PodMetricsEndpoints[0].Port)

params, err = newParams("", "testdata/prometheus-exporter.yaml")
assert.NoError(t, err)
params.OtelCol.Spec.Mode = v1alpha1.ModeSidecar
params.OtelCol.Spec.Observability.Metrics.EnableMetrics = true
actual, err = PodMonitor(params)
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, fmt.Sprintf("%s-collector", params.OtelCol.Name), actual.Name)
assert.Equal(t, params.OtelCol.Namespace, actual.Namespace)
assert.Equal(t, "monitoring", actual.Spec.PodMetricsEndpoints[0].Port)
assert.Equal(t, "prometheus-dev", actual.Spec.PodMetricsEndpoints[1].Port)
assert.Equal(t, "prometheus-prod", actual.Spec.PodMetricsEndpoints[2].Port)
}
19 changes: 10 additions & 9 deletions internal/manifests/collector/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro
)
return nil, nil
}
var sm monitoringv1.ServiceMonitor

sm := monitoringv1.ServiceMonitor{
if params.OtelCol.Spec.Mode == v1alpha1.ModeSidecar {
return nil, nil
}
sm = monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Namespace: params.OtelCol.Namespace,
Name: naming.ServiceMonitor(params.OtelCol.Name),
Expand All @@ -49,7 +53,11 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro
},
},
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{},
Endpoints: append([]monitoringv1.Endpoint{
{
Port: "monitoring",
},
}, endpointsFromConfig(params.Log, params.OtelCol)...),
NamespaceSelector: monitoringv1.NamespaceSelector{
MatchNames: []string{params.OtelCol.Namespace},
},
Expand All @@ -62,13 +70,6 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro
},
}

endpoints := []monitoringv1.Endpoint{
{
Port: "monitoring",
},
}

sm.Spec.Endpoints = append(endpoints, endpointsFromConfig(params.Log, params.OtelCol)...)
return &sm, nil
}

Expand Down
7 changes: 6 additions & 1 deletion internal/naming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ func ServiceAccount(otelcol string) string {
return DNSName(Truncate("%s-collector", 63, otelcol))
}

// ServiceMonitor builds the service account name based on the instance.
// ServiceMonitor builds the service Monitor name based on the instance.
func ServiceMonitor(otelcol string) string {
return DNSName(Truncate("%s-collector", 63, otelcol))
}

// PodMonitor builds the pod Monitor name based on the instance.
func PodMonitor(otelcol string) string {
return DNSName(Truncate("%s-collector", 63, otelcol))
}

// TargetAllocatorServiceAccount returns the TargetAllocator service account resource name.
func TargetAllocatorServiceAccount(otelcol string) string {
return DNSName(Truncate("%s-targetallocator", 63, otelcol))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: v1
kind: Namespace
metadata:
name: create-pm-prometheus
---
apiVersion: opentelemetry.io/v1alpha1
kind: OpenTelemetryCollector
metadata:
name: simplest
namespace: create-pm-prometheus
spec:
mode: sidecar
observability:
metrics:
enableMetrics: true
config: |
receivers:
otlp:
protocols:
grpc:
http:

exporters:
prometheus/prod:
endpoint: 0.0.0.0:8884

prometheus/dev:
endpoint: 0.0.0.0:8885

service:
pipelines:
metrics:
receivers: [otlp]
processors: []
exporters: [prometheus/dev, prometheus/prod]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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/instance"
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-with-sidecar
namespace: create-pm-prometheus
spec:
selector:
matchLabels:
app: pod-with-sidecar
replicas: 1
template:
metadata:
labels:
app: pod-with-sidecar
annotations:
sidecar.opentelemetry.io/inject: "true"
spec:
containers:
- name: myapp
image: ghcr.io/open-telemetry/opentelemetry-operator/e2e-test-app-python:main
Loading