-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pipeline function
pt_name
(#45)
- Loading branch information
Showing
10 changed files
with
237 additions
and
3 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
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
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
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
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,31 @@ | ||
package funcs | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/GuanceCloud/platypus/pkg/ast" | ||
"github.com/GuanceCloud/platypus/pkg/engine/runtime" | ||
"github.com/GuanceCloud/platypus/pkg/errchain" | ||
) | ||
|
||
func PtNameChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { | ||
if len(funcExpr.Param) > 1 { | ||
return runtime.NewRunError(ctx, fmt.Sprintf( | ||
"func `%s' can have at most one parameter", funcExpr.Name), funcExpr.NamePos) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func PtName(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { | ||
if len(funcExpr.Param) == 1 { | ||
if val, _, err := runtime.RunStmt(ctx, funcExpr.Param[0]); err == nil { | ||
if val, ok := val.(string); ok { | ||
_ = setPtName(ctx.InData(), val) | ||
} | ||
} | ||
} | ||
|
||
ctx.Regs.ReturnAppend(getPtName(ctx.InData()), ast.String) | ||
return nil | ||
} |
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,122 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the MIT License. | ||
// This product includes software developed at Guance Cloud (https://www.guance.com/). | ||
// Copyright 2021-present Guance, Inc. | ||
|
||
package funcs | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/GuanceCloud/cliutils/pipeline/ptinput" | ||
"github.com/GuanceCloud/cliutils/point" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPtName(t *testing.T) { | ||
defatultPtName := "default" | ||
|
||
cases := []struct { | ||
name, pl, in string | ||
out string | ||
expect string | ||
fail bool | ||
}{ | ||
{ | ||
name: "pt_name_set_and_get", | ||
in: `162.62.81.1 - - [29/Nov/2021:07:30:50 +0000] "123 /?signature=b8d8ea×tamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`, | ||
pl: ` | ||
set_tag(client_ip) | ||
grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA:data} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}") | ||
cast(data, "int") | ||
add_key("pt_name",pt_name(client_ip)) | ||
`, | ||
out: "pt_name", | ||
expect: "162.62.81.1", | ||
fail: false, | ||
}, | ||
{ | ||
name: "pt_name_set_str_and_get", | ||
in: `162.62.81.1 - - [29/Nov/2021:07:30:50 +0000] "123 /?signature=b8d8ea×tamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`, | ||
pl: ` | ||
set_tag(client_ip) | ||
grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA:data} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}") | ||
cast(data, "int") | ||
add_key("pt_name", pt_name("aa")) | ||
`, | ||
out: "pt_name", | ||
expect: "aa", | ||
fail: false, | ||
}, | ||
{ | ||
name: "pt_name_set_int_1_and_get", | ||
in: `162.62.81.1 - - [29/Nov/2021:07:30:50 +0000] "123 /?signature=b8d8ea×tamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`, | ||
pl: ` | ||
set_tag(client_ip) | ||
grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA:data} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}") | ||
cast(data, "int") | ||
add_key("pt_name",pt_name(1)) | ||
`, | ||
out: "pt_name", | ||
expect: defatultPtName, | ||
fail: false, | ||
}, | ||
{ | ||
name: "pt_name_set_int_var_and_get", | ||
in: `162.62.81.1 - - [29/Nov/2021:07:30:50 +0000] "123 /?signature=b8d8ea×tamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`, | ||
pl: ` | ||
set_tag(client_ip) | ||
grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA:data} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}") | ||
cast(data, "int") | ||
a=1 | ||
add_key("pt_name",pt_name(aa)) | ||
`, | ||
out: "pt_name", | ||
expect: defatultPtName, | ||
fail: false, | ||
}, | ||
{ | ||
name: "pt_name_get", | ||
in: `162.62.81.1 - - [29/Nov/2021:07:30:50 +0000] "123 /?signature=b8d8ea×tamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`, | ||
pl: ` | ||
grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA:data} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}") | ||
set_tag(client_ip) | ||
cast(data, "int") | ||
add_key("pt_name", pt_name()) | ||
`, | ||
out: "pt_name", | ||
expect: defatultPtName, | ||
fail: false, | ||
}, | ||
} | ||
|
||
for idx, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
runner, err := NewTestingRunner(tc.pl) | ||
if err != nil { | ||
if tc.fail { | ||
t.Logf("[%d]expect error: %s", idx, err) | ||
} else { | ||
t.Errorf("[%d] failed: %s", idx, err) | ||
} | ||
return | ||
} | ||
pt := ptinput.NewPlPoint( | ||
point.Logging, defatultPtName, nil, map[string]any{"message": tc.in}, time.Now()) | ||
errR := runScript(runner, pt) | ||
|
||
if errR != nil { | ||
t.Fatal(errR.Error()) | ||
} | ||
|
||
_, _, ok := pt.GetWithIsTag(tc.out) | ||
assert.True(t, ok) | ||
assert.Equal(t, tc.expect, pt.GetPtName()) | ||
t.Logf("[%d] PASS", idx) | ||
}) | ||
} | ||
} |
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
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,24 @@ | ||
### `pt_name()` {#fn-pt-name} | ||
|
||
Function prototype: `fn pt_name(name: str = "") -> str` | ||
|
||
Function description: Get the name of point; if the parameter is not empty, set the new name. | ||
|
||
Function parameters: | ||
|
||
- `name`: Value as point name; defaults to empty string. | ||
|
||
The field mapping relationship between point name and various types of data storage: | ||
|
||
| category | field name | | ||
| ------------- | ---------- | | ||
| custom_object | class | | ||
| keyevent | - | | ||
| logging | source | | ||
| metric | - | | ||
| network | source | | ||
| object | class | | ||
| profiling | source | | ||
| rum | source | | ||
| security | rule | | ||
| tracing | source | |
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,24 @@ | ||
### `pt_name()` {#fn-pt-name} | ||
|
||
函数原型:`fn pt_name(name: str = "") -> str` | ||
|
||
函数说明:获取 point 的 name;如果参数不为空则设置新的 name。 | ||
|
||
函数参数: | ||
|
||
- `name`: 值作为 point name;默认值为空字符串 | ||
|
||
Point Name 与各个类型数据存储时的字段映射关系: | ||
|
||
| 类别 | 字段名 | | ||
| ------------- | ------ | | ||
| custom_object | class | | ||
| keyevent | - | | ||
| logging | source | | ||
| metric | - | | ||
| network | source | | ||
| object | class | | ||
| profiling | source | | ||
| rum | source | | ||
| security | rule | | ||
| tracing | source | |
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