Skip to content

Commit fae8c94

Browse files
kyle-a-wongdhartunian
authored andcommitted
log, server: Create new log.EventLog() API
This PR introduces a new log.EventLog() API that gives first class support for writing to the system.eventlog table. This API lives in the log package and doesn't have a dependency on the sql package, making it easy to use across the repo. This is implemented using a new set of interfaces in the log package that allows the system and tenant servers to register new event log writers. The logic for writing to the event log table is the same as that which is sql/event_log.go. For now the code is duplicated, but it will be removed in a future commit when this file is updated to use the new API. Epic: CRDB-42978 Release note: None
1 parent 7ca9db8 commit fae8c94

28 files changed

+776
-38
lines changed

pkg/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ ALL_TESTS = [
753753
"//pkg/util/json:json_test",
754754
"//pkg/util/jsonpath/parser:parser_test",
755755
"//pkg/util/limit:limit_test",
756+
"//pkg/util/log/eventlog:eventlog_test",
756757
"//pkg/util/log/eventpb:eventpb_test",
757758
"//pkg/util/log/logconfig:logconfig_test",
758759
"//pkg/util/log/logcrash:logcrash_test",
@@ -2624,6 +2625,8 @@ GO_TARGETS = [
26242625
"//pkg/util/limit:limit",
26252626
"//pkg/util/limit:limit_test",
26262627
"//pkg/util/log/channel:channel",
2628+
"//pkg/util/log/eventlog:eventlog",
2629+
"//pkg/util/log/eventlog:eventlog_test",
26272630
"//pkg/util/log/eventpb/eventpbgen:eventpbgen",
26282631
"//pkg/util/log/eventpb/eventpbgen:eventpbgen_lib",
26292632
"//pkg/util/log/eventpb:eventpb",

pkg/base/serverident/server_ident.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ type ServerIdentificationPayload interface {
5050
ServerIdentityString(key ServerIdentificationKey) string
5151
}
5252

53+
type ServerIdentifier interface {
54+
GetServerIdentificationPayload() ServerIdentificationPayload
55+
}
56+
5357
// ServerIdentificationKey represents a possible parameter to the
5458
// ServerIdentityString() method in ServerIdentificationPayload.
5559
type ServerIdentificationKey int

pkg/ccl/telemetryccl/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ go_test(
3030
"//pkg/testutils/testcluster",
3131
"//pkg/util/leaktest",
3232
"//pkg/util/log",
33+
"//pkg/util/log/eventlog",
3334
"//pkg/util/log/eventpb",
3435
"//pkg/util/log/logpb",
3536
"//pkg/util/log/logtestutils",

pkg/ccl/telemetryccl/telemetry_logging_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
2929
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
3030
"github.com/cockroachdb/cockroach/pkg/util/log"
31+
"github.com/cockroachdb/cockroach/pkg/util/log/eventlog"
3132
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
3233
"github.com/cockroachdb/cockroach/pkg/util/log/logpb"
3334
"github.com/cockroachdb/cockroach/pkg/util/log/logtestutils"
@@ -218,7 +219,7 @@ func TestBulkJobTelemetryLogging(t *testing.T) {
218219
testCluster := serverutils.StartCluster(t, 1, base.TestClusterArgs{
219220
ServerArgs: base.TestServerArgs{
220221
Knobs: base.TestingKnobs{
221-
EventLog: &sql.EventLogTestingKnobs{
222+
EventLog: &eventlog.EventLogTestingKnobs{
222223
// The sampling checks below need to have a deterministic
223224
// number of statements run by internal executor.
224225
SyncWrites: true,

pkg/server/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ go_library(
315315
"//pkg/util/json",
316316
"//pkg/util/limit",
317317
"//pkg/util/log",
318+
"//pkg/util/log/eventlog",
318319
"//pkg/util/log/eventpb",
319320
"//pkg/util/log/logcrash",
320321
"//pkg/util/log/logmetrics",

pkg/server/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import (
107107
"github.com/cockroachdb/cockroach/pkg/util/hlc"
108108
"github.com/cockroachdb/cockroach/pkg/util/hlc/logger"
109109
"github.com/cockroachdb/cockroach/pkg/util/log"
110+
"github.com/cockroachdb/cockroach/pkg/util/log/eventlog"
110111
"github.com/cockroachdb/cockroach/pkg/util/log/logmetrics"
111112
"github.com/cockroachdb/cockroach/pkg/util/metric"
112113
"github.com/cockroachdb/cockroach/pkg/util/mon"
@@ -1886,6 +1887,11 @@ func (s *topLevelServer) PreStart(ctx context.Context) error {
18861887

18871888
advHTTPAddrU := util.NewUnresolvedAddr("tcp", s.cfg.HTTPAdvertiseAddr)
18881889

1890+
// Registers an event log writer for the system tenant. This will enable the
1891+
// ability to persist structured events to the system tenant's
1892+
//system.eventlog table.
1893+
eventlog.Register(ctx, s.cfg.TestingKnobs.EventLog, s.node.execCfg.InternalDB, s.stopper, s.cfg.AmbientCtx, s.ClusterSettings())
1894+
18891895
if err := s.node.start(
18901896
ctx, workersCtx,
18911897
advAddrU,

pkg/server/server_sql.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ import (
121121
"github.com/cockroachdb/cockroach/pkg/util/envutil"
122122
"github.com/cockroachdb/cockroach/pkg/util/hlc"
123123
"github.com/cockroachdb/cockroach/pkg/util/log"
124+
"github.com/cockroachdb/cockroach/pkg/util/log/eventlog"
124125
"github.com/cockroachdb/cockroach/pkg/util/metric"
125126
"github.com/cockroachdb/cockroach/pkg/util/mon"
126127
"github.com/cockroachdb/cockroach/pkg/util/netutil"
@@ -1116,7 +1117,7 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
11161117
execCfg.SQLStatsTestingKnobs = sqlStatsKnobs.(*sqlstats.TestingKnobs)
11171118
}
11181119
if eventlogKnobs := cfg.TestingKnobs.EventLog; eventlogKnobs != nil {
1119-
execCfg.EventLogTestingKnobs = eventlogKnobs.(*sql.EventLogTestingKnobs)
1120+
execCfg.EventLogTestingKnobs = eventlogKnobs.(*eventlog.EventLogTestingKnobs)
11201121
}
11211122
if telemetryLoggingKnobs := cfg.TestingKnobs.TelemetryLoggingKnobs; telemetryLoggingKnobs != nil {
11221123
execCfg.TelemetryLoggingTestingKnobs = telemetryLoggingKnobs.(*sql.TelemetryLoggingTestingKnobs)

pkg/server/tenant.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import (
6868
"github.com/cockroachdb/cockroach/pkg/util/admission"
6969
"github.com/cockroachdb/cockroach/pkg/util/hlc"
7070
"github.com/cockroachdb/cockroach/pkg/util/log"
71+
"github.com/cockroachdb/cockroach/pkg/util/log/eventlog"
7172
"github.com/cockroachdb/cockroach/pkg/util/log/logmetrics"
7273
"github.com/cockroachdb/cockroach/pkg/util/metric"
7374
"github.com/cockroachdb/cockroach/pkg/util/netutil"
@@ -104,7 +105,7 @@ type SQLServerWrapper struct {
104105
db *kv.DB
105106

106107
// Metric registries.
107-
// See the explanatory comments in server.go and status/recorder.g o
108+
// See the explanatory comments in server.go and status/recorder.go
108109
// for details.
109110
registry *metric.Registry
110111
sysRegistry *metric.Registry
@@ -730,6 +731,10 @@ func (s *SQLServerWrapper) PreStart(ctx context.Context) error {
730731
})
731732
})
732733

734+
// Registers an event log writer for the tenant. This will enable the ability
735+
// to persist structured events to the tenants system.eventlog table.
736+
eventlog.Register(ctx, s.cfg.TestingKnobs.EventLog, s.sqlServer.execCfg.InternalDB, s.stopper, s.cfg.AmbientCtx, s.ClusterSettings())
737+
733738
// Init a log metrics registry.
734739
logRegistry := logmetrics.NewRegistry()
735740
if logRegistry == nil {

pkg/sql/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ go_library(
581581
"//pkg/util/json",
582582
"//pkg/util/jsonpath/parser",
583583
"//pkg/util/log",
584+
"//pkg/util/log/eventlog",
584585
"//pkg/util/log/eventpb",
585586
"//pkg/util/log/logcrash",
586587
"//pkg/util/log/logpb",
@@ -948,6 +949,7 @@ go_test(
948949
"//pkg/util/leaktest",
949950
"//pkg/util/log",
950951
"//pkg/util/log/channel",
952+
"//pkg/util/log/eventlog",
951953
"//pkg/util/log/eventpb",
952954
"//pkg/util/log/logconfig",
953955
"//pkg/util/log/logpb",

pkg/sql/event_log.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import (
1616
"github.com/cockroachdb/cockroach/pkg/jobs"
1717
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
1818
"github.com/cockroachdb/cockroach/pkg/security/username"
19-
"github.com/cockroachdb/cockroach/pkg/settings"
2019
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
2120
"github.com/cockroachdb/cockroach/pkg/sql/isql"
2221
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scbuild"
2322
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scrun"
2423
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
2524
"github.com/cockroachdb/cockroach/pkg/util/log"
25+
"github.com/cockroachdb/cockroach/pkg/util/log/eventlog"
2626
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
2727
"github.com/cockroachdb/cockroach/pkg/util/log/logpb"
2828
"github.com/cockroachdb/cockroach/pkg/util/log/severity"
@@ -401,23 +401,6 @@ func LogEventForJobs(
401401
)
402402
}
403403

404-
var eventLogSystemTableEnabled = settings.RegisterBoolSetting(
405-
settings.ApplicationLevel,
406-
"server.eventlog.enabled",
407-
"if set, logged notable events are also stored in the table system.eventlog",
408-
true,
409-
settings.WithPublic)
410-
411-
// EventLogTestingKnobs provides hooks and knobs for event logging.
412-
type EventLogTestingKnobs struct {
413-
// SyncWrites causes events to be written on the same txn as
414-
// the SQL statement that causes them.
415-
SyncWrites bool
416-
}
417-
418-
// ModuleTestingKnobs implements base.ModuleTestingKnobs interface.
419-
func (*EventLogTestingKnobs) ModuleTestingKnobs() {}
420-
421404
// LogEventDestination indicates for InsertEventRecords where the
422405
// event should be directed to.
423406
type LogEventDestination int
@@ -529,7 +512,7 @@ func insertEventRecords(
529512
}
530513

531514
// If we only want to log externally and not write to the events table, early exit.
532-
loggingToSystemTable := opts.dst.hasFlag(LogToSystemTable) && eventLogSystemTableEnabled.Get(&execCfg.Settings.SV)
515+
loggingToSystemTable := opts.dst.hasFlag(LogToSystemTable) && eventlog.SystemTableEnabled.Get(&execCfg.Settings.SV)
533516
if !loggingToSystemTable {
534517
// Simply emit the events to their respective channels and call it a day.
535518
if opts.dst.hasFlag(LogExternally) {

0 commit comments

Comments
 (0)