You can import and use workload-exporter in your Go projects as a library.
go get github.com/cockroachlabs/workload-exporter@latestOr specify a specific version:
go get github.com/cockroachlabs/workload-exporter@v1.4.0package main
import (
"log"
"time"
"github.com/cockroachlabs/workload-exporter/pkg/export"
)
func main() {
// Create exporter configuration
config := export.Config{
ConnectionString: "postgresql://user:password@host:26257/?sslmode=verify-full",
OutputFile: "my-export.zip",
TimeRange: export.TimeRange{
Start: time.Now().Add(-2 * time.Hour),
End: time.Now(),
},
}
// Initialize exporter
exporter, err := export.NewExporter(config)
if err != nil {
log.Fatalf("Failed to create exporter: %v", err)
}
defer exporter.Close()
// Perform export
if err := exporter.Export(); err != nil {
log.Fatalf("Export failed: %v", err)
}
log.Println("Export completed successfully")
}Configuration for the export operation.
Fields:
ConnectionString(string): PostgreSQL connection URL for CockroachDBOutputFile(string): Path to output zip fileTimeRange(TimeRange): Time window for filtering statistics data
Defines the time window for filtering exported data.
Fields:
Start(time.Time): Beginning of time range (inclusive)End(time.Time): End of time range (inclusive)
Handles the export of workload data from a CockroachDB cluster.
Fields:
Config(Config): Export configuration settingsDb(*pgx.Conn): Active database connectionCleanConnectionString(string): Connection string with password redacted
Creates a new Exporter instance with the given configuration.
Parameters:
config: Export configuration
Returns:
*Exporter: Exporter instanceerror: Error if connection fails or configuration is invalid
Example:
exporter, err := export.NewExporter(config)
if err != nil {
return err
}
defer exporter.Close()Performs the complete export operation.
Returns:
error: Error if any step of the export process fails
Example:
if err := exporter.Export(); err != nil {
log.Fatalf("Export failed: %v", err)
}Closes the database connection.
Returns:
error: Error if closing the connection fails
Example:
defer exporter.Close()// Export last 24 hours
config := export.Config{
ConnectionString: connStr,
OutputFile: "daily-export.zip",
TimeRange: export.TimeRange{
Start: time.Now().Add(-24 * time.Hour),
End: time.Now(),
},
}// Export specific date range
start, _ := time.Parse(time.RFC3339, "2025-01-01T00:00:00Z")
end, _ := time.Parse(time.RFC3339, "2025-01-31T23:59:59Z")
config := export.Config{
ConnectionString: connStr,
OutputFile: "january-export.zip",
TimeRange: export.TimeRange{
Start: start,
End: end,
},
}exporter, err := export.NewExporter(config)
if err != nil {
if strings.Contains(err.Error(), "connection refused") {
log.Fatal("Cannot connect to database - is the cluster running?")
}
log.Fatalf("Failed to create exporter: %v", err)
}
defer exporter.Close()
if err := exporter.Export(); err != nil {
if strings.Contains(err.Error(), "permission denied") {
log.Fatal("User lacks required permissions")
}
log.Fatalf("Export failed: %v", err)
}For complete API documentation, see pkg.go.dev.
The library supports CockroachDB versions 24.1 and later. For CockroachDB 26.1+, the library automatically enables the allow_unsafe_internals setting required to access crdb_internal tables.