Skip to content

feat: add timestamps flag to logs collector #1776

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

Merged
merged 2 commits into from
Apr 17, 2025
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
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_collectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ spec:
items:
type: string
type: array
timestamps:
type: boolean
required:
- selector
type: object
Expand Down
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2382,6 +2382,8 @@ spec:
items:
type: string
type: array
timestamps:
type: boolean
required:
- selector
type: object
Expand Down
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,8 @@ spec:
items:
type: string
type: array
timestamps:
type: boolean
required:
- selector
type: object
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/collector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Logs struct {
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
ContainerNames []string `json:"containerNames,omitempty" yaml:"containerNames,omitempty"`
Limits *LogLimits `json:"limits,omitempty" yaml:"limits,omitempty"`
Timestamps bool `json:"timestamps,omitempty" yaml:"timestamps,omitempty"`
}

type Data struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/collect/cluster_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (c *CollectClusterResources) Collect(progressChan chan<- interface{}) (Coll
// that is too old/not relevant.
MaxBytes: 5000000,
}
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, "", container.Name, limits, false, false)
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, "", container.Name, limits, false, false, false)
if err != nil {
errPath := filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_PODS_LOGS, pod.Namespace, pod.Name, fmt.Sprintf("%s-logs-errors.log", container.Name))
output.SaveResult(c.BundlePath, errPath, bytes.NewBuffer([]byte(err.Error())))
Expand Down
10 changes: 6 additions & 4 deletions pkg/collect/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *CollectLogs) CollectWithClient(progressChan chan<- interface{}, client
}

for _, containerName := range containerNames {
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true)
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true, c.Collector.Timestamps)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
klog.Errorf("Pod logs timed out for pod %s and container %s: %v", pod.Name, containerName, err)
Expand All @@ -100,7 +100,7 @@ func (c *CollectLogs) CollectWithClient(progressChan chan<- interface{}, client
}
} else {
for _, containerName := range c.Collector.ContainerNames {
containerLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true)
containerLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true, c.Collector.Timestamps)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
klog.Errorf("Pod logs timed out for pod %s and container %s: %v", pod.Name, containerName, err)
Expand Down Expand Up @@ -144,10 +144,12 @@ func savePodLogs(
limits *troubleshootv1beta2.LogLimits,
follow bool,
createSymLinks bool,
timestamps bool,
) (CollectorResult, error) {
podLogOpts := corev1.PodLogOptions{
Follow: follow,
Container: container,
Follow: follow,
Container: container,
Timestamps: timestamps,
}

result := NewResult()
Expand Down
13 changes: 12 additions & 1 deletion pkg/collect/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func Test_savePodLogs(t *testing.T) {
withContainerName bool
collectorName string
createSymLinks bool
timestamps bool
want CollectorResult
}{
{
Expand Down Expand Up @@ -150,6 +151,16 @@ func Test_savePodLogs(t *testing.T) {
"cluster-resources/pods/logs/my-namespace/test-pod/nginx-previous.log": []byte("fake logs"),
},
},
{
name: "with timestamps",
withContainerName: true,
collectorName: "all-logs",
timestamps: true,
want: CollectorResult{
"cluster-resources/pods/logs/my-namespace/test-pod/nginx.log": []byte("fake logs"),
"cluster-resources/pods/logs/my-namespace/test-pod/nginx-previous.log": []byte("fake logs"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -165,7 +176,7 @@ func Test_savePodLogs(t *testing.T) {
if !tt.withContainerName {
containerName = ""
}
got, err := savePodLogs(ctx, "", client, pod, tt.collectorName, containerName, limits, false, tt.createSymLinks)
got, err := savePodLogs(ctx, "", client, pod, tt.collectorName, containerName, limits, false, tt.createSymLinks, tt.timestamps)
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/collect/run_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func runWithoutTimeout(ctx context.Context, bundlePath string, clientConfig *res
MaxLines: 10000,
MaxBytes: 5000000,
}
podLogs, err := savePodLogs(ctx, bundlePath, client, pod, collectorName, "", &limits, true, true)
podLogs, err := savePodLogs(ctx, bundlePath, client, pod, collectorName, "", &limits, true, true, false)
if err != nil {
return nil, errors.Wrap(err, "failed to get pod logs")
}
Expand Down
3 changes: 3 additions & 0 deletions schemas/collector-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,9 @@
"items": {
"type": "string"
}
},
"timestamps": {
"type": "boolean"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions schemas/preflight-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3578,6 +3578,9 @@
"items": {
"type": "string"
}
},
"timestamps": {
"type": "boolean"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3624,6 +3624,9 @@
"items": {
"type": "string"
}
},
"timestamps": {
"type": "boolean"
}
}
},
Expand Down
Loading