Skip to content

Commit

Permalink
Reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
desa committed Sep 28, 2017
1 parent 347d1b6 commit 03529c8
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 139 deletions.
26 changes: 2 additions & 24 deletions services/diagnostic/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,14 @@ func (s *SessionService) handleSessions(w http.ResponseWriter, r *http.Request)
contentType := r.Header.Get("Content-Type")

// TODO: do better verification of content type here
session := s.SessionsStore.Create(&httpWriteFlusher{w: w}, contentType, tags)
session := s.SessionsStore.Create(w, contentType, tags)
defer s.SessionsStore.Delete(session)

header := w.Header()
header.Add("Transfer-Encoding", "chunked")
header.Add("Content-Type", r.Header.Get("Content-Type"))
w.WriteHeader(http.StatusOK)
// TODO: set content headers

<-r.Context().Done()
}

type WriteFlusher interface {
Write([]byte) (int, error)
Flush() error
}

type httpWriteFlusher struct {
w http.ResponseWriter
}

func (h *httpWriteFlusher) Write(buf []byte) (int, error) {
return h.w.Write(buf)
}
func (h *httpWriteFlusher) Flush() error {
flusher, ok := h.w.(http.Flusher)
if !ok {
return errors.New("failed to coerce to http.Flusher")
}

flusher.Flush()

return nil
}
6 changes: 0 additions & 6 deletions services/diagnostic/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"time"
)

type Writer interface {
Write([]byte) (int, error)
WriteByte(byte) error
WriteString(string) (int, error)
}

func writeString(w Writer, s string) (n int, err error) {
var m int
// TODO: revisit
Expand Down
57 changes: 0 additions & 57 deletions services/diagnostic/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,63 +38,6 @@ import (
plog "github.com/prometheus/common/log"
)

type Logger interface {
Error(msg string, ctx ...Field)
Warn(msg string, ctx ...Field)
Debug(msg string, ctx ...Field)
Info(msg string, ctx ...Field)
With(ctx ...Field) Logger
}

type MultiLogger struct {
loggers []Logger
}

func NewMultiLogger(loggers ...Logger) *MultiLogger {
return &MultiLogger{
loggers: loggers,
}
}

func (l *MultiLogger) Error(msg string, ctx ...Field) {
for _, logger := range l.loggers {
logger.Error(msg, ctx...)
}
}

func (l *MultiLogger) Warn(msg string, ctx ...Field) {
for _, logger := range l.loggers {
logger.Warn(msg, ctx...)
}
}

func (l *MultiLogger) Debug(msg string, ctx ...Field) {
for _, logger := range l.loggers {
logger.Debug(msg, ctx...)
}
}

func (l *MultiLogger) Info(msg string, ctx ...Field) {
for _, logger := range l.loggers {
logger.Info(msg, ctx...)
}
}

func (l *MultiLogger) With(ctx ...Field) Logger {
loggers := []Logger{}
for _, logger := range l.loggers {
loggers = append(loggers, logger.With(ctx...))
}

return NewMultiLogger(loggers...)
}

// session logger
// api and implements session

//api server
// uses session logger

func Err(l Logger, msg string, err error, ctx []keyvalue.T) {
if len(ctx) == 0 {
l.Error(msg, Error(err))
Expand Down
100 changes: 99 additions & 1 deletion services/diagnostic/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,68 @@ const (
ErrorLevel
)

type Logger interface {
Error(msg string, ctx ...Field)
Warn(msg string, ctx ...Field)
Debug(msg string, ctx ...Field)
Info(msg string, ctx ...Field)
With(ctx ...Field) Logger
}

type Writer interface {
Write([]byte) (int, error)
WriteByte(byte) error
WriteString(string) (int, error)
}

type WriteFlusher interface {
Write([]byte) (int, error)
Flush() error
}

type MultiLogger struct {
loggers []Logger
}

func NewMultiLogger(loggers ...Logger) *MultiLogger {
return &MultiLogger{
loggers: loggers,
}
}

func (l *MultiLogger) Error(msg string, ctx ...Field) {
for _, logger := range l.loggers {
logger.Error(msg, ctx...)
}
}

func (l *MultiLogger) Warn(msg string, ctx ...Field) {
for _, logger := range l.loggers {
logger.Warn(msg, ctx...)
}
}

func (l *MultiLogger) Debug(msg string, ctx ...Field) {
for _, logger := range l.loggers {
logger.Debug(msg, ctx...)
}
}

func (l *MultiLogger) Info(msg string, ctx ...Field) {
for _, logger := range l.loggers {
logger.Info(msg, ctx...)
}
}

func (l *MultiLogger) With(ctx ...Field) Logger {
loggers := []Logger{}
for _, logger := range l.loggers {
loggers = append(loggers, logger.With(ctx...))
}

return NewMultiLogger(loggers...)
}

func defaultLevelF(lvl Level) bool {
return true
}
Expand Down Expand Up @@ -104,6 +166,43 @@ func (l *ServerLogger) Log(now time.Time, level string, msg string, ctx []Field)
l.w.Flush()
}

type sessionsLogger struct {
store SessionsStore
context []Field
}

func (s *sessionsLogger) Error(msg string, ctx ...Field) {
s.store.Each(func(sn *Session) {
sn.Error(msg, s.context, ctx)
})
}

func (s *sessionsLogger) Warn(msg string, ctx ...Field) {
s.store.Each(func(sn *Session) {
sn.Warn(msg, s.context, ctx)
})
}

func (s *sessionsLogger) Debug(msg string, ctx ...Field) {
s.store.Each(func(sn *Session) {
sn.Warn(msg, s.context, ctx)
})
}

func (s *sessionsLogger) Info(msg string, ctx ...Field) {
s.store.Each(func(sn *Session) {
sn.Info(msg, s.context, ctx)
})
}

func (s *sessionsLogger) With(ctx ...Field) Logger {
// TODO: this needs some kind of locking
return &sessionsLogger{
store: s.store,
context: append(s.context, ctx...),
}
}

// TODO: actually care about errors?
func writeLogfmt(w Writer, now time.Time, level string, msg string, context, fields []Field) {

Expand Down Expand Up @@ -143,7 +242,6 @@ func writeLogfmtMessage(w Writer, msg string) {
}

// TODO: actually care about errors?
// TODO: should we ensure non-duplicate keys?
func writeJSON(w Writer, now time.Time, level string, msg string, context, fields []Field) {

w.WriteByte('{')
Expand Down
15 changes: 9 additions & 6 deletions services/diagnostic/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"strconv"
"testing"
Expand Down Expand Up @@ -439,7 +440,7 @@ func TestSessionsLoggerWithoutContext(t *testing.T) {
service := diagnostic.NewSessionService()
// TODO: we need ths?
_ = service.NewLogger()
session := service.SessionsStore.Create(&writeFlusher{buf: buf}, "application/json", nil)
session := service.SessionsStore.Create(&httpResponseWriter{buf: buf}, "application/json", nil)
defer service.SessionsStore.Delete(session)

tests := []struct {
Expand Down Expand Up @@ -791,14 +792,16 @@ func jsonStr(o interface{}) (string, error) {
return string(b), err
}

type writeFlusher struct {
type httpResponseWriter struct {
buf *bytes.Buffer
}

func (w *writeFlusher) Write(b []byte) (int, error) {
return w.buf.Write(b)
func (w *httpResponseWriter) Header() http.Header {
return http.Header{}
}

func (w *writeFlusher) Flush() error {
return nil
func (w *httpResponseWriter) Write(b []byte) (int, error) {
return w.buf.Write(b)
}

func (w *httpResponseWriter) WriteHeader(n int) {}
Loading

0 comments on commit 03529c8

Please sign in to comment.