Skip to content

Commit

Permalink
#patch create gcp log formatter (flyteorg#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckiosidis authored Nov 25, 2021
1 parent 21d07de commit b4c933a
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 5 deletions.
1 change: 1 addition & 0 deletions flytestdlib/logger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type FormatterType = string
const (
FormatterJSON FormatterType = "json"
FormatterText FormatterType = "text"
FormatterGCP FormatterType = "gcp"
)

const (
Expand Down
66 changes: 66 additions & 0 deletions flytestdlib/logger/gcp_formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package logger

import (
"encoding/json"
"fmt"
"time"

"github.com/sirupsen/logrus"
)

// GcpFormatter Log formatter compatible with GCP stackdriver logging.
type GcpFormatter struct {
}

type GcpEntry struct {
Data map[string]interface{} `json:"data,omitempty"`
Message string `json:"message,omitempty"`
Severity GcpSeverity `json:"severity,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
}

type GcpSeverity = string

// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
const (
GcpSeverityDebug GcpSeverity = "DEBUG"
GcpSeverityInfo GcpSeverity = "INFO"
GcpSeverityWarning GcpSeverity = "WARNING"
GcpSeverityError GcpSeverity = "ERROR"
GcpSeverityCritical GcpSeverity = "CRITICAL"
GcpSeverityAlert GcpSeverity = "ALERT"
)

var (
logrusToGcp = map[logrus.Level]GcpSeverity{
logrus.DebugLevel: GcpSeverityDebug,
logrus.InfoLevel: GcpSeverityInfo,
logrus.WarnLevel: GcpSeverityWarning,
logrus.ErrorLevel: GcpSeverityError,
logrus.FatalLevel: GcpSeverityCritical,
logrus.PanicLevel: GcpSeverityAlert,
}
)

func toGcpSeverityOrDefault(level logrus.Level) GcpSeverity {
if severity, found := logrusToGcp[level]; found {
return severity
}
// default value
return GcpSeverityDebug
}

func (f *GcpFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var log = &GcpEntry{
Timestamp: entry.Time.Format(time.RFC3339),
Message: entry.Message,
Severity: toGcpSeverityOrDefault(entry.Level),
Data: entry.Data,
}

serialized, err := json.Marshal(log)
if err != nil {
return nil, fmt.Errorf("Failed to marshal fields to JSON, %w", err)
}
return append(serialized, '\n'), nil
}
62 changes: 62 additions & 0 deletions flytestdlib/logger/gcp_formatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package logger

import (
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func Test_gcpFormatter(t *testing.T) {
tests := []struct {
name string
entry *logrus.Entry
expected string
}{
{"test", &logrus.Entry{
Logger: nil,
Data: map[string]interface{}{"src": "some-src"},
Time: time.Date(2000, 01, 01, 01, 01, 000, 000, time.UTC),
Level: 1,
Caller: nil,
Message: "some-message",
Buffer: nil,
Context: nil,
}, "{\"data\":{\"src\":\"some-src\"},\"message\":\"some-message\",\"severity\":\"CRITICAL\",\"timestamp\":\"2000-01-01T01:01:00Z\"}\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
formatter := GcpFormatter{}
bytelog, err := formatter.Format(tt.entry)
assert.Nil(t, err)
assert.Equal(t, tt.expected, string(bytelog))
})
}
}

func Test_gcpFormatterFails(t *testing.T) {
tests := []struct {
name string
entry *logrus.Entry
errMsg string
}{
{"test", &logrus.Entry{
Logger: nil,
Data: map[string]interface{}{"src": make(chan int)},
Time: time.Date(2000, 01, 01, 01, 01, 000, 000, time.UTC),
Level: 1,
Caller: nil,
Message: "some-message",
Buffer: nil,
Context: nil,
}, "Failed to marshal fields to JSON, json: unsupported type: chan int"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
formatter := GcpFormatter{}
_, err := formatter.Format(tt.entry)
assert.Equal(t, tt.errMsg, err.Error())
})
}
}
11 changes: 7 additions & 4 deletions flytestdlib/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ package logger

import (
"context"
"io"

"github.com/flyteorg/flytestdlib/contextutils"

"fmt"
"io"
"runtime"
"strings"

"github.com/flyteorg/flytestdlib/contextutils"

"github.com/sirupsen/logrus"
)

Expand All @@ -35,6 +34,10 @@ func onConfigUpdated(cfg Config) {
},
})
}
case FormatterGCP:
if _, isGCP := logrus.StandardLogger().Formatter.(*GcpFormatter); !isGCP {
logrus.SetFormatter(&GcpFormatter{})
}
default:
if _, isJSON := logrus.StandardLogger().Formatter.(*logrus.JSONFormatter); !isJSON {
logrus.SetFormatter(&logrus.JSONFormatter{
Expand Down
4 changes: 3 additions & 1 deletion flytestdlib/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,9 @@ func Test_onConfigUpdated(t *testing.T) {
name string
args args
}{
// TODO: Add test cases.
{"testtext", args{Config{Formatter: FormatterConfig{FormatterText}}}},
{"testjson", args{Config{Formatter: FormatterConfig{FormatterJSON}}}},
{"testgcp", args{Config{Formatter: FormatterConfig{FormatterGCP}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit b4c933a

Please sign in to comment.