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

Create ServiceMonitor to monitor target allocator #2416

Merged
merged 36 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
66021f4
add create service monitor to targetallocator
changexd Dec 4, 2023
e56875c
add chlog
changexd Dec 4, 2023
560b15f
add e2e test to ta service monitor
changexd Dec 5, 2023
4cc1f21
fix linting
changexd Dec 5, 2023
c8c855d
make bundle
changexd Dec 5, 2023
8764da3
Merge branch 'main' into ta-service-monitor
changexd Dec 5, 2023
1f188b0
fix chlog
changexd Dec 5, 2023
bfb3af9
Merge branch 'main' into ta-service-monitor
changexd Dec 6, 2023
ef94a2f
add a condition to avoid creating sm when running in unsupported modes
changexd Dec 6, 2023
f1dc33f
update bundle
changexd Dec 6, 2023
eabb815
move ta sm ports into ta definition
changexd Dec 6, 2023
39059cd
Merge branch 'main' into ta-service-monitor
changexd Dec 7, 2023
de8bc76
remove changes to config/manager
changexd Dec 8, 2023
ebf8ee7
assert ta service monitor creation in e2e
changexd Dec 9, 2023
41d601c
Merge branch 'main' into ta-service-monitor
changexd Dec 9, 2023
1246aac
enable prometheus feature in e2e test
changexd Dec 11, 2023
9bbe614
Merge branch 'main' into ta-service-monitor
changexd Dec 12, 2023
f235362
change description of observability field
changexd Dec 12, 2023
f49464b
move ta observability check to build
changexd Dec 12, 2023
934b263
add target allocator to builder test
changexd Dec 13, 2023
93823b5
fix lint
changexd Dec 13, 2023
fde4422
fix ta sm manifest test
changexd Dec 13, 2023
3d2ba7d
remove changes to manager kustomization
changexd Dec 13, 2023
8da8af8
Merge branch 'main' into ta-service-monitor
changexd Dec 15, 2023
3863552
make ta sm return directly
changexd Dec 16, 2023
dc63340
move ta service monitor test to dedicated tests
changexd Dec 16, 2023
2f6726d
remove commands in e2e
changexd Dec 16, 2023
9b520ed
use function to generate label for sm
changexd Dec 18, 2023
e284412
Merge branch 'main' into ta-service-monitor
changexd Dec 18, 2023
b1ddba3
ta sm labels in builder test
changexd Dec 18, 2023
85a4275
merge with main
changexd Dec 19, 2023
508ad76
fix api.md
changexd Dec 19, 2023
2d709c3
fix bundling
changexd Dec 19, 2023
5b67f36
make api-docs again
changexd Dec 19, 2023
8cd6230
fix kustomization manager
changexd Dec 19, 2023
fd9fde7
fix ta service account in test according to fixed issues
changexd Dec 19, 2023
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/2311-ta-servicemonitor.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
changexd marked this conversation as resolved.
Show resolved Hide resolved

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Create ServiceMonitor to monitor target allocator

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

# (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:
6 changes: 6 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ type OpenTelemetryTargetAllocator struct {
// consumed in the config file for the TargetAllocator.
// +optional
Env []v1.EnvVar `json:"env,omitempty"`
// ObservabilitySpec defines how telemetry data gets handled.
//
// +optional
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Observability"
Observability ObservabilitySpec `json:"observability,omitempty"`
}

type OpenTelemetryTargetAllocatorPrometheusCR struct {
Expand Down
1 change: 1 addition & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ metadata:
categories: Logging & Tracing,Monitoring
certified: "false"
containerImage: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
createdAt: "2023-11-17T13:24:32Z"
createdAt: "2023-12-05T09:09:57Z"
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 @@ -140,6 +140,17 @@ spec:
feature gate must be enabled to use this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: observability.metrics.enableMetrics
- description: ObservabilitySpec defines how telemetry data gets handled.
displayName: Observability
path: targetAllocator.observability
- description: Metrics defines the metrics configuration for operands.
displayName: Metrics Config
path: targetAllocator.observability.metrics
- description: EnableMetrics specifies if ServiceMonitor should be created for
the OpenTelemetry Collector and Prometheus Exporters. The operator.observability.prometheus
feature gate must be enabled to use this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: targetAllocator.observability.metrics.enableMetrics
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
15 changes: 15 additions & 0 deletions bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4966,6 +4966,21 @@ spec:
description: NodeSelector to schedule OpenTelemetry TargetAllocator
pods.
type: object
observability:
description: ObservabilitySpec defines how telemetry data gets
handled.
properties:
metrics:
description: Metrics defines the metrics configuration for
operands.
properties:
enableMetrics:
description: EnableMetrics specifies if ServiceMonitor
should be created for the OpenTelemetry Collector and
Prometheus Exporters. The operator.observability.
type: boolean
type: object
type: object
prometheusCR:
description: PrometheusCR defines the configuration for the retrieval
of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1
Expand Down
15 changes: 15 additions & 0 deletions config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4963,6 +4963,21 @@ spec:
description: NodeSelector to schedule OpenTelemetry TargetAllocator
pods.
type: object
observability:
description: ObservabilitySpec defines how telemetry data gets
handled.
properties:
metrics:
description: Metrics defines the metrics configuration for
operands.
properties:
enableMetrics:
description: EnableMetrics specifies if ServiceMonitor
should be created for the OpenTelemetry Collector and
Prometheus Exporters. The operator.observability.
type: boolean
type: object
type: object
prometheusCR:
description: PrometheusCR defines the configuration for the retrieval
of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1
Expand Down
6 changes: 6 additions & 0 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
resources:
- manager.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
changexd marked this conversation as resolved.
Show resolved Hide resolved
kind: Kustomization
images:
- name: controller
newName: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
newTag: 0.89.0
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ spec:
feature gate must be enabled to use this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: observability.metrics.enableMetrics
- description: ObservabilitySpec defines how telemetry data gets handled.
displayName: Observability
path: targetAllocator.observability
- description: Metrics defines the metrics configuration for operands.
displayName: Metrics Config
path: targetAllocator.observability.metrics
- description: EnableMetrics specifies if ServiceMonitor should be created for
the OpenTelemetry Collector and Prometheus Exporters. The operator.observability.prometheus
feature gate must be enabled to use this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: targetAllocator.observability.metrics.enableMetrics
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
61 changes: 61 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18060,6 +18060,13 @@ TargetAllocator indicates a value which determines whether to spawn a target all
NodeSelector to schedule OpenTelemetry TargetAllocator pods.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#opentelemetrycollectorspectargetallocatorobservability">observability</a></b></td>
<td>object</td>
<td>
ObservabilitySpec defines how telemetry data gets handled.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#opentelemetrycollectorspectargetallocatorprometheuscr">prometheusCR</a></b></td>
<td>object</td>
Expand Down Expand Up @@ -19656,6 +19663,60 @@ Selects a key of a secret in the pod's namespace
</table>


### OpenTelemetryCollector.spec.targetAllocator.observability
<sup><sup>[↩ Parent](#opentelemetrycollectorspectargetallocator)</sup></sup>



ObservabilitySpec defines how telemetry data gets handled.

<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Required</th>
</tr>
</thead>
<tbody><tr>
<td><b><a href="#opentelemetrycollectorspectargetallocatorobservabilitymetrics">metrics</a></b></td>
<td>object</td>
<td>
Metrics defines the metrics configuration for operands.<br/>
</td>
<td>false</td>
</tr></tbody>
</table>


### OpenTelemetryCollector.spec.targetAllocator.observability.metrics
<sup><sup>[↩ Parent](#opentelemetrycollectorspectargetallocatorobservability)</sup></sup>



Metrics defines the metrics configuration for operands.

<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Required</th>
</tr>
</thead>
<tbody><tr>
<td><b>enableMetrics</b></td>
<td>boolean</td>
<td>
EnableMetrics specifies if ServiceMonitor should be created for the OpenTelemetry Collector and Prometheus Exporters. The operator.observability.<br/>
</td>
<td>false</td>
</tr></tbody>
</table>


### OpenTelemetryCollector.spec.targetAllocator.prometheusCR
<sup><sup>[↩ Parent](#opentelemetrycollectorspectargetallocator)</sup></sup>

Expand Down
70 changes: 70 additions & 0 deletions internal/manifests/targetallocator/servicemonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 targetallocator

import (
"fmt"

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/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

// ServiceMonitor returns the service monitor for the given instance.
func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, error) {
if !params.OtelCol.Spec.TargetAllocator.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
}

changexd marked this conversation as resolved.
Show resolved Hide resolved
sm := monitoringv1.ServiceMonitor{
changexd marked this conversation as resolved.
Show resolved Hide resolved
ObjectMeta: metav1.ObjectMeta{
Namespace: params.OtelCol.Namespace,
Name: naming.TargetAllocator(params.OtelCol.Name),
Labels: map[string]string{
"app.kubernetes.io/name": naming.TargetAllocator(params.OtelCol.Name),
"app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name),
"app.kubernetes.io/managed-by": "opentelemetry-operator",
"app.kubernetes.io/component": "opentelemetry-targetallocator",
},
changexd marked this conversation as resolved.
Show resolved Hide resolved
},
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{},
yuriolisa marked this conversation as resolved.
Show resolved Hide resolved
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),
"app.kubernetes.io/component": "opentelemetry-targetallocator",
changexd marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
}

sm.Spec.Endpoints = []monitoringv1.Endpoint{
{
Port: "targetallocation",
},
}

return &sm, nil
}
48 changes: 48 additions & 0 deletions internal/manifests/targetallocator/servicemonitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 targetallocator

import (
"testing"

"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"

"github.com/stretchr/testify/assert"
)

func TestDesiredServiceMonitors(t *testing.T) {
otelcol := collectorInstance()
cfg := config.New()

params := manifests.Params{
OtelCol: otelcol,
Config: cfg,
Log: logger,
}

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

params.OtelCol.Spec.TargetAllocator.Observability.Metrics.EnableMetrics = true
actual, err = ServiceMonitor(params)
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, naming.TargetAllocator(params.OtelCol.Name), actual.Name)
assert.Equal(t, params.OtelCol.Namespace, actual.Namespace)
assert.Equal(t, "targetallocation", actual.Spec.Endpoints[0].Port)
}
6 changes: 6 additions & 0 deletions internal/manifests/targetallocator/targetallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
)

// Build creates the manifest for the TargetAllocator resource.
Expand All @@ -32,6 +33,11 @@ func Build(params manifests.Params) ([]client.Object, error) {
manifests.FactoryWithoutError(ServiceAccount),
manifests.FactoryWithoutError(Service),
}

if params.OtelCol.Spec.TargetAllocator.Observability.Metrics.EnableMetrics && featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
resourceFactories = append(resourceFactories, manifests.Factory(ServiceMonitor))
}

for _, factory := range resourceFactories {
res, err := factory(params)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/naming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func TargetAllocatorServiceAccount(otelcol string) string {
return DNSName(Truncate("%s-targetallocator", 63, otelcol))
}

// TargetAllocatorServiceMonitor returns the TargetAllocator service account resource name.
func TargetAllocatorServiceMonitor(otelcol string) string {
return DNSName(Truncate("%s-targetallocator", 63, otelcol))
}

// OpAMPBridgeServiceAccount builds the service account name based on the instance.
func OpAMPBridgeServiceAccount(opampBridge string) string {
return DNSName(Truncate("%s-opamp-bridge", 63, opampBridge))
Expand Down
3 changes: 3 additions & 0 deletions tests/e2e/targetallocator-prometheuscr/00-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ spec:
image: "local/opentelemetry-operator-targetallocator:e2e"
prometheusCR:
enabled: true
observability:
metrics:
enableMetrics: true
changexd marked this conversation as resolved.
Show resolved Hide resolved
config: |
receivers:
prometheus:
Expand Down
18 changes: 17 additions & 1 deletion tests/e2e/targetallocator-prometheuscr/01-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ spec:
args:
- /bin/sh
- -c
- curl -s http://prometheus-cr-targetallocator/jobs | grep "prometheus-cr"
- curl -s http://prometheus-cr-targetallocator/jobs | grep "prometheus-cr"
---
apiVersion: batch/v1
kind: Job
metadata:
name: check-ta-metrics
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: check-metrics
image: curlimages/curl
args:
- /bin/sh
- -c
- curl -s http://prometheus-cr-targetallocator/jobs | grep "targetallocator"
Loading