Skip to content

Commit d53ff48

Browse files
committed
Fix Cronjob name
Signed-off-by: Janario Oliveira <[email protected]>
1 parent 78e7c2f commit d53ff48

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

pkg/instrumentation/sdk.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"go.opentelemetry.io/otel/attribute"
3232
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
3333
appsv1 "k8s.io/api/apps/v1"
34+
batchv1 "k8s.io/api/batch/v1"
3435
corev1 "k8s.io/api/core/v1"
3536
apierrors "k8s.io/apimachinery/pkg/api/errors"
3637
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -380,10 +381,10 @@ func chooseServiceName(pod corev1.Pod, resources map[string]string, index int) s
380381
if name := resources[string(semconv.K8SDaemonSetNameKey)]; name != "" {
381382
return name
382383
}
383-
if name := resources[string(semconv.K8SJobNameKey)]; name != "" {
384+
if name := resources[string(semconv.K8SCronJobNameKey)]; name != "" {
384385
return name
385386
}
386-
if name := resources[string(semconv.K8SCronJobNameKey)]; name != "" {
387+
if name := resources[string(semconv.K8SJobNameKey)]; name != "" {
387388
return name
388389
}
389390
if name := resources[string(semconv.K8SPodNameKey)]; name != "" {
@@ -502,6 +503,26 @@ func (i *sdkInjector) addParentResourceLabels(ctx context.Context, uid bool, ns
502503
if uid {
503504
resources[semconv.K8SJobUIDKey] = string(owner.UID)
504505
}
506+
507+
// parent of Job can be CronJob which we are interested to know
508+
j := batchv1.Job{}
509+
nsn := types.NamespacedName{Namespace: ns.Name, Name: owner.Name}
510+
backOff := wait.Backoff{Duration: 10 * time.Millisecond, Factor: 1.5, Jitter: 0.1, Steps: 20, Cap: 2 * time.Second}
511+
512+
checkError := func(err error) bool {
513+
return apierrors.IsNotFound(err)
514+
}
515+
516+
getJob := func() error {
517+
return i.client.Get(ctx, nsn, &j)
518+
}
519+
520+
// use a retry loop to get the Job. A single call to client.get fails occasionally
521+
err := retry.OnError(backOff, checkError, getJob)
522+
if err != nil {
523+
i.logger.Error(err, "failed to get job", "job", nsn.Name, "namespace", nsn.Namespace)
524+
}
525+
i.addParentResourceLabels(ctx, uid, ns, j.ObjectMeta, resources)
505526
case "cronjob":
506527
resources[semconv.K8SCronJobNameKey] = owner.Name
507528
if uid {

0 commit comments

Comments
 (0)