Skip to content

Commit

Permalink
eventpb: make the Timestamp field an int64
Browse files Browse the repository at this point in the history
While exploring how to use the data in practice, I noticed that the
timestamp was encoded as a string in the JSON output, and that was
making the events hard to parse and to use. There are many
more functions that can operate on a number.

Moreover, the integer representation is just more compact.

Release note (api change):  The 'Timestamp' field of structured
notable events is now numeric, and encodes a number of nanoseconds
since the unix epoch. (Note that this API has not yet been published
in a released version of CockroachDB. The release note exists only to
track the list of relevant items for the doc project.)
  • Loading branch information
knz committed Dec 18, 2020
1 parent 77e0156 commit 3605e9c
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 127 deletions.
110 changes: 55 additions & 55 deletions docs/generated/eventlog.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/server/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func (n *Node) recordJoinEvent(ctx context.Context) {
nodeDetails = &ev.CommonNodeEventDetails
nodeDetails.LastUp = n.startedAt
}
event.CommonDetails().Timestamp = timeutil.Now()
event.CommonDetails().Timestamp = timeutil.Now().UnixNano()
nodeDetails.StartedAt = n.startedAt
nodeDetails.NodeID = int32(n.Descriptor.NodeID)

Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ func (s *Server) Decommission(
} else {
panic("unexpected target membership status")
}
event.CommonDetails().Timestamp = timeutil.Now()
event.CommonDetails().Timestamp = timeutil.Now().UnixNano()
nodeDetails.RequestingNodeID = int32(s.NodeID())

for _, nodeID := range nodeIDs {
Expand Down
11 changes: 5 additions & 6 deletions pkg/sql/event_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package sql
import (
"context"
"encoding/json"
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/kv"
Expand All @@ -23,6 +22,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)

Expand All @@ -49,7 +49,7 @@ func logEventInternalForSchemaChanges(
mutationID descpb.MutationID,
event eventpb.EventPayload,
) error {
event.CommonDetails().Timestamp = txn.ReadTimestamp().GoTime()
event.CommonDetails().Timestamp = txn.ReadTimestamp().WallTime
scCommon, ok := event.(eventpb.EventWithCommonSchemaChangePayload)
if !ok {
return errors.AssertionFailedf("unknown event type: %T", event)
Expand Down Expand Up @@ -84,7 +84,7 @@ func logEventInternalForSQLStatements(
event eventpb.EventPayload,
) error {
// Inject the common fields into the payload provided by the caller.
event.CommonDetails().Timestamp = txn.ReadTimestamp().GoTime()
event.CommonDetails().Timestamp = txn.ReadTimestamp().WallTime
sqlCommon, ok := event.(eventpb.EventWithCommonSQLPayload)
if !ok {
return errors.AssertionFailedf("unknown event type: %T", event)
Expand Down Expand Up @@ -135,8 +135,7 @@ func InsertEventRecord(
info.CommonDetails().EventType = eventType

// The caller is responsible for the timestamp field.
var zeroTime time.Time
if info.CommonDetails().Timestamp == zeroTime {
if info.CommonDetails().Timestamp == 0 {
return errors.AssertionFailedf("programming error: timestamp field in event not populated: %T", info)
}

Expand All @@ -160,7 +159,7 @@ VALUES(
)
`
args := []interface{}{
info.CommonDetails().Timestamp,
timeutil.Unix(0, info.CommonDetails().Timestamp),
eventType,
targetID,
reportingID,
Expand Down
6 changes: 2 additions & 4 deletions pkg/util/log/event_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package log
import (
"context"
"encoding/json"
"time"

"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/cockroach/pkg/util/log/severity"
Expand All @@ -24,9 +23,8 @@ import (
func StructuredEvent(ctx context.Context, event eventpb.EventPayload) {
// Populate the missing common fields.
common := event.CommonDetails()
var zeroTime time.Time
if common.Timestamp == zeroTime {
common.Timestamp = timeutil.Now()
if common.Timestamp == 0 {
common.Timestamp = timeutil.Now().UnixNano()
}
if len(common.EventType) == 0 {
common.EventType = eventpb.GetEventTypeName(event)
Expand Down
1 change: 0 additions & 1 deletion pkg/util/log/eventpb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ go_library(
deps = [
"//pkg/util/log/logpb",
"//vendor/github.com/gogo/protobuf/proto",
"//vendor/github.com/gogo/protobuf/types",
],
)
95 changes: 38 additions & 57 deletions pkg/util/log/eventpb/events.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pkg/util/log/eventpb/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import "google/protobuf/timestamp.proto";

// CommonEventDetails contains the fields common to all events.
message CommonEventDetails {
// The timestamp of the event.
google.protobuf.Timestamp timestamp = 1 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true, (gogoproto.jsontag) = ",omitempty"];
// The timestamp of the event. Expressed as nanoseconds since
// the Unix epoch.
int64 timestamp = 1 [(gogoproto.jsontag) = ",omitempty"];
// The type of the event.
string event_type = 2 [(gogoproto.jsontag) = ",omitempty"];
}
Expand Down

0 comments on commit 3605e9c

Please sign in to comment.