Files
troubleshoot/pkg/collect/postgres.go
Andrew Lavery d016e3269c add global and per-collector redactors
add redact type, and begin wiring global redactors

use per-collector redactors

add a test of the 'data' collector and redaction

handle literal string replacements

remove redundant types and redact calls

add proper redactor type, foundations of global redactors

accept global redactors from the CLI, include sample redaction spec
2020-04-16 14:03:00 -04:00

47 lines
1.1 KiB
Go

package collect
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
"github.com/pkg/errors"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
)
func Postgres(ctx *Context, databaseCollector *troubleshootv1beta1.Database) (map[string][]byte, error) {
databaseConnection := DatabaseConnection{}
db, err := sql.Open("postgres", databaseCollector.URI)
if err != nil {
databaseConnection.Error = err.Error()
} else {
query := `select version()`
row := db.QueryRow(query)
version := ""
if err := row.Scan(&version); err != nil {
databaseConnection.Error = err.Error()
} else {
databaseConnection.IsConnected = true
databaseConnection.Version = version
}
}
b, err := json.Marshal(databaseConnection)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal database connection")
}
collectorName := databaseCollector.CollectorName
if collectorName == "" {
collectorName = "postgres"
}
postgresOutput := map[string][]byte{
fmt.Sprintf("postgres/%s.json", collectorName): b,
}
return postgresOutput, nil
}