pingcap / pingcap/tidb

Add RedactAny and RedactArgs helper functions to redact package

Open
#64,410 2 comments 0 reactions 0 assignees View on GitHub
contribution first-time-contributor
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

### Feature Request

**Problem:**

The TiDB redact package currently provides `Value()` and `Key()` functions for redacting specific types of data. However, downstream projects like TiCDC need additional helper functions to support comprehensive log redaction across different scenarios:

1. **Generic value redaction**: Need to redact arbitrary types (integers, strings, booleans, etc.) without type-specific logic
2. **SQL argument formatting**: Need to format and redact SQL parameter lists for error messages and debug logs

**Proposed Solution:**

Add two new helper functions to `pkg/util/redact`:

**1. `RedactAny(value any) string`**
- Accept any value type and apply redaction
- Handle nil values gracefully
- Use consistent redaction behavior with existing functions

**2. `RedactArgs(args []interface{}) string`**
- Format SQL arguments as a comma-separated list
- Apply redaction to each argument
- Return formatted string like `(?, ?, ?)` when redaction is ON

**Use Case:**

TiCDC logs SQL execution details and error messages that contain sensitive data:
```go
log.Info("Failed to execute DML",
zap.String("query", redact.String(sql)),
zap.String("args", redact.RedactArgs(args))) // Need this!
```

Error messages with query parameters:
```go
err := fmt.Errorf("MySQL txn error: query=%s, args:%s",
redact.String(query),
redact.RedactArgs(args)) // Need this!
```

**Benefits:**
- Centralize redaction logic in TiDB for ecosystem-wide consistency
- Simplify redaction implementation in downstream projects
- Ensure comprehensive protection of sensitive data in logs

**Related:**
- TiCDC log redaction implementation: https://github.com/pingcap/ticdc/issues/2918

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.