Skip to content

Commit

Permalink
Be more like a library to support mobile (slackhq#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrownus authored Jun 30, 2020
1 parent 1ea8847 commit 41578ca
Show file tree
Hide file tree
Showing 21 changed files with 477 additions and 69 deletions.
4 changes: 2 additions & 2 deletions bits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ func TestBitsLostCounter(t *testing.T) {
func BenchmarkBits(b *testing.B) {
z := NewBits(10)
for n := 0; n < b.N; n++ {
for i, _ := range z.bits {
for i := range z.bits {
z.bits[i] = true
}
for i, _ := range z.bits {
for i := range z.bits {
z.bits[i] = false
}

Expand Down
26 changes: 23 additions & 3 deletions cmd/nebula-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"flag"
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/slackhq/nebula"
"os"
)

// A version string that can be set with
Expand Down Expand Up @@ -45,5 +45,25 @@ func main() {
os.Exit(1)
}

nebula.Main(*configPath, *configTest, Build)
config := nebula.NewConfig()
err := config.Load(*configPath)
if err != nil {
fmt.Printf("failed to load config: %s", err)
os.Exit(1)
}

l := logrus.New()
l.Out = os.Stdout
err = nebula.Main(config, *configTest, true, Build, l, nil, nil)

switch v := err.(type) {
case nebula.ContextualError:
v.Log(l)
os.Exit(1)
case error:
l.WithError(err).Error("Failed to start")
os.Exit(1)
}

os.Exit(0)
}
13 changes: 11 additions & 2 deletions cmd/nebula-service/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"github.com/sirupsen/logrus"
"log"
"os"
"path/filepath"
Expand All @@ -27,8 +29,15 @@ func (p *program) Start(s service.Service) error {
}

func (p *program) run() error {
nebula.Main(*p.configPath, *p.configTest, Build)
return nil
config := nebula.NewConfig()
err := config.Load(*p.configPath)
if err != nil {
return fmt.Errorf("failed to load config: %s", err)
}

l := logrus.New()
l.Out = os.Stdout
return nebula.Main(config, *p.configTest, true, Build, l, nil, nil)
}

func (p *program) Stop(s service.Service) error {
Expand Down
23 changes: 22 additions & 1 deletion cmd/nebula/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"github.com/sirupsen/logrus"
"os"

"github.com/slackhq/nebula"
Expand Down Expand Up @@ -39,5 +40,25 @@ func main() {
os.Exit(1)
}

nebula.Main(*configPath, *configTest, Build)
config := nebula.NewConfig()
err := config.Load(*configPath)
if err != nil {
fmt.Printf("failed to load config: %s", err)
os.Exit(1)
}

l := logrus.New()
l.Out = os.Stdout
err = nebula.Main(config, *configTest, true, Build, l, nil, nil)

switch v := err.(type) {
case nebula.ContextualError:
v.Log(l)
os.Exit(1)
case error:
l.WithError(err).Error("Failed to start")
os.Exit(1)
}

os.Exit(0)
}
20 changes: 20 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nebula

import (
"errors"
"fmt"
"github.com/imdario/mergo"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -56,6 +57,13 @@ func (c *Config) Load(path string) error {
return nil
}

func (c *Config) LoadString(raw string) error {
if raw == "" {
return errors.New("Empty configuration")
}
return c.parseRaw([]byte(raw))
}

// RegisterReloadCallback stores a function to be called when a config reload is triggered. The functions registered
// here should decide if they need to make a change to the current process before making the change. HasChanged can be
// used to help decide if a change is necessary.
Expand Down Expand Up @@ -407,6 +415,18 @@ func (c *Config) addFile(path string, direct bool) error {
return nil
}

func (c *Config) parseRaw(b []byte) error {
var m map[interface{}]interface{}

err := yaml.Unmarshal(b, &m)
if err != nil {
return err
}

c.Settings = m
return nil
}

func (c *Config) parse() error {
var m map[interface{}]interface{}

Expand Down
31 changes: 31 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nebula

import (
"github.com/sirupsen/logrus"
)

type ContextualError struct {
RealError error
Fields map[string]interface{}
Context string
}

func NewContextualError(msg string, fields map[string]interface{}, realError error) ContextualError {
return ContextualError{Context: msg, Fields: fields, RealError: realError}
}

func (ce ContextualError) Error() string {
return ce.RealError.Error()
}

func (ce ContextualError) Unwrap() error {
return ce.RealError
}

func (ce *ContextualError) Log(lr *logrus.Logger) {
if ce.RealError != nil {
lr.WithFields(ce.Fields).WithError(ce.RealError).Error(ce.Context)
} else {
lr.WithFields(ce.Fields).Error(ce.Context)
}
}
66 changes: 66 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package nebula

import (
"errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)

type TestLogWriter struct {
Logs []string
}

func NewTestLogWriter() *TestLogWriter {
return &TestLogWriter{Logs: make([]string, 0)}
}

func (tl *TestLogWriter) Write(p []byte) (n int, err error) {
tl.Logs = append(tl.Logs, string(p))
return len(p), nil
}

func (tl *TestLogWriter) Reset() {
tl.Logs = tl.Logs[:0]
}

func TestContextualError_Log(t *testing.T) {
l := logrus.New()
l.Formatter = &logrus.TextFormatter{
DisableTimestamp: true,
DisableColors: true,
}

tl := NewTestLogWriter()
l.Out = tl

// Test a full context line
tl.Reset()
e := NewContextualError("test message", m{"field": "1"}, errors.New("error"))
e.Log(l)
assert.Equal(t, []string{"level=error msg=\"test message\" error=error field=1\n"}, tl.Logs)

// Test a line with an error and msg but no fields
tl.Reset()
e = NewContextualError("test message", nil, errors.New("error"))
e.Log(l)
assert.Equal(t, []string{"level=error msg=\"test message\" error=error\n"}, tl.Logs)

// Test just a context and fields
tl.Reset()
e = NewContextualError("test message", m{"field": "1"}, nil)
e.Log(l)
assert.Equal(t, []string{"level=error msg=\"test message\" field=1\n"}, tl.Logs)

// Test just a context
tl.Reset()
e = NewContextualError("test message", nil, nil)
e.Log(l)
assert.Equal(t, []string{"level=error msg=\"test message\"\n"}, tl.Logs)

// Test just an error
tl.Reset()
e = NewContextualError("", nil, errors.New("error"))
e.Log(l)
assert.Equal(t, []string{"level=error error=error\n"}, tl.Logs)
}
Loading

0 comments on commit 41578ca

Please sign in to comment.