Skip to content

Commit

Permalink
[processor/k8sattributes] add Job attributes (open-telemetry#13116)
Browse files Browse the repository at this point in the history
Add support for discovering K8S Job attributes
  • Loading branch information
povilasv authored Aug 9, 2022
1 parent b33c86d commit f5c8156
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion processor/k8sattributesprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ type ExtractConfig struct {
// k8s.pod.name, k8s.pod.uid, k8s.deployment.name,
// k8s.node.name, k8s.namespace.name, k8s.pod.start_time,
// k8s.replicaset.name, k8s.replicaset.uid,
// k8s.daemonset.name, and k8s.daemonset.uid
// k8s.daemonset.name, k8s.daemonset.uid,
// k8s.job.name, and k8s.job.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
10 changes: 9 additions & 1 deletion processor/k8sattributesprocessor/internal/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ func (c *WatchClient) extractPodAttributes(pod *api_v1.Pod) map[string]string {
}

if c.Rules.ReplicaSetID || c.Rules.ReplicaSetName ||
c.Rules.DaemonSetUID || c.Rules.DaemonSetName {
c.Rules.DaemonSetUID || c.Rules.DaemonSetName ||
c.Rules.JobUID || c.Rules.JobName {
for _, ref := range pod.OwnerReferences {
switch ref.Kind {
case "ReplicaSet":
Expand All @@ -318,6 +319,13 @@ func (c *WatchClient) extractPodAttributes(pod *api_v1.Pod) map[string]string {
if c.Rules.DaemonSetName {
tags[conventions.AttributeK8SDaemonSetName] = ref.Name
}
case "Job":
if c.Rules.JobUID {
tags[conventions.AttributeK8SJobUID] = string(ref.UID)
}
if c.Rules.JobName {
tags[conventions.AttributeK8SJobName] = ref.Name
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions processor/k8sattributesprocessor/internal/kube/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ func TestExtractionRules(t *testing.T) {
Name: "auth-daemonset",
UID: "c94d3814-2253-427a-ab13-2cf609e4dafa",
},
{
APIVersion: "batch/v1",
Kind: "Job",
Name: "pi",
UID: "59f27ac1-5c71-42e5-abe9-2c499d603706",
},
},
},
Spec: api_v1.PodSpec{
Expand Down Expand Up @@ -519,6 +525,22 @@ func TestExtractionRules(t *testing.T) {
attributes: map[string]string{
"k8s.daemonset.name": "auth-daemonset",
},
}, {
name: "jobUID",
rules: ExtractionRules{
JobUID: true,
},
attributes: map[string]string{
"k8s.job.uid": "59f27ac1-5c71-42e5-abe9-2c499d603706",
},
}, {
name: "jobName",
rules: ExtractionRules{
JobName: true,
},
attributes: map[string]string{
"k8s.job.name": "pi",
},
}, {
name: "metadata",
rules: ExtractionRules{
Expand Down
2 changes: 2 additions & 0 deletions processor/k8sattributesprocessor/internal/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ type ExtractionRules struct {
Deployment bool
DaemonSetUID bool
DaemonSetName bool
JobUID bool
JobName bool
Namespace bool
PodName bool
PodUID bool
Expand Down
4 changes: 4 additions & 0 deletions processor/k8sattributesprocessor/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func withExtractMetadata(fields ...string) option {
p.rules.DaemonSetName = true
case conventions.AttributeK8SDaemonSetUID:
p.rules.DaemonSetUID = true
case conventions.AttributeK8SJobName:
p.rules.JobName = true
case conventions.AttributeK8SJobUID:
p.rules.JobUID = true
case metadataNode, conventions.AttributeK8SNodeName:
p.rules.Node = true
case conventions.AttributeContainerID:
Expand Down
16 changes: 16 additions & 0 deletions unreleased/k8sattributes-job.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 Job name and Job UID"

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

0 comments on commit f5c8156

Please sign in to comment.