Skip to content

Commit

Permalink
Create v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dongxuny committed Oct 6, 2022
1 parent 0c6b525 commit c406a23
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 113 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/rookie-ninja/rk-query)](https://goreportcard.com/report/github.com/rookie-ninja/rk-query)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

Human readable query logger with [zap](https://github.com/uber-go/zap), [lumberjack](https://github.com/natefinch/lumberjack) and [rk-logger](https://github.com/rookie-ninja/rk-logger)
Human-readable query logger with [zap](https://github.com/uber-go/zap), [lumberjack](https://github.com/natefinch/lumberjack) and [rk-logger](https://github.com/rookie-ninja/rk-logger)

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
Expand All @@ -21,7 +21,7 @@ Human readable query logger with [zap](https://github.com/uber-go/zap), [lumberj
<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Installation
`go get github.com/rookie-ninja/rk-query`
`go get github.com/rookie-ninja/rk-query/v2`

## Quick Start
Zap logger needs to be pass to query in order to write logs
Expand Down Expand Up @@ -68,7 +68,7 @@ func withEventConsoleEncoding() {
logger, _, _ := rklogger.NewZapLoggerWithBytes(bytes, rk_logger.JSON)

fac := rkquery.NewEventFactory(
rkquery.WithAppName("appName"),
rkquery.WithServiceName("serviceName"),
rkquery.WithEncoding(rkquery.CONSOLE),
rkquery.WithOperation("op"),
rkquery.WithLogger(logger))
Expand All @@ -95,7 +95,7 @@ startTime=2021-06-13T01:16:26.581691+08:00
elapsedNano=1003868481
timezone=CST
ids={"eventId":"581812ae-924a-44b2-83f8-fa8eef071393"}
app={"appName":"appName","appVersion":"v0.0.1","entryName":"entry-example","entryType":"example"}
service={"serviceName":"appName","serviceVersion":"v0.0.1","entryName":"entry-example","entryKind":"example"}
env={"arch":"amd64","hostname":"lark.local","realm":"*","region":"*","az":"*","domain":"*","os":"darwin"}
payloads={"f1":"f2","t2":"2021-06-13T01:16:27.58554+08:00"}
error={"my error":1}
Expand Down Expand Up @@ -149,7 +149,7 @@ func withEventJSONEncoding() {
logger, _, _ := rklogger.NewZapLoggerWithBytes(bytes, rk_logger.JSON)

fac := rkquery.NewEventFactory(
rkquery.WithAppName("appName"),
rkquery.WithServiceName("serviceName"),
rkquery.WithEncoding(rkquery.JSON),
rkquery.WithOperation("op"),
rkquery.WithLogger(logger))
Expand Down Expand Up @@ -181,9 +181,9 @@ We formatted JSON output bellow, actual logs would not be a pretty formatted JSO
"requestId":"",
"traceId":""
},
"app":{
"appName":"appName",
"appVersion":"unknown",
"service":{
"serviceName":"serviceName",
"serviceVersion":"unknown",
"entryName":"unknown",
"entryType":"unknown"
},
Expand Down Expand Up @@ -260,7 +260,7 @@ func withEventConsoleEncoding() {
logger, _, _ := rklogger.NewZapLoggerWithBytes(bytes, rk_logger.JSON)

fac := rkquery.NewEventFactory(
rkquery.WithAppName("appName"),
rkquery.WithServiceName("serviceName"),
rkquery.WithEncoding(rkquery.FLATTEN),
rkquery.WithOperation("op"),
rkquery.WithLogger(logger))
Expand Down
10 changes: 5 additions & 5 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const (
elapsedKey = "elapsedNano"
timezoneKey = "timezone"
// ************* App *************
appKey = "app"
appNameKey = "appName"
appVersionKey = "appVersion"
entryNameKey = "entryName"
entryTypeKey = "entryType"
serviceKey = "service"
serviceNameKey = "serviceName"
serviceVersionKey = "serviceVersion"
entryNameKey = "entryName"
entryKindKey = "entryKind"
// ************* Env *************
envKey = "env"
hostnameKey = "hostname"
Expand Down
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Event interface {
// Payload could be anything with RPC requests or user event such as http request param.
AddPayloads(...zap.Field)

// ListPayloads will lists payloads.
// ListPayloads will list payloads.
ListPayloads() []zap.Field

// ************* Identity *************
Expand Down
70 changes: 35 additions & 35 deletions event_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,49 +78,49 @@ func WithQuietMode(quietMode bool) EventOption {
}

// WithEntryName override entry name in Event.
func WithEntryName(entryName string) EventOption {
func WithEntryName(name string) EventOption {
return func(event Event) {
switch v := event.(type) {
case *eventZap:
v.entryName = entryName
v.entryName = name
case *eventThreadSafe:
v.delegate.entryName = entryName
v.delegate.entryName = name
}
}
}

// WithEntryType override entry type in Event.
func WithEntryType(entryType string) EventOption {
// WithEntryKind override entry kind in Event.
func WithEntryKind(kind string) EventOption {
return func(event Event) {
switch v := event.(type) {
case *eventZap:
v.entryType = entryType
v.entryKind = kind
case *eventThreadSafe:
v.delegate.entryType = entryType
v.delegate.entryKind = kind
}
}
}

// WithAppName override app name in Event.
func WithAppName(appName string) EventOption {
// WithServiceName override service name in Event.
func WithServiceName(name string) EventOption {
return func(event Event) {
switch v := event.(type) {
case *eventZap:
v.appName = appName
v.serviceName = name
case *eventThreadSafe:
v.delegate.appName = appName
v.delegate.serviceName = name
}
}
}

// WithAppVersion overrides app version in event.
func WithAppVersion(appVersion string) EventOption {
func WithAppVersion(version string) EventOption {
return func(event Event) {
switch v := event.(type) {
case *eventZap:
v.appVersion = appVersion
v.serviceVersion = version
case *eventThreadSafe:
v.delegate.appVersion = appVersion
v.delegate.serviceVersion = version
}
}
}
Expand Down Expand Up @@ -158,27 +158,27 @@ func NewEventFactory(option ...EventOption) *EventFactory {
// CreateEvent creates a new event with options.
func (factory *EventFactory) CreateEvent(options ...EventOption) Event {
event := &eventZap{
logger: rklogger.EventLogger,
encoding: CONSOLE,
quietMode: false,
appName: "",
appVersion: "",
entryName: "",
entryType: "",
eventId: generateEventId(),
traceId: "",
requestId: "",
startTime: time.Now(),
timeZone: getTimeZone(),
payloads: make([]zap.Field, 0),
errors: zapcore.NewMapObjectEncoder(),
operation: "",
remoteAddr: "localhost",
resCode: "",
status: NotStarted,
pairs: zapcore.NewMapObjectEncoder(),
counters: zapcore.NewMapObjectEncoder(),
tracker: make(map[string]*timeTracker),
logger: rklogger.EventLogger,
encoding: CONSOLE,
quietMode: false,
serviceName: "",
serviceVersion: "",
entryName: "",
entryKind: "",
eventId: generateEventId(),
traceId: "",
requestId: "",
startTime: time.Now(),
timeZone: getTimeZone(),
payloads: make([]zap.Field, 0),
errors: zapcore.NewMapObjectEncoder(),
operation: "",
remoteAddr: "localhost",
resCode: "",
status: NotStarted,
pairs: zapcore.NewMapObjectEncoder(),
counters: zapcore.NewMapObjectEncoder(),
tracker: make(map[string]*timeTracker),
}

for i := range factory.options {
Expand Down
32 changes: 16 additions & 16 deletions event_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,33 +127,33 @@ func TestWithEntryName_HappyCase(t *testing.T) {
}

func TestWithEntryType_HappyCase(t *testing.T) {
entryType := "ut-type"
opt := WithEntryType(entryType)
entryKind := "ut-kind"
opt := WithEntryKind(entryKind)

// For eventZap
event := NewEventFactory().CreateEvent()
opt(event)
assert.Equal(t, entryType, event.(*eventZap).entryType)
assert.Equal(t, entryKind, event.(*eventZap).entryKind)

// For eventThreadSafe
threadSafe := NewEventFactory().CreateEventThreadSafe()
opt(threadSafe)
assert.Equal(t, entryType, threadSafe.(*eventThreadSafe).delegate.entryType)
assert.Equal(t, entryKind, threadSafe.(*eventThreadSafe).delegate.entryKind)
}

func TestWithWithAppName_HappyCase(t *testing.T) {
appName := "ut-app"
opt := WithAppName(appName)
appName := "ut-service"
opt := WithServiceName(appName)

// For eventZap
event := NewEventFactory().CreateEvent()
opt(event)
assert.Equal(t, appName, event.(*eventZap).appName)
assert.Equal(t, appName, event.(*eventZap).serviceName)

// For eventThreadSafe
threadSafe := NewEventFactory().CreateEventThreadSafe()
opt(threadSafe)
assert.Equal(t, appName, threadSafe.(*eventThreadSafe).delegate.appName)
assert.Equal(t, appName, threadSafe.(*eventThreadSafe).delegate.serviceName)
}

func TestWithWithAppVersion_HappyCase(t *testing.T) {
Expand All @@ -163,12 +163,12 @@ func TestWithWithAppVersion_HappyCase(t *testing.T) {
// For eventZap
event := NewEventFactory().CreateEvent()
opt(event)
assert.Equal(t, appVersion, event.(*eventZap).appVersion)
assert.Equal(t, appVersion, event.(*eventZap).serviceVersion)

// For eventThreadSafe
threadSafe := NewEventFactory().CreateEventThreadSafe()
opt(threadSafe)
assert.Equal(t, appVersion, threadSafe.(*eventThreadSafe).delegate.appVersion)
assert.Equal(t, appVersion, threadSafe.(*eventThreadSafe).delegate.serviceVersion)
}

func TestWithWithOperation_HappyCase(t *testing.T) {
Expand Down Expand Up @@ -211,10 +211,10 @@ func TestEventFactory_CreateEvent_WithoutOptions(t *testing.T) {
assert.Equal(t, rklogger.EventLogger, event.(*eventZap).logger)
assert.Equal(t, CONSOLE.String(), event.(*eventZap).encoding.String())
assert.False(t, event.(*eventZap).quietMode)
assert.Empty(t, event.(*eventZap).appName)
assert.Empty(t, event.(*eventZap).appVersion)
assert.Empty(t, event.(*eventZap).serviceName)
assert.Empty(t, event.(*eventZap).serviceVersion)
assert.Empty(t, event.(*eventZap).entryName)
assert.Empty(t, event.(*eventZap).entryType)
assert.Empty(t, event.(*eventZap).entryKind)
assert.NotEmpty(t, event.(*eventZap).eventId)
assert.Empty(t, event.(*eventZap).traceId)
assert.Empty(t, event.(*eventZap).requestId)
Expand All @@ -232,13 +232,13 @@ func TestEventFactory_CreateEvent_WithoutOptions(t *testing.T) {
}

func TestEventFactory_CreateEvent_WithOptions(t *testing.T) {
fac := NewEventFactory(WithAppName("ut-app"))
fac := NewEventFactory(WithServiceName("ut-service"))
event := fac.CreateEvent(WithAppVersion("ut-version"))
assert.NotNil(t, event)

// Check default fields
assert.Equal(t, "ut-app", event.(*eventZap).appName)
assert.Equal(t, "ut-version", event.(*eventZap).appVersion)
assert.Equal(t, "ut-service", event.(*eventZap).serviceName)
assert.Equal(t, "ut-version", event.(*eventZap).serviceVersion)
}

func TestNewEventFactory_HappyCase(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion event_noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestEventNoop_AllInOne(t *testing.T) {
event := &eventNoop{}

// No panic should be occur
// No panic should be occurred
event.SetStartTime(time.Now())
assert.NotNil(t, event.GetStartTime())
event.SetEndTime(time.Now())
Expand Down
Loading

0 comments on commit c406a23

Please sign in to comment.