Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add logging to NATS streaming server #21379

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
1. [21349](https://github.com/influxdata/influxdb/pull/21349): Fix off-by-one error in query range calculation over partially compacted data.
1. [21350](https://github.com/influxdata/influxdb/pull/21350): Deprecate the unsupported `PostSetupUser` API.
1. [21376](https://github.com/influxdata/influxdb/pull/21376): Add limits to the `/api/v2/delete` endpoint for start and stop times with error messages.
1. [21379](https://github.com/influxdata/influxdb/pull/21379): Add logging to NATS streaming server to help debug startup failures.

## v2.0.6 [2021-04-29]
----------------------
Expand Down
2 changes: 1 addition & 1 deletion cmd/influxd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func (m *Launcher) run(ctx context.Context, opts *InfluxdOpts) (err error) {
natsOpts := nats.NewDefaultServerOptions()
natsOpts.Port = opts.NatsPort
natsOpts.MaxPayload = opts.NatsMaxPayloadBytes
m.natsServer = nats.NewServer(&natsOpts)
m.natsServer = nats.NewServer(&natsOpts, m.log.With(zap.String("service", "nats")))
if err := m.natsServer.Open(); err != nil {
m.log.Error("Failed to start nats streaming server", zap.Error(err))
return err
Expand Down
35 changes: 35 additions & 0 deletions nats/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nats

import (
"fmt"

natsserver "github.com/nats-io/gnatsd/server"
"go.uber.org/zap"
)

var _ natsserver.Logger = (*zapLoggerAdapter)(nil)

// zapLogger
type zapLoggerAdapter struct {
log *zap.Logger
}

func (z *zapLoggerAdapter) Noticef(format string, v ...interface{}) {
z.log.Debug(fmt.Sprintf(format, v...), zap.String("nats_level", "notice"))
}

func (z *zapLoggerAdapter) Debugf(format string, v ...interface{}) {
z.log.Debug(fmt.Sprintf(format, v...), zap.String("nats_level", "debug"))
}

func (z *zapLoggerAdapter) Tracef(format string, v ...interface{}) {
z.log.Debug(fmt.Sprintf(format, v...), zap.String("nats_level", "trace"))
}

func (z *zapLoggerAdapter) Fatalf(format string, v ...interface{}) {
z.log.Fatal(fmt.Sprintf(format, v...), zap.String("nats_level", "fatal"))
}

func (z *zapLoggerAdapter) Errorf(format string, v ...interface{}) {
z.log.Error(fmt.Sprintf(format, v...), zap.String("nats_level", "error"))
}
7 changes: 5 additions & 2 deletions nats/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/nats-io/gnatsd/server"
sserver "github.com/nats-io/nats-streaming-server/server"
"github.com/nats-io/nats-streaming-server/stores"
"go.uber.org/zap"
)

const ServerName = "platform"
Expand All @@ -23,6 +24,7 @@ var ErrNoNatsConnection = errors.New("nats connection has not been established.
type Server struct {
serverOpts *server.Options
Server *sserver.StanServer
logger server.Logger
}

// Open starts a NATS streaming server
Expand All @@ -31,6 +33,7 @@ func (s *Server) Open() error {
opts := sserver.GetDefaultOptions()
opts.StoreType = stores.TypeMemory
opts.ID = ServerName
opts.CustomLogger = s.logger

server, err := sserver.RunServerWithOpts(opts, s.serverOpts)
if err != nil {
Expand All @@ -54,10 +57,10 @@ func NewDefaultServerOptions() server.Options {
}

// NewServer creates a new streaming server with the provided server options.
func NewServer(opts *server.Options) *Server {
func NewServer(opts *server.Options, log *zap.Logger) *Server {
if opts == nil {
o := NewDefaultServerOptions()
opts = &o
}
return &Server{serverOpts: opts}
return &Server{serverOpts: opts, logger: &zapLoggerAdapter{log}}
}