Skip to content

Commit

Permalink
fix(logging): fix stdout log http request format (#7083)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche authored Nov 22, 2022
1 parent d96cb02 commit 2894e66
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
35 changes: 35 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,41 @@ type structuredLogEntry struct {
TraceSampled bool `json:"logging.googleapis.com/trace_sampled,omitempty"`
}

func convertSnakeToMixedCase(snakeStr string) string {
words := strings.Split(snakeStr, "_")
mixedStr := words[0]
for _, word := range words[1:] {
mixedStr += strings.Title(word)
}
return mixedStr
}

func (s structuredLogEntry) MarshalJSON() ([]byte, error) {
// extract structuredLogEntry into json map
type Alias structuredLogEntry
var mapData map[string]interface{}
data, err := json.Marshal(Alias(s))
if err == nil {
err = json.Unmarshal(data, &mapData)
}
if err == nil {
// ensure all inner dicts use mixed case instead of snake case
innerDicts := [3]string{"httpRequest", "logging.googleapis.com/operation", "logging.googleapis.com/sourceLocation"}
for _, field := range innerDicts {
if fieldData, ok := mapData[field]; ok {
formattedFieldData := make(map[string]interface{})
for k, v := range fieldData.(map[string]interface{}) {
formattedFieldData[convertSnakeToMixedCase(k)] = v
}
mapData[field] = formattedFieldData
}
}
// serialize json map into raw bytes
return json.Marshal(mapData)
}
return data, err
}

func serializeEntryToWriter(entry *logpb.LogEntry, w io.Writer) error {
jsonifiedEntry := structuredLogEntry{
Severity: entry.Severity.String(),
Expand Down
19 changes: 11 additions & 8 deletions logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,12 +1281,14 @@ func TestRedirectOutputFormats(t *testing.T) {
Method: "POST",
},
},

Payload: "this is text payload",
},
want: `{"message":"this is text payload","severity":"DEBUG","httpRequest":{"request_method":"POST","request_url":"https://example.com/test"},` +
`"timestamp":"seconds:1000","logging.googleapis.com/labels":{"key1":"value1","key2":"value2"},"logging.googleapis.com/insertId":"0000AAA01",` +
`"logging.googleapis.com/operation":{"id":"0123456789","producer":"test"},"logging.googleapis.com/sourceLocation":{"file":"acme.go","line":100,"function":"main"},` +
`"logging.googleapis.com/spanId":"000000000001","logging.googleapis.com/trace":"projects/P/ABCD12345678AB12345678","logging.googleapis.com/trace_sampled":true}`,
want: `{"httpRequest":{"requestMethod":"POST","requestUrl":"https://example.com/test"},"logging.googleapis.com/insertId":"0000AAA01",` +
`"logging.googleapis.com/labels":{"key1":"value1","key2":"value2"},"logging.googleapis.com/operation":{"id":"0123456789","producer":"test"},` +
`"logging.googleapis.com/sourceLocation":{"file":"acme.go","function":"main","line":100},"logging.googleapis.com/spanId":"000000000001",` +
`"logging.googleapis.com/trace":"projects/P/ABCD12345678AB12345678","logging.googleapis.com/trace_sampled":true,` +
`"message":"this is text payload","severity":"DEBUG","timestamp":"seconds:1000"}`,
},
{
name: "full data redirect with json payload",
Expand Down Expand Up @@ -1318,10 +1320,11 @@ func TestRedirectOutputFormats(t *testing.T) {
"Latency": 321,
},
},
want: `{"message":{"Latency":321,"Message":"message part of the payload"},"severity":"DEBUG","httpRequest":{"request_method":"POST","request_url":"https://example.com/test"},` +
`"timestamp":"seconds:1000","logging.googleapis.com/labels":{"key1":"value1","key2":"value2"},"logging.googleapis.com/insertId":"0000AAA01",` +
`"logging.googleapis.com/operation":{"id":"0123456789","producer":"test"},"logging.googleapis.com/sourceLocation":{"file":"acme.go","line":100,"function":"main"},` +
`"logging.googleapis.com/spanId":"000000000001","logging.googleapis.com/trace":"projects/P/ABCD12345678AB12345678","logging.googleapis.com/trace_sampled":true}`,
want: `{"httpRequest":{"requestMethod":"POST","requestUrl":"https://example.com/test"},"logging.googleapis.com/insertId":"0000AAA01",` +
`"logging.googleapis.com/labels":{"key1":"value1","key2":"value2"},"logging.googleapis.com/operation":{"id":"0123456789","producer":"test"},` +
`"logging.googleapis.com/sourceLocation":{"file":"acme.go","function":"main","line":100},"logging.googleapis.com/spanId":"000000000001",` +
`"logging.googleapis.com/trace":"projects/P/ABCD12345678AB12345678","logging.googleapis.com/trace_sampled":true,` +
`"message":{"Latency":321,"Message":"message part of the payload"},"severity":"DEBUG","timestamp":"seconds:1000"}`,
},
{
name: "error on redirect with proto payload",
Expand Down

0 comments on commit 2894e66

Please sign in to comment.