add schema, id and time to client connect events

This bring these to same level as the JS events, these are the ones
I care for right now but will do this to the rest here in time as well
and document them in JSON schema

Signed-off-by: R.I.Pienaar <rip@devco.net>
This commit is contained in:
R.I.Pienaar
2020-04-23 18:38:33 +02:00
committed by Derek Collison
parent c64e39ea61
commit 0703f266cc
2 changed files with 35 additions and 1 deletions

View File

@@ -27,6 +27,8 @@ import (
"sync/atomic"
"time"
"github.com/nats-io/nuid"
"github.com/nats-io/nats-server/v2/server/pse"
)
@@ -90,6 +92,9 @@ type ServerStatsMsg struct {
// ConnectEventMsg is sent when a new connection is made that is part of an account.
type ConnectEventMsg struct {
Schema string `json:"schema"`
ID string `json:"id"`
Time string `json:"timestamp"`
Server ServerInfo `json:"server"`
Client ClientInfo `json:"client"`
}
@@ -97,6 +102,9 @@ type ConnectEventMsg struct {
// DisconnectEventMsg is sent when a new connection previously defined from a
// ConnectEventMsg is closed.
type DisconnectEventMsg struct {
Schema string `json:"schema"`
ID string `json:"id"`
Time string `json:"timestamp"`
Server ServerInfo `json:"server"`
Client ClientInfo `json:"client"`
Sent DataStats `json:"sent"`
@@ -995,6 +1003,9 @@ func (s *Server) accountConnectEvent(c *client) {
}
m := ConnectEventMsg{
Schema: "io.nats.server.advisory.v1.client_connect",
ID: nuid.Next(),
Time: time.Now().UTC().Format(time.RFC3339Nano),
Client: ClientInfo{
Start: c.start,
Host: c.host,
@@ -1032,6 +1043,9 @@ func (s *Server) accountDisconnectEvent(c *client, now time.Time, reason string)
}
m := DisconnectEventMsg{
Schema: "io.nats.server.advisory.v1.client_disconnect",
ID: nuid.Next(),
Time: time.Now().UTC().Format(time.RFC3339Nano),
Client: ClientInfo{
Start: c.start,
Stop: &now,
@@ -1070,6 +1084,9 @@ func (s *Server) sendAuthErrorEvent(c *client) {
now := time.Now()
c.mu.Lock()
m := DisconnectEventMsg{
Schema: "io.nats.server.advisory.v1.client_disconnect",
ID: nuid.Next(),
Time: time.Now().UTC().Format(time.RFC3339Nano),
Client: ClientInfo{
Start: c.start,
Stop: &now,

View File

@@ -230,6 +230,15 @@ func TestSystemAccountNewConnection(t *testing.T) {
if err := json.Unmarshal(msg.Data, &cem); err != nil {
t.Fatalf("Error unmarshalling connect event message: %v", err)
}
if cem.Schema != "io.nats.server.advisory.v1.client_connect" {
t.Fatalf("Incorrect schema in connect event: %s", cem.Schema)
}
if cem.Time == "" {
t.Fatalf("Event time is not set")
}
if len(cem.ID) != 22 {
t.Fatalf("Event ID is incorrectly set to len %d", len(cem.ID))
}
if cem.Server.ID != s.ID() {
t.Fatalf("Expected server to be %q, got %q", s.ID(), cem.Server.ID)
}
@@ -277,7 +286,15 @@ func TestSystemAccountNewConnection(t *testing.T) {
if err := json.Unmarshal(msg.Data, &dem); err != nil {
t.Fatalf("Error unmarshalling disconnect event message: %v", err)
}
if dem.Schema != "io.nats.server.advisory.v1.client_disconnect" {
t.Fatalf("Incorrect schema in connect event: %s", cem.Schema)
}
if dem.Time == "" {
t.Fatalf("Event time is not set")
}
if len(dem.ID) != 22 {
t.Fatalf("Event ID is incorrectly set to len %d", len(cem.ID))
}
if dem.Server.ID != s.ID() {
t.Fatalf("Expected server to be %q, got %q", s.ID(), dem.Server.ID)
}