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

[processor/k8sattributes] add CronJob name attribute #13214

Merged
merged 2 commits into from
Aug 18, 2022
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
4 changes: 2 additions & 2 deletions processor/k8sattributesprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ type ExtractConfig struct {
// k8s.node.name, k8s.namespace.name, k8s.pod.start_time,
// k8s.replicaset.name, k8s.replicaset.uid,
// k8s.daemonset.name, k8s.daemonset.uid,
// k8s.job.name, k8s.job.uid,
// k8s.statefulset.name, and k8s.statefulset.uid
// k8s.job.name, k8s.job.uid, k8s.cronjob.name,
// k8s.statefulset.name, k8s.statefulset.uid
//
// Specifying anything other than these values will result in an error.
// By default all of the fields are extracted and added to spans and metrics.
Expand Down
14 changes: 13 additions & 1 deletion processor/k8sattributesprocessor/internal/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type WatchClient struct {
informer cache.SharedInformer
namespaceInformer cache.SharedInformer
replicasetRegex *regexp.Regexp
cronJobRegex *regexp.Regexp
deleteQueue []deleteRequest
stopCh chan struct{}

Expand All @@ -63,6 +64,10 @@ type WatchClient struct {
// format: [deployment-name]-[Random-String-For-ReplicaSet]
var rRegex = regexp.MustCompile(`^(.*)-[0-9a-zA-Z]+$`)

// Extract CronJob name from the Job name. Job name is created using
// format: [cronjob-name]-[time-hash-int]
var cronJobRegex = regexp.MustCompile(`^(.*)-[0-9]+$`)

// New initializes a new k8s Client.
func New(logger *zap.Logger, apiCfg k8sconfig.APIConfig, rules ExtractionRules, filters Filters, associations []Association, exclude Excludes, newClientSet APIClientsetProvider, newInformer InformerProvider, newNamespaceInformer InformerProviderNamespace) (Client, error) {
c := &WatchClient{
Expand All @@ -72,6 +77,7 @@ func New(logger *zap.Logger, apiCfg k8sconfig.APIConfig, rules ExtractionRules,
Associations: associations,
Exclude: exclude,
replicasetRegex: rRegex,
cronJobRegex: cronJobRegex,
stopCh: make(chan struct{}),
}
go c.deleteLoop(time.Second*30, defaultPodDeleteGracePeriod)
Expand Down Expand Up @@ -296,7 +302,7 @@ func (c *WatchClient) extractPodAttributes(pod *api_v1.Pod) map[string]string {
c.Rules.DaemonSetUID || c.Rules.DaemonSetName ||
c.Rules.JobUID || c.Rules.JobName ||
c.Rules.StatefulSetUID || c.Rules.StatefulSetName ||
c.Rules.Deployment {
c.Rules.Deployment || c.Rules.CronJobName {
for _, ref := range pod.OwnerReferences {
switch ref.Kind {
case "ReplicaSet":
Expand Down Expand Up @@ -328,6 +334,12 @@ func (c *WatchClient) extractPodAttributes(pod *api_v1.Pod) map[string]string {
tags[conventions.AttributeK8SStatefulSetName] = ref.Name
}
case "Job":
if c.Rules.CronJobName {
parts := c.cronJobRegex.FindStringSubmatch(ref.Name)
if len(parts) == 2 {
tags[conventions.AttributeK8SCronJobName] = parts[1]
}
}
if c.Rules.JobUID {
tags[conventions.AttributeK8SJobUID] = string(ref.UID)
}
Expand Down
12 changes: 10 additions & 2 deletions processor/k8sattributesprocessor/internal/kube/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ func TestExtractionRules(t *testing.T) {
{
APIVersion: "batch/v1",
Kind: "Job",
Name: "pi",
Name: "auth-cronjob-27667920",
UID: "59f27ac1-5c71-42e5-abe9-2c499d603706",
},
{
Expand Down Expand Up @@ -545,7 +545,15 @@ func TestExtractionRules(t *testing.T) {
JobName: true,
},
attributes: map[string]string{
"k8s.job.name": "pi",
"k8s.job.name": "auth-cronjob-27667920",
},
}, {
name: "cronJob",
rules: ExtractionRules{
CronJobName: true,
},
attributes: map[string]string{
"k8s.cronjob.name": "auth-cronjob",
},
}, {
name: "statefulsetUID",
Expand Down
1 change: 1 addition & 0 deletions processor/k8sattributesprocessor/internal/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ type FieldFilter struct {
// ExtractionRules is used to specify the information that needs to be extracted
// from pods and added to the spans as tags.
type ExtractionRules struct {
CronJobName bool
Deployment bool
DaemonSetUID bool
DaemonSetName bool
Expand Down
2 changes: 2 additions & 0 deletions processor/k8sattributesprocessor/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ func withExtractMetadata(fields ...string) option {
p.rules.JobName = true
case conventions.AttributeK8SJobUID:
p.rules.JobUID = true
case conventions.AttributeK8SCronJobName:
p.rules.CronJobName = true
case metadataNode, conventions.AttributeK8SNodeName:
p.rules.Node = true
case conventions.AttributeContainerID:
Expand Down
16 changes: 16 additions & 0 deletions unreleased/k8sattributes-cronjob.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. filelogreceiver)
component: k8sattributesprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Add support for discovering Kubernetes CronJob name"

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

# (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: