Skip to content

Commit

Permalink
Merge pull request cockroachdb#84848 from THardy98/backport21.2-84195
Browse files Browse the repository at this point in the history
release-21.2: sql: Add database ID to sampled query log
  • Loading branch information
Thomas Hardy authored Jul 21, 2022
2 parents b0b7bda + c633d13 commit 0009dbf
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 46 deletions.
1 change: 1 addition & 0 deletions docs/generated/eventlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,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
Expand Down
10 changes: 8 additions & 2 deletions pkg/sql/exec_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,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,
Expand All @@ -386,7 +387,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()
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/telemetry_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ package sql

import (
"context"
gosql "database/sql"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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();")
Expand Down Expand Up @@ -263,6 +269,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 {
Expand Down
9 changes: 9 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.

119 changes: 75 additions & 44 deletions pkg/util/log/eventpb/telemetry.pb.go

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

3 changes: 3 additions & 0 deletions pkg/util/log/eventpb/telemetry.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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
Expand Down

0 comments on commit 0009dbf

Please sign in to comment.