Skip to content

Commit

Permalink
Allows a descriptive server_name to be set
Browse files Browse the repository at this point in the history
This adds a new config option server_name that
when set will be exposed in varz, events and more
as a descriptive name for the server.

If unset though the server_name will default to the pk

Signed-off-by: R.I.Pienaar <rip@devco.net>
  • Loading branch information
ripienaar committed Oct 17, 2019
1 parent 116ae2a commit bcf96fa
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 7 deletions.
2 changes: 2 additions & 0 deletions server/configs/test.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Simple config file

server_name: testing_server

listen: 127.0.0.1:4242

http: 8222
Expand Down
3 changes: 3 additions & 0 deletions server/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type accNumConnsReq struct {

// ServerInfo identifies remote servers.
type ServerInfo struct {
Name string `json:"name"`
Host string `json:"host"`
ID string `json:"id"`
Cluster string `json:"cluster,omitempty"`
Expand Down Expand Up @@ -215,6 +216,7 @@ func (s *Server) internalSendLoop(wg *sync.WaitGroup) {
sendq := s.sys.sendq
id := s.info.ID
host := s.info.Host
servername := s.info.Name
seqp := &s.sys.seq
var cluster string
if s.gateway.enabled {
Expand All @@ -237,6 +239,7 @@ func (s *Server) internalSendLoop(wg *sync.WaitGroup) {
select {
case pm := <-sendq:
if pm.si != nil {
pm.si.Name = servername
pm.si.Host = host
pm.si.Cluster = cluster
pm.si.ID = id
Expand Down
2 changes: 2 additions & 0 deletions server/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ func (s *Server) HandleStacksz(w http.ResponseWriter, r *http.Request) {
// Varz will output server information on the monitoring port at /varz.
type Varz struct {
ID string `json:"server_id"`
Name string `json:"server_name"`
Version string `json:"version"`
Proto int `json:"proto"`
GitCommit string `json:"git_commit,omitempty"`
Expand Down Expand Up @@ -1077,6 +1078,7 @@ func (s *Server) createVarz(pcpu float64, rss int64) *Varz {
Proto: info.Proto,
GitCommit: info.GitCommit,
GoVersion: info.GoVersion,
Name: info.Name,
Host: info.Host,
Port: info.Port,
IP: info.IP,
Expand Down
16 changes: 10 additions & 6 deletions server/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ const CLUSTER_PORT = -1

func DefaultMonitorOptions() *Options {
return &Options{
Host: "127.0.0.1",
Port: CLIENT_PORT,
HTTPHost: "127.0.0.1",
HTTPPort: MONITOR_PORT,
NoLog: true,
NoSigs: true,
Host: "127.0.0.1",
Port: CLIENT_PORT,
HTTPHost: "127.0.0.1",
HTTPPort: MONITOR_PORT,
ServerName: "monitor_server",
NoLog: true,
NoSigs: true,
}
}

Expand Down Expand Up @@ -210,6 +211,9 @@ func TestHandleVarz(t *testing.T) {
if v.Subscriptions != 0 {
t.Fatalf("Expected Subscriptions of 0, got %v\n", v.Subscriptions)
}
if v.Name != "monitor_server" {
t.Fatal("Expected ServerName to be 'monitor_server'")
}
}

// Test JSONP
Expand Down
3 changes: 3 additions & 0 deletions server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type RemoteLeafOpts struct {
// and json tags are deprecated and may be removed in the future.
type Options struct {
ConfigFile string `json:"-"`
ServerName string `json:"server_name"`
Host string `json:"addr"`
Port int `json:"port"`
ClientAdvertise string `json:"-"`
Expand Down Expand Up @@ -446,6 +447,8 @@ func (o *Options) ProcessConfigFile(configFile string) error {
o.ClientAdvertise = v.(string)
case "port":
o.Port = int(v.(int64))
case "server_name":
o.ServerName = v.(string)
case "host", "net":
o.Host = v.(string)
case "debug":
Expand Down
2 changes: 2 additions & 0 deletions server/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestOptions_RandomPort(t *testing.T) {
func TestConfigFile(t *testing.T) {
golden := &Options{
ConfigFile: "./configs/test.conf",
ServerName: "testing_server",
Host: "127.0.0.1",
Port: 4242,
Username: "derek",
Expand Down Expand Up @@ -239,6 +240,7 @@ func TestTLSConfigFile(t *testing.T) {
func TestMergeOverrides(t *testing.T) {
golden := &Options{
ConfigFile: "./configs/test.conf",
ServerName: "testing_server",
Host: "127.0.0.1",
Port: 2222,
Username: "derek",
Expand Down
7 changes: 7 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var lameDuckModeInitialDelay = int64(lameDuckModeDefaultInitialDelay)
// to help them understand information about this server.
type Info struct {
ID string `json:"server_id"`
Name string `json:"server_name"`
Version string `json:"version"`
Proto int `json:"proto"`
GitCommit string `json:"git_commit,omitempty"`
Expand Down Expand Up @@ -212,6 +213,11 @@ func NewServer(opts *Options) (*Server, error) {
kp, _ := nkeys.CreateServer()
pub, _ := kp.PublicKey()

serverName := pub
if opts.ServerName != "" {
serverName = opts.ServerName
}

// Validate some options. This is here because we cannot assume that
// server will always be started with configuration parsing (that could
// report issues). Its options can be (incorrectly) set by hand when
Expand All @@ -226,6 +232,7 @@ func NewServer(opts *Options) (*Server, error) {
Proto: PROTO,
GitCommit: gitCommit,
GoVersion: runtime.Version(),
Name: serverName,
Host: opts.Host,
Port: opts.Port,
AuthRequired: false,
Expand Down
25 changes: 25 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,31 @@ func TestGetConnectURLs(t *testing.T) {
}
}

func TestInfoServerNameDefaultsToPK(t *testing.T) {
opts := DefaultOptions()
opts.Port = 4222
opts.ClientAdvertise = "nats.example.com"
s := New(opts)
defer s.Shutdown()

if s.info.Name != s.info.ID {
t.Fatalf("server info hostname is incorrect, got: '%v' expected: '%v'", s.info.Name, s.info.ID)
}
}

func TestInfoServerNameIsSettable(t *testing.T) {
opts := DefaultOptions()
opts.Port = 4222
opts.ClientAdvertise = "nats.example.com"
opts.ServerName = "test_server_name"
s := New(opts)
defer s.Shutdown()

if s.info.Name != "test_server_name" {
t.Fatalf("server info hostname is incorrect, got: '%v' expected: 'test_server_name'", s.info.Name)
}
}

func TestClientAdvertiseConnectURL(t *testing.T) {
opts := DefaultOptions()
opts.Port = 4222
Expand Down
8 changes: 7 additions & 1 deletion server/sublist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"flag"
"fmt"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -1047,9 +1048,14 @@ func TestSublistAll(t *testing.T) {
var benchSublistSubs []*subscription
var benchSublistSl = NewSublistWithCache()

// https://github.com/golang/go/issues/31859
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}

func init() {
initSublist := false
flag.Parse()
flag.Visit(func(f *flag.Flag) {
if f.Name == "test.bench" {
initSublist = true
Expand Down

0 comments on commit bcf96fa

Please sign in to comment.