Skip to content

Commit

Permalink
[rk-query] Refactoring with noop event and threadsafe event [commit:d…
Browse files Browse the repository at this point in the history
…ongxuny]
  • Loading branch information
dongxuny committed Jul 30, 2020
1 parent 1c62256 commit 90237bf
Show file tree
Hide file tree
Showing 11 changed files with 613 additions and 106 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ var (
}`)
)

func withEventZapRkFormat() {
func withEventRkFormat() {
logger, _, _ := rk_logger.NewZapLoggerWithBytes(bytes, rk_logger.JSON)

fac := rk_query.NewEventZapFactory(
fac := rk_query.NewEventFactory(
rk_query.WithAppName("appName"),
rk_query.WithFormat(rk_query.RK),
rk_query.WithOperation("op"),
rk_query.WithLogger(logger))
event := fac.CreateEventZap()
event := fac.CreateEvent()

event.SetStartTime(time.Now())
event.StartTimer("t1")
Expand All @@ -77,7 +77,7 @@ Output
end_time=2020-07-30T03:42:22.393874+08:00
start_time=2020-07-30T03:42:21.390642+08:00
time=1003
hostname=JEREMYYIN-MB0
hostname=MYLOCAL
timing={"t1.count":1,"t1.elapsed_ms":1003}
counter={"count":1}
pair={"key":"value"}
Expand Down Expand Up @@ -127,15 +127,15 @@ var (
}`)
)

func withEventZapRkFormat() {
func withEventJSONFormat() {
logger, _, _ := rk_logger.NewZapLoggerWithBytes(bytes, rk_logger.JSON)

fac := rk_query.NewEventZapFactory(
fac := rk_query.NewEventFactory(
rk_query.WithAppName("appName"),
rk_query.WithFormat(rk_query.JSON),
rk_query.WithOperation("op"),
rk_query.WithLogger(logger))
event := fac.CreateEventZap()
event := fac.CreateEvent()

event.SetStartTime(time.Now())
event.StartTimer("t1")
Expand All @@ -156,7 +156,7 @@ We formatted JSON output bellow, actual logs would not be a pretty formatted JSO
"end_time":"2020-07-30T03:42:23.398+0800",
"start_time":"2020-07-30T03:42:22.394+0800",
"time":1004,
"hostname":"JEREMYYIN-MB0",
"hostname":"MYLOCAL",
"timing":{
"t1.count":1,
"t1.elapsed_ms":1004
Expand Down
43 changes: 22 additions & 21 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
package rk_query

const (
ScopeDelimiter string = "------------------------------------------------------------------------"
EOE string = "EOE"
Unknown string = "Unknown"
Truncated string = "TRUNCATED"
CommaTruncated string = ",TRUNCATED"
MaxHistoryLength int = 1024
OpenMarker string = "-open-"
appNameKey = "app_name"
hostnameKey = "hostname"
operationKey = "operation"
remoteAddrKey = "remote_addr"
eventStatusKey = "event_status"
historyKey = "history"
startTimeKey = "start_time"
endTimeKey = "end_time"
timeKey = "time"
timingKey = "timing"
counterKey = "counter"
pairKey = "pair"
errKey = "error"
fieldKey = "field"
ScopeDelimiter = "------------------------------------------------------------------------"
EOE = "EOE"
Unknown = "Unknown"
Truncated = "TRUNCATED"
CommaTruncated = ",TRUNCATED"
MaxHistoryLength = 1024
OpenMarker = "-open-"
appNameKey = "app_name"
hostnameKey = "hostname"
eventIdKey = "event_id"
operationKey = "operation"
remoteAddrKey = "remote_addr"
eventStatusKey = "event_status"
historyKey = "history"
startTimeKey = "start_time"
endTimeKey = "end_time"
timeKey = "time"
timingKey = "timing"
counterKey = "counter"
pairKey = "pair"
errKey = "error"
fieldKey = "field"
)
78 changes: 78 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package rk_query

import (
"go.uber.org/zap"
"time"
)

type Event interface {
GetValue(string) string

GetAppName() string

GetEventId() string

SetEventId(string)

GetHostname() string

GetLogger() *zap.Logger

GetOperation() string

SetOperation(string)

GetEventStatus() eventStatus

SetStartTime(time.Time)

GetStartTime() time.Time

GetEndTime() time.Time

SetEndTime(time.Time)

StartTimer(string)

EndTimer(string)

UpdateTimer(string, int64)

UpdateTimerWithSample(string, int64, int64)

GetTimeElapsedMS(string) int64

GetRemoteAddr() string

SetRemoteAddr(string)

GetCounter(string) int64

SetCounter(string, int64)

InCCounter (string, int64)

AddPair(string, string)

AddErr(error)

GetErrCount(error) int64

AddFields(...zap.Field)

GetFields() []zap.Field

RecordHistoryEvent(string)

WriteLog()

setLogger(*zap.Logger)

setFormat(Format)

setQuietMode(bool)

setAppName(string)

setHostname(string)
}
84 changes: 53 additions & 31 deletions event_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,85 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"sync"
)

type EventZapOption func(*EventZap)
type EventOption func(Event)

func WithLogger(logger *zap.Logger) EventZapOption {
return func(event *EventZap) {
event.logger = logger
func WithLogger(logger *zap.Logger) EventOption {
return func(event Event) {
event.setLogger(logger)
}
}

func WithFormat(format Format) EventZapOption {
return func(event *EventZap) {
event.format = format
func WithFormat(format Format) EventOption {
return func(event Event) {
event.setFormat(format)
}
}

func WithQuietMode(quietMode bool) EventZapOption {
return func(event *EventZap) {
event.quietMode = quietMode
func WithQuietMode(quietMode bool) EventOption {
return func(event Event) {
event.setQuietMode(quietMode)
}
}

func WithAppName(appName string) EventZapOption {
return func(event *EventZap) {
event.appName = appName
func WithAppName(appName string) EventOption {
return func(event Event) {
event.setAppName(appName)
}
}

func WithHostname(hostname string) EventZapOption {
return func(event *EventZap) {
event.hostname = hostname
func WithHostname(hostname string) EventOption {
return func(event Event) {
event.setHostname(hostname)
}
}

func WithOperation(operation string) EventZapOption {
return func(event *EventZap) {
event.operation = operation
func WithOperation(operation string) EventOption {
return func(event Event) {
event.SetOperation(operation)
}
}

func WithRemoteAddr(addr string) EventZapOption {
return func(event *EventZap) {
event.remoteAddr = addr
func WithRemoteAddr(addr string) EventOption {
return func(event Event) {
event.SetRemoteAddr(addr)
}
}

func WithFields(fields []zap.Field) EventZapOption {
return func(event *EventZap) {
event.fields = append(event.fields, fields...)
func WithFields(fields []zap.Field) EventOption {
return func(event Event) {
event.AddFields(fields...)
}
}

// Not thread safe!!!
type EventZapFactory struct {
options []EventZapOption
type EventFactory struct {
appName string
options []EventOption
}

func NewEventZapFactory(option ...EventZapOption) *EventZapFactory {
return &EventZapFactory{
func NewEventFactory(option ...EventOption) *EventFactory {
factory := &EventFactory{
options: option,
}

return factory
}

func (factory *EventFactory) GetAppName() string {
return factory.appName
}

func (factory *EventZapFactory) CreateEventZap(options ...EventZapOption) *EventZap {
func (factory *EventFactory) CreateEvent(options ...EventOption) Event {
event := &EventZap{
logger: zap.NewNop(),
format: RK,
status: notStarted,
appName: Unknown,
hostname: obtainHostName(),
remoteAddr: Unknown,
remoteAddr: obtainHostName(),
operation: Unknown,
counters: zapcore.NewMapObjectEncoder(),
pairs: zapcore.NewMapObjectEncoder(),
Expand All @@ -97,6 +105,8 @@ func (factory *EventZapFactory) CreateEventZap(options ...EventZapOption) *Event
opt(event)
}

factory.appName = event.GetAppName()

event.logger.Core().Sync()

if !event.quietMode {
Expand All @@ -106,6 +116,18 @@ func (factory *EventZapFactory) CreateEventZap(options ...EventZapOption) *Event
return event
}

func (factory *EventFactory) CreateEventNoop() Event {
return &EventNoop{}
}

func (factory *EventFactory) CreateEventThreadSafe(options ...EventOption) Event {
event := factory.CreateEvent(options...)
return &EventThreadSafe{
delegate: event,
lock: &sync.Mutex{},
}
}

func obtainHostName() string {
hostName, err := os.Hostname()

Expand Down
16 changes: 8 additions & 8 deletions event_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
"testing"
)

func TestNewEventZapFactoryHappyCase(t *testing.T) {
fac := NewEventZapFactory()
func TestNewEventFactoryHappyCase(t *testing.T) {
fac := NewEventFactory()
assert.NotNil(t, fac)
}

func TestEventZapFactory_CreateEvent(t *testing.T) {
fac := NewEventZapFactory()
event := fac.CreateEventZap()
func TestEventFactory_CreateEvent(t *testing.T) {
fac := NewEventFactory()
event := fac.CreateEvent()
assert.NotNil(t, event)
}

func TestEventZapFactory_CreateEvent_WithAppName(t *testing.T) {
fac := NewEventZapFactory()
event := fac.CreateEventZap(WithAppName("app"))
func TestEventFactory_CreateEvent_WithAppName(t *testing.T) {
fac := NewEventFactory()
event := fac.CreateEvent(WithAppName("app"))
assert.NotNil(t, event)
assert.Equal(t, "appName", event.GetAppName())
}
Loading

0 comments on commit 90237bf

Please sign in to comment.