diff --git a/docs/generated/eventlog.md b/docs/generated/eventlog.md index 3627093c512a..7a71d8af4444 100644 --- a/docs/generated/eventlog.md +++ b/docs/generated/eventlog.md @@ -2438,6 +2438,7 @@ contains common SQL event/execution details. | `Database` | Name of the database that initiated the query. | no | | `StatementID` | Statement ID of the query. | no | | `TransactionID` | Transaction ID of the query. | no | +| `DatabaseID` | Database ID of the query. | no | #### Common fields diff --git a/pkg/sql/exec_log.go b/pkg/sql/exec_log.go index c10eddd76361..0fd49a8b3794 100644 --- a/pkg/sql/exec_log.go +++ b/pkg/sql/exec_log.go @@ -384,7 +384,8 @@ func (p *planner) maybeLogStatementInternal( } if telemetryMetrics.maybeUpdateLastEmittedTime(telemetryMetrics.timeNow(), requiredTimeElapsed) { skippedQueries := telemetryMetrics.resetSkippedQueryCount() - p.logOperationalEventsOnlyExternally(ctx, eventLogEntry{event: &eventpb.SampledQuery{ + databaseName := p.CurrentDatabase() + sampledQuery := eventpb.SampledQuery{ CommonSQLExecDetails: execDetails, SkippedQueries: skippedQueries, CostEstimate: p.curPlan.instrumentation.costEstimate, @@ -394,7 +395,12 @@ func (p *planner) maybeLogStatementInternal( Database: p.CurrentDatabase(), StatementID: p.stmt.QueryID.String(), TransactionID: p.txn.ID().String(), - }}) + } + db, _ := p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn, databaseName, tree.DatabaseLookupFlags{Required: true}) + if db != nil { + sampledQuery.DatabaseID = uint32(db.GetID()) + } + p.logOperationalEventsOnlyExternally(ctx, eventLogEntry{event: &sampledQuery}) } else { telemetryMetrics.incSkippedQueryCount() } diff --git a/pkg/sql/telemetry_logging_test.go b/pkg/sql/telemetry_logging_test.go index 3eaa39c71518..349c953330d5 100644 --- a/pkg/sql/telemetry_logging_test.go +++ b/pkg/sql/telemetry_logging_test.go @@ -12,9 +12,11 @@ package sql import ( "context" + gosql "database/sql" "fmt" "math" "regexp" + "strconv" "strings" "testing" "time" @@ -94,10 +96,14 @@ func TestTelemetryLogging(t *testing.T) { var sessionID string var databaseName string + var dbID uint32 db := sqlutils.MakeSQLRunner(sqlDB) + conn := db.DB.(*gosql.DB) + db.QueryRow(t, `SHOW session_id`).Scan(&sessionID) db.QueryRow(t, `SHOW database`).Scan(&databaseName) + dbID = sqlutils.QueryDatabaseID(t, conn, 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();") @@ -268,6 +274,9 @@ func TestTelemetryLogging(t *testing.T) { if !strings.Contains(e.Message, "\"Database\":\""+databaseName+"\"") { t.Errorf("expected to find Database: %s", databaseName) } + if !strings.Contains(e.Message, "\"DatabaseID\":"+strconv.Itoa(int(dbID))) { + t.Errorf("expected to find DatabaseID: %v", dbID) + } } } if logCount != expectedLogCount { diff --git a/pkg/util/log/eventpb/json_encode_generated.go b/pkg/util/log/eventpb/json_encode_generated.go index 6c8b299a25bf..1ee4b674af53 100644 --- a/pkg/util/log/eventpb/json_encode_generated.go +++ b/pkg/util/log/eventpb/json_encode_generated.go @@ -3276,6 +3276,15 @@ func (m *SampledQuery) AppendJSONFields(printComma bool, b redact.RedactableByte b = append(b, '"') } + if m.DatabaseID != 0 { + if printComma { + b = append(b, ',') + } + printComma = true + b = append(b, "\"DatabaseID\":"...) + b = strconv.AppendUint(b, uint64(m.DatabaseID), 10) + } + return printComma, b } diff --git a/pkg/util/log/eventpb/telemetry.proto b/pkg/util/log/eventpb/telemetry.proto index 31724be822c6..99cff40b72b2 100644 --- a/pkg/util/log/eventpb/telemetry.proto +++ b/pkg/util/log/eventpb/telemetry.proto @@ -58,6 +58,9 @@ message SampledQuery { // Transaction ID of the query. string transaction_id = 11 [(gogoproto.customname) = "TransactionID", (gogoproto.jsontag) = ',omitempty', (gogoproto.moretags) = "redact:\"nonsensitive\""]; + + // Database ID of the query. + uint32 database_id = 12 [(gogoproto.customname) = "DatabaseID", (gogoproto.jsontag) = ",omitempty"]; } // CapturedIndexUsageStats