Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pandora_sender inner set pandora key convert #611

Merged
merged 1 commit into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sender/pandora/pandora.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,9 @@ func (s *Sender) checkSchemaUpdate() {
}

func (s *Sender) Send(datas []Data) (se error) {
for i, v := range datas {
datas[i] = DeepConvertKey(v)
}
switch s.sendType {
case SendTypeRaw:
return s.rawSend(datas)
Expand Down
11 changes: 8 additions & 3 deletions sender/pandora/pandora_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,11 @@ func TestPandoraExtraInfo(t *testing.T) {
if err != nil {
t.Fatal(err)
}
d = Data{}
d["x1"] = "123.2"
d = Data{
"*x1": "123.2",
"x2.dot": "123.2",
"@timestamp": "2018-07-18T10:17:36.549054846+08:00",
}
err = s.Send([]Data{d})
if st, ok := err.(*StatsError); ok {
err = st.ErrorDetail
Expand All @@ -1035,5 +1038,7 @@ func TestPandoraExtraInfo(t *testing.T) {
t.Error(err)
}
resp = pandora.Body
assert.Equal(t, resp, "x1=123.2")
assert.Equal(t, true, strings.Contains(resp, "x1=123.2"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实可以用 assert.True(t, xxx),不过不改效果也一样

assert.Equal(t, true, strings.Contains(resp, "x2_dot=123.2"))
assert.Equal(t, true, strings.Contains(resp, "timestamp=2018-07-18T10:17:36.549054846+08:00"))
}
14 changes: 1 addition & 13 deletions transforms/mutate/pandorakey_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,13 @@ func (g *PandoraKeyConvert) RawTransform(datas []string) ([]string, error) {

func (g *PandoraKeyConvert) Transform(datas []Data) ([]Data, error) {
for i, v := range datas {
datas[i] = deepConvertKey(v)
datas[i] = DeepConvertKey(v)
}

g.stats, _ = transforms.SetStatsInfo(nil, g.stats, 0, int64(len(datas)), g.Type())
return datas, nil
}

func deepConvertKey(data map[string]interface{}) map[string]interface{} {
newData := make(map[string]interface{})
for k, v := range data {
nk := PandoraKey(k)
if nv, ok := v.(map[string]interface{}); ok {
v = deepConvertKey(nv)
}
newData[nk] = v
}
return newData
}

func (g *PandoraKeyConvert) Description() string {
//return "pandora_key_convert can convert data key name to valid pandora key"
return "将数据中的key名称中不合Pandora字段名规则的字符转为下划线, 如 a.b/c 改为 a_b_c"
Expand Down
17 changes: 0 additions & 17 deletions utils/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,3 @@ func (se *StatsError) ErrorIndexIn(idx int) bool {
}
return false
}

func PandoraKey(key string) string {
var nk string
for _, c := range key {
if c >= '0' && c <= '9' {
if len(nk) == 0 {
nk = "K"
}
nk = nk + string(c)
} else if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') {
nk = nk + string(c)
} else if len(nk) > 0 {
nk = nk + "_"
}
}
return nk
}
29 changes: 29 additions & 0 deletions utils/models/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,3 +751,32 @@ func GetMapList(data string) map[string]string {
}
return ret
}

func PandoraKey(key string) string {
var nk string
for _, c := range key {
if c >= '0' && c <= '9' {
if len(nk) == 0 {
nk = "K"
}
nk = nk + string(c)
} else if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') {
nk = nk + string(c)
} else if len(nk) > 0 {
nk = nk + "_"
}
}
return nk
}

func DeepConvertKey(data map[string]interface{}) map[string]interface{} {
newData := make(map[string]interface{})
for k, v := range data {
nk := PandoraKey(k)
if nv, ok := v.(map[string]interface{}); ok {
v = DeepConvertKey(nv)
}
newData[nk] = v
}
return newData
}
44 changes: 44 additions & 0 deletions utils/models/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,47 @@ func TestPickMapValue(t *testing.T) {
PickMapValue(m, pick, "multi", "otherword")
assert.NotEqual(t, exp, pick)
}

func TestPandoraKey(t *testing.T) {
testKeys := []string{"@timestamp", ".dot", "percent%100", "^^^^^^^^^^", "timestamp"}
expectKeys := []string{"timestamp", "dot", "percent_100", "", "timestamp"}
for idx, key := range testKeys {
actual := PandoraKey(key)
assert.Equal(t, expectKeys[idx], actual)
}
}

func TestDeepConvertKey(t *testing.T) {
testDatas := []map[string]interface{}{
{
"@timestamp": "2018-07-18T10:17:36.549054846+08:00",
//"timestamp": "2018-07-19T10:17:36.549054846+08:00",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个注释的用意什么

},
{
".dot": "dot",
},
{
"dot": "dot",
"percent%100": 100,
"^^^^^^^^^^": "mytest",
},
}
expectDatas := []map[string]interface{}{
{
"timestamp": "2018-07-18T10:17:36.549054846+08:00",
},
{
"dot": "dot",
},
{
"dot": "dot",
"percent_100": 100,
"": "mytest",
},
}

for idx, data := range testDatas {
actual := DeepConvertKey(data)
assert.Equal(t, expectDatas[idx], actual)
}
}