|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package collector |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/go-logr/logr" |
| 22 | + monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + |
| 25 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" |
| 26 | + "github.com/open-telemetry/opentelemetry-operator/internal/manifests" |
| 27 | + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters" |
| 28 | + "github.com/open-telemetry/opentelemetry-operator/internal/naming" |
| 29 | +) |
| 30 | + |
| 31 | +// ServiceMonitor returns the service monitor for the given instance. |
| 32 | +func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) { |
| 33 | + if !params.OtelCol.Spec.Observability.Metrics.EnableMetrics { |
| 34 | + params.Log.V(2).Info("Metrics disabled for this OTEL Collector", |
| 35 | + "params.OtelCol.name", params.OtelCol.Name, |
| 36 | + "params.OtelCol.namespace", params.OtelCol.Namespace, |
| 37 | + ) |
| 38 | + return nil, nil |
| 39 | + } |
| 40 | + var pm monitoringv1.PodMonitor |
| 41 | + |
| 42 | + if params.OtelCol.Spec.Mode != v1alpha1.ModeSidecar { |
| 43 | + return nil, nil |
| 44 | + } |
| 45 | + |
| 46 | + pm = monitoringv1.PodMonitor{ |
| 47 | + ObjectMeta: metav1.ObjectMeta{ |
| 48 | + Namespace: params.OtelCol.Namespace, |
| 49 | + Name: naming.PodMonitor(params.OtelCol.Name), |
| 50 | + Labels: map[string]string{ |
| 51 | + "app.kubernetes.io/name": naming.PodMonitor(params.OtelCol.Name), |
| 52 | + "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), |
| 53 | + "app.kubernetes.io/managed-by": "opentelemetry-operator", |
| 54 | + }, |
| 55 | + }, |
| 56 | + Spec: monitoringv1.PodMonitorSpec{ |
| 57 | + JobLabel: "app.kubernetes.io/instance", |
| 58 | + PodTargetLabels: []string{"app.kubernetes.io/name", "app.kubernetes.io/instance", "app.kubernetes.io/managed-by"}, |
| 59 | + NamespaceSelector: monitoringv1.NamespaceSelector{ |
| 60 | + MatchNames: []string{params.OtelCol.Namespace}, |
| 61 | + }, |
| 62 | + Selector: metav1.LabelSelector{ |
| 63 | + MatchLabels: map[string]string{ |
| 64 | + "app.kubernetes.io/managed-by": "opentelemetry-operator", |
| 65 | + "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), |
| 66 | + }, |
| 67 | + }, |
| 68 | + PodMetricsEndpoints: append( |
| 69 | + []monitoringv1.PodMetricsEndpoint{ |
| 70 | + { |
| 71 | + Port: "monitoring", |
| 72 | + }, |
| 73 | + }, metricsEndpointsFromConfig(params.Log, params.OtelCol)...), |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + return &pm, nil |
| 78 | +} |
| 79 | + |
| 80 | +func metricsEndpointsFromConfig(logger logr.Logger, otelcol v1alpha1.OpenTelemetryCollector) []monitoringv1.PodMetricsEndpoint { |
| 81 | + config, err := adapters.ConfigFromString(otelcol.Spec.Config) |
| 82 | + if err != nil { |
| 83 | + logger.V(2).Error(err, "Error while parsing the configuration") |
| 84 | + return []monitoringv1.PodMetricsEndpoint{} |
| 85 | + } |
| 86 | + exporterPorts, err := adapters.ConfigToComponentPorts(logger, adapters.ComponentTypeExporter, config) |
| 87 | + if err != nil { |
| 88 | + logger.Error(err, "couldn't build endpoints to podMonitors from configuration") |
| 89 | + return []monitoringv1.PodMetricsEndpoint{} |
| 90 | + } |
| 91 | + metricsEndpoints := []monitoringv1.PodMetricsEndpoint{} |
| 92 | + for _, port := range exporterPorts { |
| 93 | + if strings.Contains(port.Name, "prometheus") { |
| 94 | + e := monitoringv1.PodMetricsEndpoint{ |
| 95 | + Port: port.Name, |
| 96 | + } |
| 97 | + metricsEndpoints = append(metricsEndpoints, e) |
| 98 | + } |
| 99 | + } |
| 100 | + return metricsEndpoints |
| 101 | +} |
0 commit comments