Skip to content

Commit

Permalink
feat(lib/logger): add parameters to add extra values
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Jun 19, 2022
1 parent d95d31e commit 90347f4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
6 changes: 3 additions & 3 deletions lib-ayd/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func Example_probePlugin() {
ok := true

if ok {
logger.Healthy("target is healthy!")
logger.Healthy("target is healthy!", nil)
} else {
logger.Failure("target is down")
logger.Failure("target is down", nil)
}
}

Expand All @@ -38,7 +38,7 @@ func Example_alertPlugin() {

// send alert to somewhere

logger.Healthy("alert sent")
logger.Healthy("alert sent", nil)
}

func Example_apiClient() {
Expand Down
15 changes: 10 additions & 5 deletions lib-ayd/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,50 +68,55 @@ func (l Logger) Print(r Record) error {
// Healthy prints Healthy status record.
//
// Seealso StatusHealthy.
func (l Logger) Healthy(message string) error {
func (l Logger) Healthy(message string, extra map[string]interface{}) error {
return l.Print(Record{
Status: StatusHealthy,
Message: message,
Extra: extra,
})
}

// Aborted prints Aborted status record.
//
// Seealso StatusAborted.
func (l Logger) Aborted(message string) error {
func (l Logger) Aborted(message string, extra map[string]interface{}) error {
return l.Print(Record{
Status: StatusAborted,
Message: message,
Extra: extra,
})
}

// Unknown prints Unknown status record.
//
// Seealso StatusUnknown.
func (l Logger) Unknown(message string) error {
func (l Logger) Unknown(message string, extra map[string]interface{}) error {
return l.Print(Record{
Status: StatusUnknown,
Message: message,
Extra: extra,
})
}

// Degrade prints Degrade status record.
//
// Seealso StatusDegrade.
func (l Logger) Degrade(message string) error {
func (l Logger) Degrade(message string, extra map[string]interface{}) error {
return l.Print(Record{
Status: StatusDegrade,
Message: message,
Extra: extra,
})
}

// Failure prints Failure status record.
//
// Seealso StatusFailure.
func (l Logger) Failure(message string) error {
func (l Logger) Failure(message string, extra map[string]interface{}) error {
return l.Print(Record{
Status: StatusFailure,
Message: message,
Extra: extra,
})
}

Expand Down
60 changes: 41 additions & 19 deletions lib-ayd/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func ExampleLogger() {

logger := ayd.NewLogger(target)

logger.Healthy("hello world")
logger.Healthy("hello world", nil)
logger.Failure("hello world", map[string]interface{}{
"extra": "information",
})
}

func ExampleLogger_setExtraValues() {
Expand All @@ -31,17 +34,25 @@ func ExampleLogger_setExtraValues() {
logger = logger.WithTime(startTime, latency)

// report target status with a message
logger.Healthy("target is healthy")
logger.Degrade("target is partialy working")
logger.Failure("target seems down")
logger.Unknown("failed to check, so target status is unknown")
logger.Aborted("the check was aborted by user or something")
logger.Healthy("target is healthy", map[string]interface{}{
"hello": "world",
})
logger.Degrade("target is partialy working", map[string]interface{}{
"number": 42,
})
logger.Failure("target seems down", map[string]interface{}{
"list": []string{"hello", "world"},
})
logger.Unknown("failed to check, so target status is unknown", map[string]interface{}{
"obj": map[string]string{"hello": "world"},
})
logger.Aborted("the check was aborted by user or something", nil)

// Output:
// {"time":"2001-02-03T16:05:06+09:00", "status":"HEALTHY", "latency":123.000, "target":"foobar:your-plugin-url", "message":"target is healthy"}
// {"time":"2001-02-03T16:05:06+09:00", "status":"DEGRADE", "latency":123.000, "target":"foobar:your-plugin-url", "message":"target is partialy working"}
// {"time":"2001-02-03T16:05:06+09:00", "status":"FAILURE", "latency":123.000, "target":"foobar:your-plugin-url", "message":"target seems down"}
// {"time":"2001-02-03T16:05:06+09:00", "status":"UNKNOWN", "latency":123.000, "target":"foobar:your-plugin-url", "message":"failed to check, so target status is unknown"}
// {"time":"2001-02-03T16:05:06+09:00", "status":"HEALTHY", "latency":123.000, "target":"foobar:your-plugin-url", "message":"target is healthy", "hello":"world"}
// {"time":"2001-02-03T16:05:06+09:00", "status":"DEGRADE", "latency":123.000, "target":"foobar:your-plugin-url", "message":"target is partialy working", "number":42}
// {"time":"2001-02-03T16:05:06+09:00", "status":"FAILURE", "latency":123.000, "target":"foobar:your-plugin-url", "message":"target seems down", "list":["hello","world"]}
// {"time":"2001-02-03T16:05:06+09:00", "status":"UNKNOWN", "latency":123.000, "target":"foobar:your-plugin-url", "message":"failed to check, so target status is unknown", "obj":{"hello":"world"}}
// {"time":"2001-02-03T16:05:06+09:00", "status":"ABORTED", "latency":123.000, "target":"foobar:your-plugin-url", "message":"the check was aborted by user or something"}
}

Expand All @@ -61,6 +72,16 @@ func ExampleLogger_Print() {
Message: "without status",
})

logger.Print(ayd.Record{
Target: &ayd.URL{Scheme: "foo", Host: "bar"},
Status: ayd.StatusHealthy,
Time: time.Date(2001, 2, 3, 16, 5, 6, 7, time.UTC),
Message: "with extra",
Extra: map[string]interface{}{
"hello": "world",
},
})

err := logger.Print(ayd.Record{
Time: time.Date(2001, 2, 3, 16, 5, 8, 0, time.UTC),
Message: "without target",
Expand All @@ -70,6 +91,7 @@ func ExampleLogger_Print() {
// Output:
// {"time":"2001-02-03T16:05:06Z", "status":"HEALTHY", "latency":0.000, "target":"foo://bar", "message":"hello world"}
// {"time":"2001-02-03T16:05:07Z", "status":"UNKNOWN", "latency":0.000, "target":"foo://bar", "message":"without status"}
// {"time":"2001-02-03T16:05:06Z", "status":"HEALTHY", "latency":0.000, "target":"foo://bar", "message":"with extra", "hello":"world"}
// error: invalid record: the target URL is required
}

Expand All @@ -78,7 +100,7 @@ func ExampleLogger_WithTarget() {

target, _ := ayd.ParseURL("foobar:your-plugin-url")

logger.WithTarget(target).Healthy("hello world")
logger.WithTarget(target).Healthy("hello world", nil)
}

func ExampleLogger_WithTime() {
Expand All @@ -89,7 +111,7 @@ func ExampleLogger_WithTime() {
startTime, _ := time.Parse(time.RFC3339, "2001-02-03T16:05:06+09:00")
latency := 123 * time.Millisecond

logger.WithTime(startTime, latency).Healthy("hello world")
logger.WithTime(startTime, latency).Healthy("hello world", nil)

// Output:
// {"time":"2001-02-03T16:05:06+09:00", "status":"HEALTHY", "latency":123.000, "target":"foobar:your-plugin-url", "message":"hello world"}
Expand All @@ -102,7 +124,7 @@ func ExampleLogger_StartTimer() {

l := logger.StartTimer()
// check your target status
l.Healthy("hello world")
l.Healthy("hello world", nil)
}

func ExampleLogger_StopTimer() {
Expand All @@ -116,7 +138,7 @@ func ExampleLogger_StopTimer() {

// do something, for example calculate result of the check

l.Healthy("hello world")
l.Healthy("hello world", nil)
}

func TestLogger_Print(t *testing.T) {
Expand All @@ -135,20 +157,20 @@ func TestLogger_Print(t *testing.T) {
target, _ := ayd.ParseURL("dummy:")
l := ayd.NewLoggerWithWriter(buf, target)

l.Healthy("hello")
l.Healthy("hello", nil)
assert(`^{"time":"[-+:0-9TZ]+", "status":"HEALTHY", "latency":0\.000, "target":"dummy:", "message":"hello"}` + "\n$")

l.Failure("world")
l.Failure("world", nil)
assert(`^{"time":"[-+:0-9TZ]+", "status":"FAILURE", "latency":0\.000, "target":"dummy:", "message":"world"}` + "\n$")

l.StartTimer().Healthy("no-delay")
l.StartTimer().Healthy("no-delay", nil)
assert(`^{"time":"[-+:0-9TZ]+", "status":"HEALTHY", "latency":0\.[0-9]{3}, "target":"dummy:", "message":"no-delay"}` + "\n$")

l.StartTimer().StopTimer().Healthy("no-delay-stop")
l.StartTimer().StopTimer().Healthy("no-delay-stop", nil)
assert(`^{"time":"[-+:0-9TZ]+", "status":"HEALTHY", "latency":0\.[0-9]{3}, "target":"dummy:", "message":"no-delay-stop"}` + "\n$")

l2 := l.StartTimer()
time.Sleep(100 * time.Millisecond)
l2.Healthy("with-delay")
l2.Healthy("with-delay", nil)
assert(`^{"time":"[-+:0-9TZ]+", "status":"HEALTHY", "latency":[0-9]{3}\.[0-9]{3}, "target":"dummy:", "message":"with-delay"}` + "\n$")
}

0 comments on commit 90347f4

Please sign in to comment.