Skip to content

Commit

Permalink
Implement C Sharp unhandled exceptions in join_template plugin (#762)
Browse files Browse the repository at this point in the history
* Add c sharp exceptions

* Add tests and benchmarks

---------

Co-authored-by: george pogosyan <[email protected]>
  • Loading branch information
goshansmails and george pogosyan authored Feb 26, 2025
1 parent 1e53729 commit 3d3fb9e
Show file tree
Hide file tree
Showing 20 changed files with 1,520 additions and 883 deletions.
3 changes: 2 additions & 1 deletion plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ pipelines:
Alias to "join" plugin with predefined `start` and `continue` parameters.

> ⚠ Parsing the whole event flow could be very CPU intensive because the plugin uses regular expressions.
> Consider `match_fields` parameter to process only particular events. Check out an example for details.
> Enable explicit checks without regular expressions (use `fast_check` flag) or
> consider `match_fields` parameter to process only particular events. Check out an example for details.

**Example of joining Go panics**:
```yaml
Expand Down
3 changes: 2 additions & 1 deletion plugin/action/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ pipelines:
Alias to "join" plugin with predefined `start` and `continue` parameters.

> ⚠ Parsing the whole event flow could be very CPU intensive because the plugin uses regular expressions.
> Consider `match_fields` parameter to process only particular events. Check out an example for details.
> Enable explicit checks without regular expressions (use `fast_check` flag) or
> consider `match_fields` parameter to process only particular events. Check out an example for details.

**Example of joining Go panics**:
```yaml
Expand Down
5 changes: 3 additions & 2 deletions plugin/action/join_template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Alias to "join" plugin with predefined `start` and `continue` parameters.

> ⚠ Parsing the whole event flow could be very CPU intensive because the plugin uses regular expressions.
> Consider `match_fields` parameter to process only particular events. Check out an example for details.
> Enable explicit checks without regular expressions (use `fast_check` flag) or
> consider `match_fields` parameter to process only particular events. Check out an example for details.
**Example of joining Go panics**:
```yaml
Expand Down Expand Up @@ -33,7 +34,7 @@ Max size of the resulted event. If it is set and the event exceeds the limit, th

**`template`** *`string`* *`required`*

The name of the template. Available templates: `go_panic`.
The name of the template. Available templates: `go_panic`, `cs_exception`.

<br>

Expand Down
46 changes: 46 additions & 0 deletions plugin/action/join_template/ascii/ascii.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ascii

func IsSpace(c byte) bool {
switch c {
case ' ', '\n', '\t':
return true
default:
return false
}
}

func IsHexDigit(c byte) bool {
return IsDigit(c) || ('a' <= c && c <= 'f')
}

func IsDigit(c byte) bool {
return '0' <= c && c <= '9'
}

func IsLetterOrUnderscoreOrDigit(c byte) bool {
return IsLetterOrUnderscore(c) || IsDigit(c)
}

func IsLetterOrUnderscore(c byte) bool {
return IsLetter(c) || c == '_'
}

func IsLetter(c byte) bool {
return IsLowerCaseLetter(c) || IsUpperCaseLetter(c)
}

func IsLowerCaseLetter(c byte) bool {
return 'a' <= c && c <= 'z'
}

func ToLower(c byte) byte {
if IsUpperCaseLetter(c) {
return c + 'a' - 'A'
}

return c
}

func IsUpperCaseLetter(c byte) bool {
return 'A' <= c && c <= 'Z'
}
23 changes: 23 additions & 0 deletions plugin/action/join_template/ascii/ascii_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ascii

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestToLower(t *testing.T) {
f := make([]byte, 1<<8)
for x := range f {
f[x] = byte(x)
}

f['A'] = 'a'
for x := 'B'; x <= 'Z'; x++ {
f[x] = f[x-1] + 1
}

for x := range f {
require.Equal(t, f[x], ToLower(byte(x)))
}
}
58 changes: 0 additions & 58 deletions plugin/action/join_template/common.go

This file was deleted.

45 changes: 10 additions & 35 deletions plugin/action/join_template/join_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/ozontech/file.d/logger"
"github.com/ozontech/file.d/pipeline"
"github.com/ozontech/file.d/plugin/action/join"
"github.com/ozontech/file.d/plugin/action/join_template/template"
)

/*{ introduction
Alias to "join" plugin with predefined `start` and `continue` parameters.
> ⚠ Parsing the whole event flow could be very CPU intensive because the plugin uses regular expressions.
> Consider `match_fields` parameter to process only particular events. Check out an example for details.
> Enable explicit checks without regular expressions (use `fast_check` flag) or
> consider `match_fields` parameter to process only particular events. Check out an example for details.
**Example of joining Go panics**:
```yaml
Expand All @@ -29,24 +31,6 @@ pipelines:
```
}*/

type joinTemplates map[string]struct {
startRePat string
continueRePat string

startCheckFunc func(string) bool
continueCheckFunc func(string) bool
}

var templates = joinTemplates{
"go_panic": {
startRePat: "/^(panic:)|(http: panic serving)|^(fatal error:)/",
continueRePat: "/(^\\s*$)|(goroutine [0-9]+ \\[)|(\\.go:[0-9]+)|(created by .*\\/?.*\\.)|(^\\[signal)|(panic.+[0-9]x[0-9,a-f]+)|(panic:)|([A-Za-z_]+[A-Za-z0-9_]*\\)?\\.[A-Za-z0-9_]+\\(.*\\))/",

startCheckFunc: goPanicStartCheck,
continueCheckFunc: goPanicContinueCheck,
},
}

type Plugin struct {
config *Config

Expand All @@ -69,7 +53,7 @@ type Config struct {

// > @3@4@5@6
// >
// > The name of the template. Available templates: `go_panic`.
// > The name of the template. Available templates: `go_panic`, `cs_exception`.
Template string `json:"template" required:"true"` // *

// > @3@4@5@6
Expand All @@ -93,30 +77,21 @@ func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.ActionPluginP
p.config = config.(*Config)

templateName := p.config.Template
template, ok := templates[templateName]
if !ok {
logger.Fatalf("join template \"%s\" not found", templateName)
}

startRe, err := cfg.CompileRegex(template.startRePat)
if err != nil {
logger.Fatalf("failed to compile regex for template \"%s\": %s", templateName, err.Error())
}
continueRe, err := cfg.CompileRegex(template.continueRePat)
curTemplate, err := template.InitTemplate(templateName)
if err != nil {
logger.Fatalf("failed to compile regex for template \"%s\": %s", templateName, err.Error())
logger.Fatalf("failed to init join template \"%s\": %s", templateName, err)
}

jConfig := &join.Config{
Field_: p.config.Field_,
MaxEventSize: p.config.MaxEventSize,
Start_: startRe,
Continue_: continueRe,
Start_: curTemplate.StartRe,
Continue_: curTemplate.ContinueRe,

FastCheck: p.config.FastCheck,

StartCheckFunc_: template.startCheckFunc,
ContinueCheckFunc_: template.continueCheckFunc,
StartCheckFunc_: curTemplate.StartCheck,
ContinueCheckFunc_: curTemplate.ContinueCheck,
}
p.jp = &join.Plugin{}
p.jp.Start(jConfig, params)
Expand Down
Loading

0 comments on commit 3d3fb9e

Please sign in to comment.