Skip to content

Commit

Permalink
Revert "Remove charset restrictions on tag values (census-instrumenta…
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll authored Apr 6, 2018
1 parent c40611a commit 5e4356a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 59 deletions.
7 changes: 0 additions & 7 deletions tag/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,5 @@ 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: 8 additions & 13 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, "数据库着火了 🔥🔥🔥"},
{k3, "v3"},
{k4, "v4 is very weird <>.,?/'\";:`~!@#$%^&*()_-+={[}]|\\"},
},
},
Expand Down Expand Up @@ -107,7 +107,6 @@ 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,18 +120,18 @@ func TestDecode(t *testing.T) {
want: FromContext(ctx),
wantErr: false,
},
{
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 key",
bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 25, 2, 118, 49},
want: nil,
wantErr: true,
},
{
name: "non-ascii value",
bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 50, 2, 118, 25},
want: nil,
wantErr: true,
},
{
name: "long value",
bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 50, 172, 2, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97},
Expand All @@ -148,11 +147,7 @@ func TestDecode(t *testing.T) {
return
}
if !reflect.DeepEqual(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)
t.Errorf("Decode() = %v, want %v", got, tt.want)
}
})
}
Expand Down
44 changes: 22 additions & 22 deletions tag/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func TestNewMap(t *testing.T) {
name: "from empty; invalid",
initial: nil,
mods: []Mutator{
Insert(k5, strings.Repeat("x", 256)),
Insert(k5, "v\x19"),
Upsert(k5, "v\x19"),
Update(k5, "v\x19"),
},
want: nil,
},
Expand All @@ -138,33 +140,31 @@ func TestNewMap(t *testing.T) {
initial: nil,
mods: []Mutator{
Insert(k5, "v1"),
Update(k5, strings.Repeat("x", 256)),
Update(k5, "v\x19"),
},
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mods := []Mutator{
Insert(k1, "v1"),
Insert(k2, "v2"),
Update(k3, "v3"),
Upsert(k4, "v4"),
Insert(k2, "v2"),
Delete(k1),
}
mods = append(mods, tt.mods...)
ctx := NewContext(context.Background(), tt.initial)
ctx, err := New(ctx, mods...)
if tt.want != nil && err != nil {
t.Errorf("New = %v", err)
}
mods := []Mutator{
Insert(k1, "v1"),
Insert(k2, "v2"),
Update(k3, "v3"),
Upsert(k4, "v4"),
Insert(k2, "v2"),
Delete(k1),
}
mods = append(mods, tt.mods...)
ctx := NewContext(context.Background(), tt.initial)
ctx, err := New(ctx, mods...)
if tt.want != nil && err != nil {
t.Errorf("%v: New = %v", tt.name, err)
}

if got, want := FromContext(ctx), tt.want; !reflect.DeepEqual(got, want) {
t.Errorf("got %v; want %v", got, want)
}
})
if got, want := FromContext(ctx), tt.want; !reflect.DeepEqual(got, want) {
t.Errorf("%v: got %v; want %v", tt.name, got, want)
}
}
}

Expand All @@ -183,7 +183,7 @@ func TestNewMapValidation(t *testing.T) {
// Value validation
{err: "", seed: &Map{m: map[Key]string{{name: "key"}: ""}}},
{err: "", seed: &Map{m: map[Key]string{{name: "key"}: strings.Repeat("a", 255)}}},
{err: "", seed: &Map{m: map[Key]string{{name: "key"}: "Приве́т"}}},
{err: "invalid value", seed: &Map{m: map[Key]string{{name: "key"}: "Приве́т"}}},
{err: "invalid value", seed: &Map{m: map[Key]string{{name: "key"}: strings.Repeat("a", 256)}}},
}

Expand Down
7 changes: 5 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: max length must be 255 UTF-8 characters")
errInvalidValue = errors.New("invalid value: only ASCII characters accepted; max length must be 255 characters")
)

func checkKeyName(name string) bool {
Expand All @@ -49,5 +49,8 @@ func isASCII(s string) bool {
}

func checkValue(v string) bool {
return len(v) <= maxKeyLength
if len(v) > maxKeyLength {
return false
}
return isASCII(v)
}
25 changes: 10 additions & 15 deletions tag/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ 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 @@ -90,21 +100,6 @@ 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 5e4356a

Please sign in to comment.