Skip to content

Commit

Permalink
added regex module to starlark init modules
Browse files Browse the repository at this point in the history
  • Loading branch information
daidokoro committed Oct 7, 2023
1 parent e427149 commit b3cc3d0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
14 changes: 12 additions & 2 deletions processor/starlarktransform/internal/logs/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/qri-io/starlib/re"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/plog"
Expand Down Expand Up @@ -40,11 +41,20 @@ type Processor struct {

func (p *Processor) Start(context.Context, component.Host) error {

global := starlark.StringDict{
modules := starlark.StringDict{
"json": jsonlib.Module,
}

globals, err := starlark.ExecFile(p.thread, "", p.code, global)
regexMod, err := re.LoadModule()
if err != nil {
return err
}

for k, v := range regexMod {
modules[k] = v
}

globals, err := starlark.ExecFile(p.thread, "", p.code, modules)
if err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions processor/starlarktransform/internal/logs/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,32 @@ func TestLogConsumer(t *testing.T) {
expectError: nil,
expectStartError: errors.New("starlark: no 'transform' function defined in script"),
},
{
name: "regex transform",
event: testlogevent,
code: heredoc.Doc(`
def transform(event):
event = json.decode(event)
val = event['resourceLogs'][0]['scopeLogs'][0]['logRecords'][0]['body']['stringValue']
val = re.sub('{.*}', 'NONE', val)
start, end = re.search('[A-Z]{4}', val)
# update log body
event['resourceLogs'][0]['scopeLogs'][0]['logRecords'][0]['body']['stringValue'] = val
# add severity
event['resourceLogs'][0]['scopeLogs'][0]['logRecords'][0]['attributes'].append({
"key": "severity",
"value": {"stringValue": val[start:end]}
})
return event`),

next: &fakeLogConsumer{
t: t,
expected: `{"resourceLogs":[{"resource":{"attributes":[{"key":"log.file.name","value":{"stringValue":"test.log"}}]},"scopeLogs":[{"scope":{},"logRecords":[{"observedTimeUnixNano":"1694127596456358000","body":{"stringValue":"2023-09-06T01:09:24.045+0200 INFO internal/command.go:117 OpenTelemetry Collector Builder NONE"},"attributes":[{"key":"app","value":{"stringValue":"dev"}},{"key":"severity","value":{"stringValue":"INFO"}}],"traceId":"","spanId":""}]}]}]}`},
expectError: nil,
expectStartError: nil,
},
}

for _, tt := range testcases {
Expand Down
14 changes: 12 additions & 2 deletions processor/starlarktransform/internal/metrics/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/qri-io/starlib/re"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pmetric"
Expand Down Expand Up @@ -40,11 +41,20 @@ type Processor struct {

func (p *Processor) Start(context.Context, component.Host) error {

global := starlark.StringDict{
modules := starlark.StringDict{
"json": jsonlib.Module,
}

globals, err := starlark.ExecFile(p.thread, "", p.code, global)
regexMod, err := re.LoadModule()
if err != nil {
return err
}

for k, v := range regexMod {
modules[k] = v
}

globals, err := starlark.ExecFile(p.thread, "", p.code, modules)
if err != nil {
return err
}
Expand Down
14 changes: 12 additions & 2 deletions processor/starlarktransform/internal/traces/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/qri-io/starlib/re"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/ptrace"
Expand Down Expand Up @@ -40,11 +41,20 @@ type Processor struct {

func (p *Processor) Start(context.Context, component.Host) error {

global := starlark.StringDict{
modules := starlark.StringDict{
"json": jsonlib.Module,
}

globals, err := starlark.ExecFile(p.thread, "", p.code, global)
regexMod, err := re.LoadModule()
if err != nil {
return err
}

for k, v := range regexMod {
modules[k] = v
}

globals, err := starlark.ExecFile(p.thread, "", p.code, modules)
if err != nil {
return err
}
Expand Down

0 comments on commit b3cc3d0

Please sign in to comment.