Skip to content

Commit

Permalink
Remove charset restrictions on tag values
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Nogueira committed Mar 26, 2018
1 parent 90f5b23 commit eb59729
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
7 changes: 7 additions & 0 deletions tag/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@ the OpenCensus instrumentation data.
Tags can be propagated on the wire and in the same
process via context.Context. Encode and Decode should be
used to represent tags into their binary propagation form.
This package supports a restrictive set of characters in tag keys which
we believe are supported by most metrics backends. Tag values are not limited in
this way, but specific exporters may have their own restrictions on tag
values and if so, should provide a way to sanitize tag values for use
with that backend.
*/
package tag // import "go.opencensus.io/tag"
21 changes: 13 additions & 8 deletions tag/map_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestEncodeDecode(t *testing.T) {
[]keyValue{
{k1, "v1"},
{k2, "v2"},
{k3, "v3"},
{k3, "数据库着火了 🔥🔥🔥"},
{k4, "v4 is very weird <>.,?/'\";:`~!@#$%^&*()_-+={[}]|\\"},
},
},
Expand Down Expand Up @@ -107,6 +107,7 @@ func TestEncodeDecode(t *testing.T) {
func TestDecode(t *testing.T) {
k1, _ := NewKey("k1")
ctx, _ := New(context.Background(), Insert(k1, "v1"))
ctx2, _ := New(context.Background(), Insert(k1, "数据库着火了 🔥🔥🔥"))

tests := []struct {
name string
Expand All @@ -121,14 +122,14 @@ func TestDecode(t *testing.T) {
wantErr: false,
},
{
name: "non-ascii key",
bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 25, 2, 118, 49},
want: nil,
wantErr: true,
name: "valid (non-ascii value)",
bytes: []byte{0x0, 0x0, 0x2, 0x6b, 0x31, 0x1f, 0xe6, 0x95, 0xb0, 0xe6, 0x8d, 0xae, 0xe5, 0xba, 0x93, 0xe7, 0x9d, 0x80, 0xe7, 0x81, 0xab, 0xe4, 0xba, 0x86, 0x20, 0xf0, 0x9f, 0x94, 0xa5, 0xf0, 0x9f, 0x94, 0xa5, 0xf0, 0x9f, 0x94, 0xa5},
want: FromContext(ctx2),
wantErr: false,
},
{
name: "non-ascii value",
bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 50, 2, 118, 25},
name: "non-ascii key",
bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 25, 2, 118, 49},
want: nil,
wantErr: true,
},
Expand All @@ -147,7 +148,11 @@ func TestDecode(t *testing.T) {
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Decode() = %v, want %v", got, tt.want)
var encoded []byte
if tt.want != nil {
encoded = Encode(tt.want)
}
t.Errorf("Decode() = %v, want %v = Decode(%#v)", got, tt.want, encoded)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions tag/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (

var (
errInvalidKeyName = errors.New("invalid key name: only ASCII characters accepted; max length must be 255 characters")
errInvalidValue = errors.New("invalid value: only ASCII characters accepted; max length must be 255 characters")
errInvalidValue = errors.New("invalid value: max length must be 255 UTF-8 characters")
)

func checkKeyName(name string) bool {
Expand All @@ -52,5 +52,5 @@ func checkValue(v string) bool {
if len(v) > maxKeyLength {
return false
}
return isASCII(v)
return true
}
25 changes: 15 additions & 10 deletions tag/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,6 @@ func TestCheckValue(t *testing.T) {
value: "v1",
wantOK: true,
},
{
name: "invalid i",
value: "k\x19",
wantOK: false,
},
{
name: "invalid ii",
value: "k\x7f",
wantOK: false,
},
{
name: "empty",
value: "",
Expand All @@ -100,6 +90,21 @@ func TestCheckValue(t *testing.T) {
value: strings.Repeat("a", 256),
wantOK: false,
},
{
name: "emoji",
value: "🔥🔥🔥",
wantOK: true,
},
{
name: "Simplified Chinese",
value: "上海",
wantOK: true,
},
{
name: "Bengali",
value: "বাংলা/বঙ্গ",
wantOK: true,
},
}
for _, tt := range tests {
ok := checkValue(tt.value)
Expand Down

0 comments on commit eb59729

Please sign in to comment.