Skip to content

Commit 3599525

Browse files
committed
Adjust support for OTel and non-OTel
With OTel, we've introduced some changes, most notably to the location of the Postgres logs; but also with the introduction of a `receiver` dir in several locations which only the collector container has access to. This PR addresses these issues by using the cluster to determine the location of the logs, and by excluding checking the `receiver` dir in several execs through the use of a '*.*' wildcard. Issues: [PGO-2358]
1 parent 96de18f commit 3599525

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

internal/cmd/exec.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,22 @@ func (exec Executor) pgBackRestCheck() (string, string, error) {
4848
}
4949

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

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

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

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

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

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

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

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

101119
return stdout.String(), stderr.String(), err

internal/cmd/exec_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,22 @@ func TestListPGLogFiles(t *testing.T) {
7272
assert.Assert(t, stderr != nil, "should capture stderr")
7373
return expected
7474
}
75-
_, _, err := Executor(exec).listPGLogFiles(1)
75+
_, _, err := Executor(exec).listPGLogFiles(1, false)
76+
assert.ErrorContains(t, err, "pass-through")
77+
78+
})
79+
80+
t.Run("has instrumentation", func(t *testing.T) {
81+
expected := errors.New("pass-through")
82+
exec := func(
83+
stdin io.Reader, stdout, stderr io.Writer, command ...string,
84+
) error {
85+
assert.DeepEqual(t, command, []string{"bash", "-ceu", "--", "ls -1dt pgdata/logs/postgres/*.* | head -1"})
86+
assert.Assert(t, stdout != nil, "should capture stdout")
87+
assert.Assert(t, stderr != nil, "should capture stderr")
88+
return expected
89+
}
90+
_, _, err := Executor(exec).listPGLogFiles(1, true)
7691
assert.ErrorContains(t, err, "pass-through")
7792

7893
})
@@ -86,7 +101,7 @@ func TestListPatroniLogFiles(t *testing.T) {
86101
exec := func(
87102
stdin io.Reader, stdout, stderr io.Writer, command ...string,
88103
) error {
89-
assert.DeepEqual(t, command, []string{"bash", "-ceu", "--", "ls -1dt pgdata/patroni/log/*"})
104+
assert.DeepEqual(t, command, []string{"bash", "-ceu", "--", "ls -1dt pgdata/patroni/log/*.*"})
90105
assert.Assert(t, stdout != nil, "should capture stdout")
91106
assert.Assert(t, stderr != nil, "should capture stderr")
92107
return expected

internal/cmd/export.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ Collecting PGO CLI logs...
462462
// All Postgres Logs on the Postgres Instances (primary and replicas)
463463
if numLogs > 0 {
464464
err = gatherPostgresLogsAndConfigs(ctx, clientset, restConfig,
465-
namespace, clusterName, outputDir, outputFile, numLogs, tw, cmd)
465+
namespace, clusterName, outputDir, outputFile, numLogs, tw, cmd, get)
466466
if err != nil {
467467
writeInfo(cmd, fmt.Sprintf("Error gathering Postgres Logs and Config: %s", err))
468468
}
@@ -992,6 +992,7 @@ func gatherPostgresLogsAndConfigs(ctx context.Context,
992992
numLogs int,
993993
tw *tar.Writer,
994994
cmd *cobra.Command,
995+
cluster *unstructured.Unstructured,
995996
) error {
996997
writeInfo(cmd, "Collecting Postgres logs...")
997998

@@ -1029,7 +1030,11 @@ func gatherPostgresLogsAndConfigs(ctx context.Context,
10291030
}
10301031

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

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

0 commit comments

Comments
 (0)