-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eventpb: new JSON serialization with redaction markers
This patch introduces a new code generator and infrastructure to emit structured event payloads using the JSON syntax, but also including redaction markers for fields not marked explicitly as safe for reporting. This is not yet connected to the remainder of the logging system -- this change will be performed in a later commit. Release note: None
- Loading branch information
Showing
20 changed files
with
3,203 additions
and
885 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,8 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "jsonbytes", | ||
srcs = ["jsonbytes.go"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/util/jsonbytes", | ||
visibility = ["//visibility:public"], | ||
) |
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,68 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package jsonbytes | ||
|
||
import "unicode/utf8" | ||
|
||
const hexAlphabet = "0123456789abcdef" | ||
|
||
// EncodeString writes a string literal to b as a JSON string. | ||
// Note that the delimiting double quotes are not included. | ||
// Cribbed from https://github.com/golang/go/blob/7badae85f20f1bce4cc344f9202447618d45d414/src/encoding/json/encode.go. | ||
func EncodeString(buf []byte, s string) []byte { | ||
start := 0 | ||
for i := 0; i < len(s); { | ||
if b := s[i]; b < utf8.RuneSelf { | ||
if b >= ' ' && b != '"' && b != '\\' { | ||
i++ | ||
continue | ||
} | ||
if start < i { | ||
buf = append(buf, s[start:i]...) | ||
} | ||
switch b { | ||
case '\\', '"': | ||
buf = append(buf, '\\', b) | ||
case '\n': | ||
buf = append(buf, '\\', 'n') | ||
case '\r': | ||
buf = append(buf, '\\', 'r') | ||
case '\t': | ||
buf = append(buf, '\\', 't') | ||
default: | ||
// This encodes bytes < 0x20 except for \t, \n and \r. | ||
// If escapeHTML is set, it also escapes <, >, and & | ||
// because they can lead to security holes when | ||
// user-controlled strings are rendered into JSON | ||
// and served to some browsers. | ||
buf = append(buf, '\\', 'u', '0', '0', hexAlphabet[b>>4], hexAlphabet[b&0xF]) | ||
} | ||
i++ | ||
start = i | ||
continue | ||
} | ||
c, size := utf8.DecodeRuneInString(s[i:]) | ||
if c == utf8.RuneError && size == 1 { | ||
if start < i { | ||
buf = append(buf, s[start:i]...) | ||
} | ||
buf = append(buf, `\ufffd`...) | ||
i += size | ||
start = i | ||
continue | ||
} | ||
i += size | ||
} | ||
if start < len(s) { | ||
buf = append(buf, s[start:]...) | ||
} | ||
return buf | ||
} |
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
Oops, something went wrong.