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

Fix Cronjob name #2717

Merged
merged 11 commits into from
Apr 3, 2024
Merged
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
19 changes: 19 additions & 0 deletions .chloggen/cronjob-service-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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: auto-instrumentation

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: CronJob name propagated to the OTEL Service Name

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

# (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: |
When instrumenting a `CronJob` it would generate a `Job` and then a `Pod`.
Previously it would use the `Job` name as the Service name, generating too many different services on each cron execution.
This release fixed to collect the `Job` parent's name, if it has one.
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-25T08:38:05Z"
createdAt: "2024-04-02T20:37:56Z"
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 @@ -320,6 +320,14 @@ spec:
- patch
- update
- watch
- apiGroups:
- batch
resources:
- jobs
verbs:
- get
- list
- watch
- apiGroups:
- config.openshift.io
resources:
Expand Down
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- batch
resources:
- jobs
verbs:
- get
- list
- watch
- apiGroups:
- config.openshift.io
resources:
Expand Down
1 change: 1 addition & 0 deletions internal/webhook/podmutation/webhookhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
// +kubebuilder:rbac:groups=opentelemetry.io,resources=opentelemetrycollectors,verbs=get;list;watch
// +kubebuilder:rbac:groups=opentelemetry.io,resources=instrumentations,verbs=get;list;watch
// +kubebuilder:rbac:groups="apps",resources=replicasets,verbs=get;list;watch
// +kubebuilder:rbac:groups="batch",resources=jobs,verbs=get;list;watch

var _ WebhookHandler = (*podMutationWebhook)(nil)

Expand Down
28 changes: 26 additions & 2 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -398,16 +399,19 @@ func chooseServiceName(pod corev1.Pod, resources map[string]string, index int) s
if name := resources[string(semconv.K8SDeploymentNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SReplicaSetNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SStatefulSetNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SDaemonSetNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SJobNameKey)]; name != "" {
if name := resources[string(semconv.K8SCronJobNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SCronJobNameKey)]; name != "" {
if name := resources[string(semconv.K8SJobNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SPodNameKey)]; name != "" {
Expand Down Expand Up @@ -526,6 +530,26 @@ func (i *sdkInjector) addParentResourceLabels(ctx context.Context, uid bool, ns
if uid {
resources[semconv.K8SJobUIDKey] = string(owner.UID)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be really nice to add an E2E test for this situation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e2e done, pls give it another review :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a unit test, while we're at it? LGTM otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I'll take a look and get back to you soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, pls give it another review :-)

// parent of Job can be CronJob which we are interested to know
j := batchv1.Job{}
nsn := types.NamespacedName{Namespace: ns.Name, Name: owner.Name}
backOff := wait.Backoff{Duration: 10 * time.Millisecond, Factor: 1.5, Jitter: 0.1, Steps: 20, Cap: 2 * time.Second}

checkError := func(err error) bool {
return apierrors.IsNotFound(err)
}

getJob := func() error {
return i.client.Get(ctx, nsn, &j)
}

// use a retry loop to get the Job. A single call to client.get fails occasionally
err := retry.OnError(backOff, checkError, getJob)
if err != nil {
i.logger.Error(err, "failed to get job", "job", nsn.Name, "namespace", nsn.Namespace)
}
i.addParentResourceLabels(ctx, uid, ns, j.ObjectMeta, resources)
case "cronjob":
resources[semconv.K8SCronJobNameKey] = owner.Name
if uid {
Expand Down
Loading
Loading