Skip to content

Commit

Permalink
remove SetLogger, add operational logs (#103)
Browse files Browse the repository at this point in the history
The NewConfig function provides a hook for replacing the logger, which is
the best place to do this, since it will capture the writer init message.

Add a few extra log lines to capture key operational details related to
startup and shutdown.
  • Loading branch information
copperlight authored Jul 5, 2024
1 parent 74e1fda commit c127864
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func getNextRequest() *Request {

func main() {
commonTags := map[string]string{"nf.platform": "my_platform", "process_name": "my_process"}
// if desired, replace the logger with a custom one, using the third parameter here:
config, _ := spectator.NewConfig("", commonTags, nil)

registry, _ := spectator.NewRegistry(config)
Expand Down
16 changes: 6 additions & 10 deletions spectator/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Meter interface {
// Registry is the main entry point for interacting with the Spectator library.
type Registry interface {
GetLogger() logger.Logger
SetLogger(logger logger.Logger)
NewId(name string, tags map[string]string) *meter.Id
AgeGauge(name string, tags map[string]string) *meter.AgeGauge
AgeGaugeWithId(id *meter.Id) *meter.AgeGauge
Expand Down Expand Up @@ -62,11 +61,11 @@ type spectatordRegistry struct {
// NewRegistry generates a new registry from a passed Config created through NewConfig.
func NewRegistry(config *Config) (Registry, error) {
if config == nil {
return nil, fmt.Errorf("config cannot be nil")
return nil, fmt.Errorf("Config cannot be nil")
}

if config.location == "" {
// Config was not created using NewConfig. Set a default config instead of using the passed one
// Config was not created using NewConfig. Set a default config instead of using the passed one.
config, _ = NewConfig("", nil, nil)
}

Expand All @@ -75,6 +74,8 @@ func NewRegistry(config *Config) (Registry, error) {
return nil, err
}

config.log.Infof("Create Registry with extra commonTags=%v", config.commonTags)

r := &spectatordRegistry{
config: config,
writer: newWriter,
Expand All @@ -89,11 +90,6 @@ func (r *spectatordRegistry) GetLogger() logger.Logger {
return r.logger
}

// SetLogger overrides the internal logger.
func (r *spectatordRegistry) SetLogger(logger logger.Logger) {
r.logger = logger
}

// NewId calls meters.NewId() and adds the commonTags registered in the config.
func (r *spectatordRegistry) NewId(name string, tags map[string]string) *meter.Id {
newId := meter.NewId(name, tags)
Expand Down Expand Up @@ -198,9 +194,9 @@ func (r *spectatordRegistry) GetWriter() writer.Writer {
}

func (r *spectatordRegistry) Close() {
r.GetLogger().Infof("Close Registry Writer")
err := r.writer.Close()

if err != nil {
r.GetLogger().Errorf("Error closing writer: %v", err)
r.GetLogger().Errorf("Error closing Registry Writer: %v", err)
}
}

0 comments on commit c127864

Please sign in to comment.