forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#patch create gcp log formatter (flyteorg#108)
- Loading branch information
Showing
5 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters