github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/event_log.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package sql 12 13 import ( 14 "context" 15 "encoding/json" 16 17 "github.com/cockroachdb/cockroach/pkg/kv" 18 "github.com/cockroachdb/cockroach/pkg/util/log" 19 "github.com/cockroachdb/errors" 20 ) 21 22 // EventLogType represents an event type that can be recorded in the event log. 23 type EventLogType string 24 25 // NOTE: When you add a new event type here. Please manually add it to 26 // pkg/ui/src/util/eventTypes.ts so that it will be recognized in the UI. 27 const ( 28 // EventLogCreateDatabase is recorded when a database is created. 29 EventLogCreateDatabase EventLogType = "create_database" 30 // EventLogDropDatabase is recorded when a database is dropped. 31 EventLogDropDatabase EventLogType = "drop_database" 32 33 // EventLogCreateTable is recorded when a table is created. 34 EventLogCreateTable EventLogType = "create_table" 35 // EventLogDropTable is recorded when a table is dropped. 36 EventLogDropTable EventLogType = "drop_table" 37 // EventLogTruncateTable is recorded when a table is truncated. 38 EventLogTruncateTable EventLogType = "truncate_table" 39 // EventLogAlterTable is recorded when a table is altered. 40 EventLogAlterTable EventLogType = "alter_table" 41 // EventLogCommentOnColumn is recorded when a column is commented. 42 EventLogCommentOnColumn EventLogType = "comment_on_column" 43 // EventLogCommentOnTable is recorded when a table is commented. 44 EventLogCommentOnDatabase EventLogType = "comment_on_database" 45 // EventLogCommentOnTable is recorded when a table is commented. 46 EventLogCommentOnTable EventLogType = "comment_on_table" 47 // EventLogCommentOnIndex is recorded when a index is commented. 48 EventLogCommentOnIndex EventLogType = "comment_on_index" 49 50 // EventLogCreateIndex is recorded when an index is created. 51 EventLogCreateIndex EventLogType = "create_index" 52 // EventLogDropIndex is recorded when an index is dropped. 53 EventLogDropIndex EventLogType = "drop_index" 54 // EventLogAlterIndex is recorded when an index is altered. 55 EventLogAlterIndex EventLogType = "alter_index" 56 57 // EventLogCreateView is recorded when a view is created. 58 EventLogCreateView EventLogType = "create_view" 59 // EventLogDropView is recorded when a view is dropped. 60 EventLogDropView EventLogType = "drop_view" 61 62 // EventLogCreateSequence is recorded when a sequence is created. 63 EventLogCreateSequence EventLogType = "create_sequence" 64 // EventLogDropSequence is recorded when a sequence is dropped. 65 EventLogDropSequence EventLogType = "drop_sequence" 66 // EventLogAlterSequence is recorded when a sequence is altered. 67 EventLogAlterSequence EventLogType = "alter_sequence" 68 69 // EventLogReverseSchemaChange is recorded when an in-progress schema change 70 // encounters a problem and is reversed. 71 EventLogReverseSchemaChange EventLogType = "reverse_schema_change" 72 // EventLogFinishSchemaChange is recorded when a previously initiated schema 73 // change has completed. 74 EventLogFinishSchemaChange EventLogType = "finish_schema_change" 75 // EventLogFinishSchemaRollback is recorded when a previously 76 // initiated schema change rollback has completed. 77 EventLogFinishSchemaRollback EventLogType = "finish_schema_change_rollback" 78 79 // EventLogNodeJoin is recorded when a node joins the cluster. 80 EventLogNodeJoin EventLogType = "node_join" 81 // EventLogNodeRestart is recorded when an existing node rejoins the cluster 82 // after being offline. 83 EventLogNodeRestart EventLogType = "node_restart" 84 // EventLogNodeDecommissioned is recorded when a node is marked as 85 // decommissioning. 86 EventLogNodeDecommissioned EventLogType = "node_decommissioned" 87 // EventLogNodeRecommissioned is recorded when a decommissioned node is 88 // recommissioned. 89 EventLogNodeRecommissioned EventLogType = "node_recommissioned" 90 91 // EventLogSetClusterSetting is recorded when a cluster setting is changed. 92 EventLogSetClusterSetting EventLogType = "set_cluster_setting" 93 94 // EventLogSetZoneConfig is recorded when a zone config is changed. 95 EventLogSetZoneConfig EventLogType = "set_zone_config" 96 // EventLogRemoveZoneConfig is recorded when a zone config is removed. 97 EventLogRemoveZoneConfig EventLogType = "remove_zone_config" 98 99 // EventLogCreateStatistics is recorded when statistics are collected for a 100 // table. 101 EventLogCreateStatistics EventLogType = "create_statistics" 102 ) 103 104 // EventLogSetClusterSettingDetail is the json details for a settings change. 105 type EventLogSetClusterSettingDetail struct { 106 SettingName string 107 Value string 108 User string 109 } 110 111 // An EventLogger exposes methods used to record events to the event table. 112 type EventLogger struct { 113 *InternalExecutor 114 } 115 116 // MakeEventLogger constructs a new EventLogger. 117 func MakeEventLogger(execCfg *ExecutorConfig) EventLogger { 118 return EventLogger{InternalExecutor: execCfg.InternalExecutor} 119 } 120 121 // InsertEventRecord inserts a single event into the event log as part of the 122 // provided transaction. 123 func (ev EventLogger) InsertEventRecord( 124 ctx context.Context, 125 txn *kv.Txn, 126 eventType EventLogType, 127 targetID, reportingID int32, 128 info interface{}, 129 ) error { 130 // Record event record insertion in local log output. 131 txn.AddCommitTrigger(func(ctx context.Context) { 132 log.Infof( 133 ctx, "Event: %q, target: %d, info: %+v", 134 eventType, 135 targetID, 136 info, 137 ) 138 }) 139 140 const insertEventTableStmt = ` 141 INSERT INTO system.eventlog ( 142 timestamp, "eventType", "targetID", "reportingID", info 143 ) 144 VALUES( 145 now(), $1, $2, $3, $4 146 ) 147 ` 148 args := []interface{}{ 149 eventType, 150 targetID, 151 reportingID, 152 nil, // info 153 } 154 if info != nil { 155 infoBytes, err := json.Marshal(info) 156 if err != nil { 157 return err 158 } 159 args[3] = string(infoBytes) 160 } 161 rows, err := ev.Exec(ctx, "log-event", txn, insertEventTableStmt, args...) 162 if err != nil { 163 return err 164 } 165 if rows != 1 { 166 return errors.Errorf("%d rows affected by log insertion; expected exactly one row affected.", rows) 167 } 168 return nil 169 }