From 60789584387276975fc5f66935725694e0ce0580 Mon Sep 17 00:00:00 2001 From: Eric Harmeling Date: Tue, 6 Sep 2022 15:39:09 -0400 Subject: [PATCH] sql: add created_at to index usage stat telemetry This commit adds a creation timestamp to the index usage statistics telemetry. Fixes https://github.com/cockroachdb/cockroach/issues/84458. Release justification: low-risk updates to new functionality Release note(sql change): Added a creation timestamp to index usage statistics telemetry. --- docs/generated/eventlog.md | 1 + pkg/sql/scheduledlogging/captured_index_usage_stats.go | 10 ++++++++-- pkg/util/log/eventpb/json_encode_generated.go | 10 ++++++++++ pkg/util/log/eventpb/telemetry.proto | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/generated/eventlog.md b/docs/generated/eventlog.md index 289f7ba18e21..63769b242f32 100644 --- a/docs/generated/eventlog.md +++ b/docs/generated/eventlog.md @@ -2469,6 +2469,7 @@ An event of type `captured_index_usage_stats` | `IndexType` | | no | | `IsUnique` | | no | | `IsInverted` | | no | +| `CreatedAt` | | no | #### Common fields diff --git a/pkg/sql/scheduledlogging/captured_index_usage_stats.go b/pkg/sql/scheduledlogging/captured_index_usage_stats.go index abaf2836fbcb..a683f5e26b3b 100644 --- a/pkg/sql/scheduledlogging/captured_index_usage_stats.go +++ b/pkg/sql/scheduledlogging/captured_index_usage_stats.go @@ -171,7 +171,7 @@ func captureIndexUsageStats( // Capture index usage statistics for each database. var ok bool - expectedNumDatums := 9 + expectedNumDatums := 10 var allCapturedIndexUsageStats []logpb.EventPayload for _, databaseName := range allDatabaseNames { // Omit index usage statistics on the default databases 'system', @@ -189,7 +189,8 @@ func captureIndexUsageStats( ti.is_unique, ti.is_inverted, total_reads, - last_read + last_read, + ti.created_at FROM %s.crdb_internal.index_usage_statistics AS us JOIN %s.crdb_internal.table_indexes ti ON us.index_id = ti.index_id @@ -233,6 +234,10 @@ func captureIndexUsageStats( if row[8] != tree.DNull { lastRead = tree.MustBeDTimestampTZ(row[8]).Time } + createdAt := time.Time{} + if row[9] != tree.DNull { + createdAt = tree.MustBeDTimestamp(row[9]).Time + } capturedIndexStats := &eventpb.CapturedIndexUsageStats{ TableID: uint32(roachpb.TableID(tableID)), @@ -245,6 +250,7 @@ func captureIndexUsageStats( IndexType: string(indexType), IsUnique: bool(isUnique), IsInverted: bool(isInverted), + CreatedAt: createdAt.String(), } allCapturedIndexUsageStats = append(allCapturedIndexUsageStats, capturedIndexStats) diff --git a/pkg/util/log/eventpb/json_encode_generated.go b/pkg/util/log/eventpb/json_encode_generated.go index be1c0b94095b..f020db42fa0a 100644 --- a/pkg/util/log/eventpb/json_encode_generated.go +++ b/pkg/util/log/eventpb/json_encode_generated.go @@ -734,6 +734,16 @@ func (m *CapturedIndexUsageStats) AppendJSONFields(printComma bool, b redact.Red b = append(b, "\"IsInverted\":true"...) } + if m.CreatedAt != "" { + if printComma { + b = append(b, ',') + } + printComma = true + b = append(b, "\"CreatedAt\":\""...) + b = redact.RedactableBytes(jsonbytes.EncodeString([]byte(b), string(m.CreatedAt))) + b = append(b, '"') + } + return printComma, b } diff --git a/pkg/util/log/eventpb/telemetry.proto b/pkg/util/log/eventpb/telemetry.proto index 9574ca924b77..91c4118037bf 100644 --- a/pkg/util/log/eventpb/telemetry.proto +++ b/pkg/util/log/eventpb/telemetry.proto @@ -171,6 +171,7 @@ message CapturedIndexUsageStats { string index_type = 9 [(gogoproto.jsontag) = ",omitempty", (gogoproto.moretags) = "redact:\"nonsensitive\""]; bool is_unique = 10 [(gogoproto.jsontag) = ",omitempty"]; bool is_inverted = 11 [(gogoproto.jsontag) = ",omitempty"]; + string created_at = 12 [(gogoproto.jsontag) = ",omitempty", (gogoproto.moretags) = "redact:\"nonsensitive\""]; } // CreateChangefeed is an event for any CREATE CHANGEFEED query that