Skip to content

Commit

Permalink
Merge pull request #84076 from THardy98/backport21.2-83938
Browse files Browse the repository at this point in the history
release-21.2: sql: add identifiers to sampled query
  • Loading branch information
Thomas Hardy authored Jul 8, 2022
2 parents 8297320 + ca7d8d4 commit ca43cb9
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 39 deletions.
4 changes: 4 additions & 0 deletions docs/generated/eventlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,10 @@ contains common SQL event/execution details.
| `SkippedQueries` | skipped_queries indicate how many SQL statements were not considered for sampling prior to this one. If the field is omitted, or its value is zero, this indicates that no statement was omitted since the last event. | no |
| `CostEstimate` | Cost of the query as estimated by the optimizer. | no |
| `Distribution` | The distribution of the DistSQL query plan (local, full, or partial). | no |
| `SessionID` | SessionID is the ID of the session that initiated the query. | no |
| `Database` | Name of the database that initiated the query. | no |
| `StatementID` | Statement ID of the query. | no |
| `TransactionID` | Transaction ID of the query. | no |


#### Common fields
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/exec_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ func (p *planner) maybeLogStatementInternal(
SkippedQueries: skippedQueries,
CostEstimate: p.curPlan.instrumentation.costEstimate,
Distribution: p.curPlan.instrumentation.distribution.String(),
SessionID: p.extendedEvalCtx.SessionID.String(),
Database: p.CurrentDatabase(),
StatementID: p.stmt.QueryID.String(),
TransactionID: p.txn.ID().String(),
}})
} else {
telemetryMetrics.incSkippedQueryCount()
Expand Down
21 changes: 21 additions & 0 deletions pkg/sql/telemetry_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ func TestTelemetryLogging(t *testing.T) {

defer s.Stopper().Stop(context.Background())

var sessionID string
var databaseName string

db := sqlutils.MakeSQLRunner(sqlDB)
db.QueryRow(t, `SHOW session_id`).Scan(&sessionID)
db.QueryRow(t, `SHOW database`).Scan(&databaseName)
db.Exec(t, `SET application_name = 'telemetry-logging-test'`)
db.Exec(t, `SET CLUSTER SETTING sql.telemetry.query_sampling.enabled = true;`)
db.Exec(t, "CREATE TABLE t();")
Expand Down Expand Up @@ -231,6 +236,16 @@ func TestTelemetryLogging(t *testing.T) {
if !distRe.MatchString(e.Message) {
t.Errorf("expected to find Distribution but none was found")
}
// Match StatementID on any non-empty string value.
stmtID := regexp.MustCompile("\"StatementID\":(\"\\S+\")")
if !stmtID.MatchString(e.Message) {
t.Errorf("expected to find StatementID but none was found in: %s", e.Message)
}
// Match TransactionID on any non-empty string value.
txnID := regexp.MustCompile("\"TransactionID\":(\"\\S+\")")
if !txnID.MatchString(e.Message) {
t.Errorf("expected to find TransactionID but none was found in: %s", e.Message)
}
for _, eTag := range tc.expectedUnredactedTags {
for _, tag := range strings.Split(e.Tags, ",") {
kv := strings.Split(tag, "=")
Expand All @@ -242,6 +257,12 @@ func TestTelemetryLogging(t *testing.T) {
if !strings.Contains(e.Message, "\"ApplicationName\":\""+tc.expectedApplicationName+"\"") {
t.Errorf("expected to find unredacted Application Name: %s", tc.expectedApplicationName)
}
if !strings.Contains(e.Message, "\"SessionID\":\""+sessionID+"\"") {
t.Errorf("expected to find sessionID: %s", sessionID)
}
if !strings.Contains(e.Message, "\"Database\":\""+databaseName+"\"") {
t.Errorf("expected to find Database: %s", databaseName)
}
}
}
if logCount != expectedLogCount {
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/log/eventpb/json_encode_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ca43cb9

Please sign in to comment.