Skip to content

Commit

Permalink
add Clock to Registry (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
copperlight authored May 8, 2024
1 parent 8c190e2 commit daad61e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
19 changes: 17 additions & 2 deletions spectator/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/Netflix/spectator-go/spectator/logger"
"github.com/Netflix/spectator-go/spectator/meter"
"github.com/Netflix/spectator-go/spectator/writer"
"io/ioutil"
"os"
"path/filepath"
"time"
)
Expand All @@ -24,6 +24,7 @@ type Meter interface {
type RegistryInterface interface {
GetLogger() logger.Logger
SetLogger(logger logger.Logger)
Clock() Clock
NewId(name string, tags map[string]string) *meter.Id
Counter(name string, tags map[string]string) *meter.Counter
CounterWithId(id *meter.Id) *meter.Counter
Expand Down Expand Up @@ -53,6 +54,7 @@ var _ RegistryInterface = (*Registry)(nil)

// Registry is the collection of meters being reported.
type Registry struct {
clock Clock
config *Config
writer writer.Writer
}
Expand All @@ -61,7 +63,7 @@ type Registry struct {
func NewRegistryConfiguredBy(filePath string) (*Registry, error) {
path := filepath.Clean(filePath)
/* #nosec G304 */
bytes, err := ioutil.ReadFile(path)
bytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -103,13 +105,22 @@ func NewRegistry(config *Config) (*Registry, error) {
config.Log.Infof("Initializing Registry using writer: %T", newWriter)

r := &Registry{
clock: &SystemClock{},
config: config,
writer: newWriter,
}

return r, nil
}

// NewRegistryWithClock returns a new registry with the clock overridden to the
// one injected here. This function is mostly used for testing.
func NewRegistryWithClock(config *Config, clock Clock) *Registry {
r, _ := NewRegistry(config)
r.clock = clock
return r
}

// GetLogger returns the internal logger.
func (r *Registry) GetLogger() logger.Logger {
return r.config.Log
Expand All @@ -119,6 +130,10 @@ func (r *Registry) SetLogger(logger logger.Logger) {
r.config.Log = logger
}

func (r *Registry) Clock() Clock {
return r.clock
}

// NewId calls meters.NewId() and adds the CommonTags registered in the config.
func (r *Registry) NewId(name string, tags map[string]string) *meter.Id {
newId := meter.NewId(name, tags)
Expand Down
20 changes: 20 additions & 0 deletions spectator/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import (
"time"
)

func TestRegistry_Clock(t *testing.T) {
mw := &writer.MemoryWriter{}
clock := &ManualClock{1}
r := NewTestRegistryWithClock(mw, clock)
now := r.Clock().Now()
expected := time.Unix(0, 1)
if now != expected {
t.Errorf("Expected '%+v', got '%+v'", expected, now)
}
}

func TestRegistryWithMemoryWriter_Counter(t *testing.T) {
mw := &writer.MemoryWriter{}
r := NewTestRegistry(mw)
Expand Down Expand Up @@ -131,7 +142,16 @@ func TestRegistryWithMemoryWriter_PercentileTimer(t *testing.T) {

func NewTestRegistry(mw *writer.MemoryWriter) *Registry {
return &Registry{
clock: &SystemClock{},
config: &Config{},
writer: mw,
}
}

func NewTestRegistryWithClock(mw *writer.MemoryWriter, clock Clock) *Registry {
return &Registry{
clock: clock,
config: &Config{},
writer: mw,
}
}

0 comments on commit daad61e

Please sign in to comment.