From 5e19b9a79f235d96e2db83c34d975507bae7ca20 Mon Sep 17 00:00:00 2001 From: Oliver Tan Date: Wed, 5 Feb 2020 06:22:03 -0800 Subject: [PATCH] sql: add telemetry for schema change usage commands This PR adds telemetry for usage of various CREATE, ALTER and DROP commands. Telemetry names may be subcategorized by different types, e.g.: ``` sql.schema.alter_table sql.schema.alter_table.add_check_constraint sql.schema.alter_table.add_column sql.schema.alter_table.add_constraint sql.schema.alter_table.column_mutation sql.schema.alter_table.drop_column sql.schema.alter_table.drop_constraint sql.schema.alter_table.set_audit sql.schema.alter_table.validate_constraint ``` Release note: None --- pkg/sql/alter_index.go | 3 + pkg/sql/alter_sequence.go | 3 + pkg/sql/alter_table.go | 7 +- pkg/sql/alter_user.go | 4 + pkg/sql/create_database.go | 3 + pkg/sql/create_index.go | 1 + pkg/sql/create_sequence.go | 3 + pkg/sql/create_stats.go | 1 + pkg/sql/create_table.go | 1 + pkg/sql/create_user.go | 4 + pkg/sql/create_view.go | 4 +- pkg/sql/drop_database.go | 4 + pkg/sql/drop_index.go | 4 + pkg/sql/drop_sequence.go | 4 + pkg/sql/drop_table.go | 4 + pkg/sql/drop_user.go | 4 + pkg/sql/drop_view.go | 4 + .../testdata/logic_test/alter_primary_key | 6 +- .../logictest/testdata/logic_test/alter_table | 1 + .../logictest/testdata/logic_test/zone_config | 10 ++ pkg/sql/sem/tree/alter_table.go | 94 ++++++++++++++++++- pkg/sql/sem/tree/zone.go | 20 ++++ pkg/sql/set_zone_config.go | 5 + pkg/sql/sqltelemetry/schema.go | 32 ++++++- 24 files changed, 215 insertions(+), 11 deletions(-) diff --git a/pkg/sql/alter_index.go b/pkg/sql/alter_index.go index c69863a76279..960497417f5d 100644 --- a/pkg/sql/alter_index.go +++ b/pkg/sql/alter_index.go @@ -13,9 +13,11 @@ package sql import ( "context" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/errors" "github.com/gogo/protobuf/proto" ) @@ -59,6 +61,7 @@ func (n *alterIndexNode) startExec(params runParams) error { for _, cmd := range n.n.Cmds { switch t := cmd.(type) { case *tree.AlterIndexPartitionBy: + telemetry.Inc(sqltelemetry.SchemaChangeAlterWithExtra("index", "partition_by")) partitioning, err := CreatePartitioning( params.ctx, params.extendedEvalCtx.Settings, params.EvalContext(), diff --git a/pkg/sql/alter_sequence.go b/pkg/sql/alter_sequence.go index 34efdb0b8746..d64652eed86e 100644 --- a/pkg/sql/alter_sequence.go +++ b/pkg/sql/alter_sequence.go @@ -13,9 +13,11 @@ package sql import ( "context" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" ) type alterSequenceNode struct { @@ -48,6 +50,7 @@ func (p *planner) AlterSequence(ctx context.Context, n *tree.AlterSequence) (pla func (n *alterSequenceNode) ReadingOwnWrites() {} func (n *alterSequenceNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeAlter("sequence")) desc := n.seqDesc err := assignSequenceOptions(desc.SequenceOpts, n.n.Options, false /* setDefaults */, ¶ms, desc.GetID()) diff --git a/pkg/sql/alter_table.go b/pkg/sql/alter_table.go index 5c52d5c5c01f..c0b0f7cbd294 100644 --- a/pkg/sql/alter_table.go +++ b/pkg/sql/alter_table.go @@ -98,6 +98,8 @@ func (p *planner) AlterTable(ctx context.Context, n *tree.AlterTable) (planNode, func (n *alterTableNode) ReadingOwnWrites() {} func (n *alterTableNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeAlter("table")) + // Commands can either change the descriptor directly (for // alterations that don't require a backfill) or add a mutation to // the list. @@ -107,6 +109,8 @@ func (n *alterTableNode) startExec(params runParams) error { tn := params.p.ResolvedName(n.n.Table) for i, cmd := range n.n.Cmds { + telemetry.Inc(cmd.TelemetryCounter()) + switch t := cmd.(type) { case *tree.AlterTableAddColumn: d := t.ColumnDef @@ -324,9 +328,6 @@ func (n *alterTableNode) startExec(params runParams) error { "session variable experimental_enable_primary_key_changes is set to false, cannot perform primary key change") } - // Increment telemetry about uses of primary key changes. - telemetry.Inc(sqltelemetry.AlterPrimaryKeyCounter) - // Ensure that there is not another primary key change attempted within this transaction. currentMutationID := n.tableDesc.ClusterVersion.NextMutationID for i := range n.tableDesc.Mutations { diff --git a/pkg/sql/alter_user.go b/pkg/sql/alter_user.go index bcd577eee048..611daf3179d8 100644 --- a/pkg/sql/alter_user.go +++ b/pkg/sql/alter_user.go @@ -14,11 +14,13 @@ import ( "context" "github.com/cockroachdb/cockroach/pkg/security" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" ) // alterUserSetPasswordNode represents an ALTER USER ... WITH PASSWORD statement. @@ -61,6 +63,8 @@ type alterUserSetPasswordRun struct { } func (n *alterUserSetPasswordNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeAlter("user")) + normalizedUsername, hashedPassword, err := n.userAuthInfo.resolve() if err != nil { return err diff --git a/pkg/sql/create_database.go b/pkg/sql/create_database.go index 562127e82557..f6529ec0f1df 100644 --- a/pkg/sql/create_database.go +++ b/pkg/sql/create_database.go @@ -14,7 +14,9 @@ import ( "context" "strings" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" ) @@ -73,6 +75,7 @@ func (p *planner) CreateDatabase(ctx context.Context, n *tree.CreateDatabase) (p } func (n *createDatabaseNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeCreate("database")) desc := makeDatabaseDesc(n.n) created, err := params.p.createDatabase(params.ctx, &desc, n.n.IfNotExists) diff --git a/pkg/sql/create_index.go b/pkg/sql/create_index.go index aa5e4dbe2eca..82734213ed9c 100644 --- a/pkg/sql/create_index.go +++ b/pkg/sql/create_index.go @@ -87,6 +87,7 @@ func MakeIndexDescriptor(n *tree.CreateIndex) (*sqlbase.IndexDescriptor, error) func (n *createIndexNode) ReadingOwnWrites() {} func (n *createIndexNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeCreate("index")) _, dropped, err := n.tableDesc.FindIndexByName(string(n.n.Name)) if err == nil { if dropped { diff --git a/pkg/sql/create_sequence.go b/pkg/sql/create_sequence.go index d19212b8abb5..340fccd221d0 100644 --- a/pkg/sql/create_sequence.go +++ b/pkg/sql/create_sequence.go @@ -15,9 +15,11 @@ import ( "github.com/cockroachdb/cockroach/pkg/internal/client" "github.com/cockroachdb/cockroach/pkg/keys" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -50,6 +52,7 @@ func (p *planner) CreateSequence(ctx context.Context, n *tree.CreateSequence) (p func (n *createSequenceNode) ReadingOwnWrites() {} func (n *createSequenceNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeCreate("sequence")) // TODO(arul): Allow temporary sequences once temp tables work for regular tables. if n.n.Temporary { return unimplemented.NewWithIssuef(5807, diff --git a/pkg/sql/create_stats.go b/pkg/sql/create_stats.go index 8501bb2dce24..e6de6be67891 100644 --- a/pkg/sql/create_stats.go +++ b/pkg/sql/create_stats.go @@ -72,6 +72,7 @@ type createStatsRun struct { } func (n *createStatsNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeCreate("stats")) n.run.resultsCh = make(chan tree.Datums) n.run.errCh = make(chan error) go func() { diff --git a/pkg/sql/create_table.go b/pkg/sql/create_table.go index cb0433b65898..6e0d1f97cddf 100644 --- a/pkg/sql/create_table.go +++ b/pkg/sql/create_table.go @@ -154,6 +154,7 @@ func getTableCreateParams( } func (n *createTableNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeCreate("table")) isTemporary := n.n.Temporary tKey, schemaID, err := getTableCreateParams(params, n.dbDesc.ID, isTemporary, n.n.Table.Table()) diff --git a/pkg/sql/create_user.go b/pkg/sql/create_user.go index f897cd38754a..9d27322ff450 100644 --- a/pkg/sql/create_user.go +++ b/pkg/sql/create_user.go @@ -15,11 +15,13 @@ import ( "regexp" "github.com/cockroachdb/cockroach/pkg/security" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/errors" ) @@ -69,6 +71,8 @@ func (p *planner) CreateUserNode( } func (n *CreateUserNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeCreate("user")) + normalizedUsername, hashedPassword, err := n.userAuthInfo.resolve() if err != nil { return err diff --git a/pkg/sql/create_view.go b/pkg/sql/create_view.go index 6f059054f870..465d26cd1b64 100644 --- a/pkg/sql/create_view.go +++ b/pkg/sql/create_view.go @@ -43,8 +43,10 @@ type createViewNode struct { func (n *createViewNode) ReadingOwnWrites() {} func (n *createViewNode) startExec(params runParams) error { - isTemporary := n.temporary + telemetry.Inc(sqltelemetry.SchemaChangeCreate("view")) + viewName := string(n.viewName) + isTemporary := n.temporary log.VEventf(params.ctx, 2, "dependencies for view %s:\n%s", viewName, n.planDeps.String()) // First check the backrefs and see if any of them are temporary. diff --git a/pkg/sql/drop_database.go b/pkg/sql/drop_database.go index 21931ad72215..4edb3e1d3c4c 100644 --- a/pkg/sql/drop_database.go +++ b/pkg/sql/drop_database.go @@ -17,11 +17,13 @@ import ( "github.com/cockroachdb/cockroach/pkg/internal/client" "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" "github.com/cockroachdb/cockroach/pkg/keys" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util/log" ) @@ -121,6 +123,8 @@ func (p *planner) DropDatabase(ctx context.Context, n *tree.DropDatabase) (planN } func (n *dropDatabaseNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeDrop("database")) + ctx := params.ctx p := params.p tbNameStrings := make([]string, 0, len(n.td)) diff --git a/pkg/sql/drop_index.go b/pkg/sql/drop_index.go index 044018e911e4..9aadc27c59c6 100644 --- a/pkg/sql/drop_index.go +++ b/pkg/sql/drop_index.go @@ -16,12 +16,14 @@ import ( "strings" "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/errors" ) @@ -66,6 +68,8 @@ func (p *planner) DropIndex(ctx context.Context, n *tree.DropIndex) (planNode, e func (n *dropIndexNode) ReadingOwnWrites() {} func (n *dropIndexNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeDrop("index")) + ctx := params.ctx for _, index := range n.idxNames { // Need to retrieve the descriptor again for each index name in diff --git a/pkg/sql/drop_sequence.go b/pkg/sql/drop_sequence.go index fd7760bda4e7..75020aa093d4 100644 --- a/pkg/sql/drop_sequence.go +++ b/pkg/sql/drop_sequence.go @@ -13,10 +13,12 @@ package sql import ( "context" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" ) @@ -61,6 +63,8 @@ func (p *planner) DropSequence(ctx context.Context, n *tree.DropSequence) (planN func (n *dropSequenceNode) ReadingOwnWrites() {} func (n *dropSequenceNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeDrop("sequence")) + ctx := params.ctx for _, toDel := range n.td { droppedDesc := toDel.desc diff --git a/pkg/sql/drop_table.go b/pkg/sql/drop_table.go index 91dfa210a36a..12fb375ab8cf 100644 --- a/pkg/sql/drop_table.go +++ b/pkg/sql/drop_table.go @@ -18,9 +18,11 @@ import ( "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/timeutil" @@ -102,6 +104,8 @@ func (p *planner) DropTable(ctx context.Context, n *tree.DropTable) (planNode, e func (n *dropTableNode) ReadingOwnWrites() {} func (n *dropTableNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeDrop("table")) + ctx := params.ctx for _, toDel := range n.td { droppedDesc := toDel.desc diff --git a/pkg/sql/drop_user.go b/pkg/sql/drop_user.go index 744f363e8116..d647ab1eec9f 100644 --- a/pkg/sql/drop_user.go +++ b/pkg/sql/drop_user.go @@ -14,11 +14,13 @@ import ( "context" "github.com/cockroachdb/cockroach/pkg/security" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util" "github.com/pkg/errors" ) @@ -71,6 +73,8 @@ type dropUserRun struct { } func (n *DropUserNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeDrop("user")) + var entryType string if n.isRole { entryType = "role" diff --git a/pkg/sql/drop_view.go b/pkg/sql/drop_view.go index 85d4af2e028e..a0be41b5eb32 100644 --- a/pkg/sql/drop_view.go +++ b/pkg/sql/drop_view.go @@ -14,9 +14,11 @@ import ( "context" "fmt" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/errors" ) @@ -75,6 +77,8 @@ func (p *planner) DropView(ctx context.Context, n *tree.DropView) (planNode, err func (n *dropViewNode) ReadingOwnWrites() {} func (n *dropViewNode) startExec(params runParams) error { + telemetry.Inc(sqltelemetry.SchemaChangeDrop("view")) + ctx := params.ctx for _, toDel := range n.td { droppedDesc := toDel.desc diff --git a/pkg/sql/logictest/testdata/logic_test/alter_primary_key b/pkg/sql/logictest/testdata/logic_test/alter_primary_key index 643744d6a163..5a91759f62c9 100644 --- a/pkg/sql/logictest/testdata/logic_test/alter_primary_key +++ b/pkg/sql/logictest/testdata/logic_test/alter_primary_key @@ -37,9 +37,11 @@ SELECT * from t@primary 9 10 11 12 query T -select feature_name FROM crdb_internal.feature_usage WHERE feature_name = 'sql.schema.alter_primary_key' AND usage_count > 0 +SELECT feature_name FROM crdb_internal.feature_usage +WHERE feature_name IN ('sql.schema.alter_table.alter_primary_key') AND usage_count > 0 +ORDER BY feature_name ---- -sql.schema.alter_primary_key +sql.schema.alter_table.alter_primary_key # Test primary key changes on storing indexes with different column families (the randomizer will do this for us). statement ok diff --git a/pkg/sql/logictest/testdata/logic_test/alter_table b/pkg/sql/logictest/testdata/logic_test/alter_table index b870ca73caa5..ef063959bc2a 100644 --- a/pkg/sql/logictest/testdata/logic_test/alter_table +++ b/pkg/sql/logictest/testdata/logic_test/alter_table @@ -1058,3 +1058,4 @@ ALTER TABLE t43092 ALTER COLUMN x DROP NOT NULL statement ok DROP TABLE t43092 + diff --git a/pkg/sql/logictest/testdata/logic_test/zone_config b/pkg/sql/logictest/testdata/logic_test/zone_config index ad7e515eda6f..1d18fb17e6eb 100644 --- a/pkg/sql/logictest/testdata/logic_test/zone_config +++ b/pkg/sql/logictest/testdata/logic_test/zone_config @@ -131,3 +131,13 @@ query I SELECT zone_id FROM [SHOW ZONE CONFIGURATION FOR TABLE a] ---- 0 + +subtest alter_table_telemetry + +query T +SELECT feature_name FROM crdb_internal.feature_usage +WHERE feature_name IN ('sql.schema.alter_range.configure_zone', 'sql.schema.alter_table.configure_zone') +ORDER BY feature_name +---- +sql.schema.alter_range.configure_zone +sql.schema.alter_table.configure_zone diff --git a/pkg/sql/sem/tree/alter_table.go b/pkg/sql/sem/tree/alter_table.go index 9726f375e648..53875566a24b 100644 --- a/pkg/sql/sem/tree/alter_table.go +++ b/pkg/sql/sem/tree/alter_table.go @@ -10,7 +10,11 @@ package tree -import "github.com/cockroachdb/cockroach/pkg/sql/types" +import ( + "github.com/cockroachdb/cockroach/pkg/server/telemetry" + "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" + "github.com/cockroachdb/cockroach/pkg/sql/types" +) // AlterTable represents an ALTER TABLE statement. type AlterTable struct { @@ -45,6 +49,9 @@ func (node *AlterTableCmds) Format(ctx *FmtCtx) { // AlterTableCmd represents a table modification operation. type AlterTableCmd interface { NodeFormatter + // TelemetryCounter returns the telemetry counter to increment + // when this command is used. + TelemetryCounter() telemetry.Counter // Placeholder function to ensure that only desired types // (AlterTable*) conform to the AlterTableCmd interface. alterTableCmd() @@ -98,6 +105,11 @@ type AlterTableAddColumn struct { ColumnDef *ColumnTableDef } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableAddColumn) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "add_column") +} + // Format implements the NodeFormatter interface. func (node *AlterTableAddColumn) Format(ctx *FmtCtx) { ctx.WriteString(" ADD COLUMN ") @@ -168,6 +180,11 @@ type AlterTableAddConstraint struct { ValidationBehavior ValidationBehavior } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableAddConstraint) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "add_constraint") +} + // Format implements the NodeFormatter interface. func (node *AlterTableAddConstraint) Format(ctx *FmtCtx) { ctx.WriteString(" ADD ") @@ -185,6 +202,11 @@ type AlterTableAlterColumnType struct { Using Expr } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableAlterColumnType) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "alter_column_type") +} + // Format implements the NodeFormatter interface. func (node *AlterTableAlterColumnType) Format(ctx *FmtCtx) { ctx.WriteString(" ALTER COLUMN ") @@ -212,6 +234,11 @@ type AlterTableAlterPrimaryKey struct { Interleave *InterleaveDef } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableAlterPrimaryKey) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "alter_primary_key") +} + // Format implements the NodeFormatter interface. func (node *AlterTableAlterPrimaryKey) Format(ctx *FmtCtx) { ctx.WriteString(" ALTER PRIMARY KEY USING COLUMNS (") @@ -229,6 +256,11 @@ type AlterTableDropColumn struct { DropBehavior DropBehavior } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableDropColumn) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "drop_column") +} + // Format implements the NodeFormatter interface. func (node *AlterTableDropColumn) Format(ctx *FmtCtx) { ctx.WriteString(" DROP COLUMN ") @@ -248,6 +280,11 @@ type AlterTableDropConstraint struct { DropBehavior DropBehavior } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableDropConstraint) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "drop_constraint") +} + // Format implements the NodeFormatter interface. func (node *AlterTableDropConstraint) Format(ctx *FmtCtx) { ctx.WriteString(" DROP CONSTRAINT ") @@ -265,6 +302,11 @@ type AlterTableValidateConstraint struct { Constraint Name } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableValidateConstraint) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "validate_constraint") +} + // Format implements the NodeFormatter interface. func (node *AlterTableValidateConstraint) Format(ctx *FmtCtx) { ctx.WriteString(" VALIDATE CONSTRAINT ") @@ -276,6 +318,11 @@ type AlterTableRenameTable struct { NewName TableName } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableRenameTable) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "rename_table") +} + // Format implements the NodeFormatter interface. func (node *AlterTableRenameTable) Format(ctx *FmtCtx) { ctx.WriteString(" RENAME TO ") @@ -288,6 +335,11 @@ type AlterTableRenameColumn struct { NewName Name } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableRenameColumn) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "rename_column") +} + // Format implements the NodeFormatter interface. func (node *AlterTableRenameColumn) Format(ctx *FmtCtx) { ctx.WriteString(" RENAME COLUMN ") @@ -302,6 +354,11 @@ type AlterTableRenameConstraint struct { NewName Name } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableRenameConstraint) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "rename_constraint") +} + // Format implements the NodeFormatter interface. func (node *AlterTableRenameConstraint) Format(ctx *FmtCtx) { ctx.WriteString(" RENAME CONSTRAINT ") @@ -322,6 +379,11 @@ func (node *AlterTableSetDefault) GetColumn() Name { return node.Column } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableSetDefault) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "set_default") +} + // Format implements the NodeFormatter interface. func (node *AlterTableSetDefault) Format(ctx *FmtCtx) { ctx.WriteString(" ALTER COLUMN ") @@ -345,6 +407,11 @@ func (node *AlterTableSetNotNull) GetColumn() Name { return node.Column } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableSetNotNull) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "set_not_null") +} + // Format implements the NodeFormatter interface. func (node *AlterTableSetNotNull) Format(ctx *FmtCtx) { ctx.WriteString(" ALTER COLUMN ") @@ -363,6 +430,11 @@ func (node *AlterTableDropNotNull) GetColumn() Name { return node.Column } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableDropNotNull) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "drop_not_null") +} + // Format implements the NodeFormatter interface. func (node *AlterTableDropNotNull) Format(ctx *FmtCtx) { ctx.WriteString(" ALTER COLUMN ") @@ -381,6 +453,11 @@ func (node *AlterTableDropStored) GetColumn() Name { return node.Column } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableDropStored) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "drop_stored") +} + // Format implements the NodeFormatter interface. func (node *AlterTableDropStored) Format(ctx *FmtCtx) { ctx.WriteString(" ALTER COLUMN ") @@ -394,6 +471,11 @@ type AlterTablePartitionBy struct { *PartitionBy } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTablePartitionBy) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "partition_by") +} + // Format implements the NodeFormatter interface. func (node *AlterTablePartitionBy) Format(ctx *FmtCtx) { ctx.FormatNode(node.PartitionBy) @@ -423,6 +505,11 @@ type AlterTableSetAudit struct { Mode AuditMode } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableSetAudit) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "set_audit") +} + // Format implements the NodeFormatter interface. func (node *AlterTableSetAudit) Format(ctx *FmtCtx) { ctx.WriteString(" EXPERIMENTAL_AUDIT SET ") @@ -434,6 +521,11 @@ type AlterTableInjectStats struct { Stats Expr } +// TelemetryCounter implements the AlterTableCmd interface. +func (node *AlterTableInjectStats) TelemetryCounter() telemetry.Counter { + return sqltelemetry.SchemaChangeAlterWithExtra("table", "inject_stats") +} + // Format implements the NodeFormatter interface. func (node *AlterTableInjectStats) Format(ctx *FmtCtx) { ctx.WriteString(" INJECT STATISTICS ") diff --git a/pkg/sql/sem/tree/zone.go b/pkg/sql/sem/tree/zone.go index 1776129caa28..889442d460de 100644 --- a/pkg/sql/sem/tree/zone.go +++ b/pkg/sql/sem/tree/zone.go @@ -23,6 +23,26 @@ type ZoneSpecifier struct { Partition Name } +// TelemetryName returns a name fitting for telemetry purposes. +func (node ZoneSpecifier) TelemetryName() string { + if node.NamedZone != "" { + return "range" + } + if node.Database != "" { + return "database" + } + str := "" + if node.Partition != "" { + str = "partition." + } + if node.TargetsIndex() { + str += "index" + } else { + str += "table" + } + return str +} + // TargetsTable returns whether the zone specifier targets a table or a subzone // within a table. func (node ZoneSpecifier) TargetsTable() bool { diff --git a/pkg/sql/set_zone_config.go b/pkg/sql/set_zone_config.go index 6b2bb7147b5d..74bdf67564f7 100644 --- a/pkg/sql/set_zone_config.go +++ b/pkg/sql/set_zone_config.go @@ -22,6 +22,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/server/serverpb" + "github.com/cockroachdb/cockroach/pkg/server/telemetry" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" @@ -297,6 +298,10 @@ func (n *setZoneConfigNode) startExec(params runParams) error { } } + telemetry.Inc( + sqltelemetry.SchemaChangeAlterWithExtra(n.zoneSpecifier.TelemetryName(), "configure_zone"), + ) + // If the specifier is for a table, partition or index, this will // resolve the table descriptor. If the specifier is for a database // or range, this is a no-op and a nil pointer is returned as diff --git a/pkg/sql/sqltelemetry/schema.go b/pkg/sql/sqltelemetry/schema.go index 76b6cf12a79f..6ecbc2d8a5e2 100644 --- a/pkg/sql/sqltelemetry/schema.go +++ b/pkg/sql/sqltelemetry/schema.go @@ -40,10 +40,34 @@ var ( CreateTempViewCounter = telemetry.GetCounterOnce("sql.schema.create_temp_view") ) +// SchemaChangeCreate is to be incremented every time a CREATE +// schema change was made. +func SchemaChangeCreate(typ string) telemetry.Counter { + return telemetry.GetCounter("sql.schema.create_" + typ) +} + +// SchemaChangeDrop is to be incremented every time a DROP +// schema change was made. +func SchemaChangeDrop(typ string) telemetry.Counter { + return telemetry.GetCounter("sql.schema.drop_" + typ) +} + +// SchemaChangeAlter behaves the same as SchemaChangeAlterWithExtra +// but with no extra metadata. +func SchemaChangeAlter(typ string) telemetry.Counter { + return SchemaChangeAlterWithExtra(typ, "") +} + +// SchemaChangeAlterWithExtra is to be incremented for ALTER schema changes. +// `typ` is for declaring which type was altered, e.g. TABLE, DATABASE. +// `extra` can be used for extra trailing useful metadata. +func SchemaChangeAlterWithExtra(typ string, extra string) telemetry.Counter { + if extra != "" { + extra = "." + extra + } + return telemetry.GetCounter(fmt.Sprintf("sql.schema.alter_%s%s", typ, extra)) +} + // SecondaryIndexColumnFamiliesCounter is a counter that is incremented every time // a secondary index that is separated into different column families is created. var SecondaryIndexColumnFamiliesCounter = telemetry.GetCounterOnce("sql.schema.secondary_index_column_families") - -// AlterPrimaryKeyCounter is a counter that is incremented every time the -// ALTER PRIMARY KEY command is used. -var AlterPrimaryKeyCounter = telemetry.GetCounterOnce("sql.schema.alter_primary_key")