Skip to content

Commit

Permalink
sql: add telemetry for SERIAL expansions
Browse files Browse the repository at this point in the history
This adds telemetry for the SERIAL expansion strategies.

Counter format: `sql.schema.serial.<normtype>.<coltype>`

For example: `sql.schema.serial.rowid.SERIAL2`

Release note (sql change): CockroachDB will now report how the SERIAL
pseudo-type is expanded in table column definitions, when telemetry is
enabled.
  • Loading branch information
knz committed Mar 12, 2019
1 parent df6bba0 commit 558c7be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/server/updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ func TestReportUsage(t *testing.T) {
t.Fatal(err)
}

if _, err := db.Exec(
fmt.Sprintf(`CREATE TABLE %[1]s.%[1]s_s (%[1]s SERIAL2)`, elemName),
); err != nil {
t.Fatal(err)
}

// Run some queries so we have some query statistics collected.
for i := 0; i < 10; i++ {
// Run some sample queries. Each are passed a string and int by Exec.
Expand Down Expand Up @@ -388,7 +394,7 @@ func TestReportUsage(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if actual := len(tables); actual != 1 {
if actual := len(tables); actual != 2 {
t.Fatalf("unexpected table count %d", actual)
}
for _, table := range tables {
Expand Down Expand Up @@ -543,6 +549,9 @@ func TestReportUsage(t *testing.T) {
"test.b": 2,
"test.c": 3,

// SERIAL normalization.
"sql.schema.serial.rowid.SERIAL2": 1,

// Although the query is executed 10 times, due to plan caching
// keyed by the SQL text, the planning only occurs once.
"sql.plan.ops.cast.string::inet": 1,
Expand Down Expand Up @@ -693,6 +702,7 @@ func TestReportUsage(t *testing.T) {
`[false,false,false] SET application_name = $1`,
`[false,false,false] SET application_name = DEFAULT`,
`[false,false,false] SET application_name = _`,
`[true,false,false] CREATE TABLE _ (_ INT8 NOT NULL DEFAULT unique_rowid())`,
`[true,false,false] CREATE TABLE _ (_ INT8, CONSTRAINT _ CHECK (_ > _))`,
`[true,false,false] INSERT INTO _ SELECT unnest(ARRAY[_, _, __more2__])`,
`[true,false,false] INSERT INTO _ VALUES (_), (__more2__)`,
Expand Down Expand Up @@ -743,6 +753,7 @@ func TestReportUsage(t *testing.T) {
`ALTER TABLE _ CONFIGURE ZONE = _`,
`CREATE DATABASE _`,
`CREATE TABLE _ (_ INT8, CONSTRAINT _ CHECK (_ > _))`,
`CREATE TABLE _ (_ INT8 NOT NULL DEFAULT unique_rowid())`,
`CREATE TABLE _ (_ INT8 PRIMARY KEY, _ INT8, INDEX (_) INTERLEAVE IN PARENT _ (_))`,
`INSERT INTO _ VALUES (length($1::STRING)), (__more1__)`,
`INSERT INTO _ VALUES (_), (__more2__)`,
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"context"
"fmt"

"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/log"
)

Expand Down Expand Up @@ -83,6 +85,8 @@ func (p *planner) processSerialInColumnDef(
newSpec.Type = t.TInt
}

telemetry.Inc(sqltelemetry.SerialColumnNormalizationCounter(t.String(), serialNormalizationMode.String()))

if serialNormalizationMode == sessiondata.SerialUsesRowID {
// We're not constructing a sequence for this SERIAL column.
// Use the "old school" CockroachDB default.
Expand Down
29 changes: 29 additions & 0 deletions pkg/sql/sqltelemetry/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package sqltelemetry

import (
"fmt"

"github.com/cockroachdb/cockroach/pkg/server/telemetry"
)

// SerialColumnNormalizationCounter is to be incremented every time
// a SERIAL type is processed in a column definition.
// It includes the normalization type, so we can
// estimate usage of the various normalization strategies.
func SerialColumnNormalizationCounter(inputType, normType string) telemetry.Counter {
return telemetry.GetCounter(fmt.Sprintf("sql.schema.serial.%s.%s", normType, inputType))
}

0 comments on commit 558c7be

Please sign in to comment.