Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 23 additions & 5 deletions internal/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ func (exec Executor) pgBackRestCheck() (string, string, error) {
}

// postgresqlListLogFiles returns the full path of numLogs log files.
func (exec Executor) listPGLogFiles(numLogs int) (string, string, error) {
func (exec Executor) listPGLogFiles(numLogs int, hasInstrumentation bool) (string, string, error) {
var stdout, stderr bytes.Buffer

command := fmt.Sprintf("ls -1dt pgdata/pg[0-9][0-9]/log/* | head -%d", numLogs)
location := "pgdata/pg[0-9][0-9]/log/*"
if hasInstrumentation {
location = "pgdata/logs/postgres/*.*"
}
// Check both the older and the newer log locations.
// If a cluster has used both locations, this will return logs from both.
// If a cluster does not have one location or the other, continue without error.
// This ensures we get as many logs as possible for whatever combination of
// log files exists on this cluster.
// Note the "*.*" pattern to exclude the `receiver` directory,
// which only the collector container has permission to access.
command := fmt.Sprintf("ls -1dt %s | head -%d",
location, numLogs)
err := exec(nil, &stdout, &stderr, "bash", "-ceu", "--", command)

return stdout.String(), stderr.String(), err
Expand All @@ -73,7 +85,9 @@ func (exec Executor) listPGConfFiles() (string, string, error) {
func (exec Executor) listBackrestLogFiles() (string, string, error) {
var stdout, stderr bytes.Buffer

command := "ls -1dt pgdata/pgbackrest/log/*"
// Note the "*.*" pattern to exclude the `receiver` directory,
// which only the collector container has permission to access.
command := "ls -1dt pgdata/pgbackrest/log/*.*"
err := exec(nil, &stdout, &stderr, "bash", "-ceu", "--", command)

return stdout.String(), stderr.String(), err
Expand All @@ -84,7 +98,9 @@ func (exec Executor) listBackrestLogFiles() (string, string, error) {
func (exec Executor) listPatroniLogFiles() (string, string, error) {
var stdout, stderr bytes.Buffer

command := "ls -1dt pgdata/patroni/log/*"
// Note the "*.*" pattern to exclude the `receiver` directory,
// which only the collector container has permission to access.
command := "ls -1dt pgdata/patroni/log/*.*"
err := exec(nil, &stdout, &stderr, "bash", "-ceu", "--", command)

return stdout.String(), stderr.String(), err
Expand All @@ -95,7 +111,9 @@ func (exec Executor) listPatroniLogFiles() (string, string, error) {
func (exec Executor) listBackrestRepoHostLogFiles() (string, string, error) {
var stdout, stderr bytes.Buffer

command := "ls -1dt pgbackrest/*/log/*"
// Note the "*.*" pattern to exclude the `receiver` directory,
// which only the collector container has permission to access.
command := "ls -1dt pgbackrest/*/log/*.*"
err := exec(nil, &stdout, &stderr, "bash", "-ceu", "--", command)

return stdout.String(), stderr.String(), err
Expand Down
19 changes: 17 additions & 2 deletions internal/cmd/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,22 @@ func TestListPGLogFiles(t *testing.T) {
assert.Assert(t, stderr != nil, "should capture stderr")
return expected
}
_, _, err := Executor(exec).listPGLogFiles(1)
_, _, err := Executor(exec).listPGLogFiles(1, false)
assert.ErrorContains(t, err, "pass-through")

})

t.Run("has instrumentation", func(t *testing.T) {
expected := errors.New("pass-through")
exec := func(
stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
assert.DeepEqual(t, command, []string{"bash", "-ceu", "--", "ls -1dt pgdata/logs/postgres/*.* | head -1"})
assert.Assert(t, stdout != nil, "should capture stdout")
assert.Assert(t, stderr != nil, "should capture stderr")
return expected
}
_, _, err := Executor(exec).listPGLogFiles(1, true)
assert.ErrorContains(t, err, "pass-through")

})
Expand All @@ -86,7 +101,7 @@ func TestListPatroniLogFiles(t *testing.T) {
exec := func(
stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
assert.DeepEqual(t, command, []string{"bash", "-ceu", "--", "ls -1dt pgdata/patroni/log/*"})
assert.DeepEqual(t, command, []string{"bash", "-ceu", "--", "ls -1dt pgdata/patroni/log/*.*"})
assert.Assert(t, stdout != nil, "should capture stdout")
assert.Assert(t, stderr != nil, "should capture stderr")
return expected
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ Collecting PGO CLI logs...
// All Postgres Logs on the Postgres Instances (primary and replicas)
if numLogs > 0 {
err = gatherPostgresLogsAndConfigs(ctx, clientset, restConfig,
namespace, clusterName, outputDir, outputFile, numLogs, tw, cmd)
namespace, clusterName, outputDir, outputFile, numLogs, tw, cmd, get)
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error gathering Postgres Logs and Config: %s", err))
}
Expand Down Expand Up @@ -992,6 +992,7 @@ func gatherPostgresLogsAndConfigs(ctx context.Context,
numLogs int,
tw *tar.Writer,
cmd *cobra.Command,
cluster *unstructured.Unstructured,
) error {
writeInfo(cmd, "Collecting Postgres logs...")

Expand Down Expand Up @@ -1029,7 +1030,11 @@ func gatherPostgresLogsAndConfigs(ctx context.Context,
}

// Get Postgres Log Files
stdout, stderr, err := Executor(exec).listPGLogFiles(numLogs)
// Pass a boolean based on whether the cluster has instrumentation
// since that will determine where the log files are stored
_, hasInstrumentation, _ := unstructured.NestedMap(cluster.Object,
"spec", "instrumentation")
stdout, stderr, err := Executor(exec).listPGLogFiles(numLogs, hasInstrumentation)

// Depending upon the list* function above:
// An error may happen when err is non-nil or stderr is non-empty.
Expand Down
Loading