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

grok support #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.32.2 // indirect
github.com/aws/smithy-go v1.22.0 // indirect
github.com/beevik/etree v1.4.1 // indirect
github.com/elastic/go-grok v0.3.1 // indirect
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/in4it/go-devops-platform v0.1.1-0.20241015201149-20da0ccd1259 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/magefile/mage v1.15.0 // indirect
github.com/mattermost/xml-roundtrip-validator v0.1.0 // indirect
github.com/russellhaering/gosaml2 v0.9.1 // indirect
github.com/russellhaering/goxmldsig v1.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/beevik/etree v1.4.1 h1:PmQJDDYahBGNKDcpdX8uPy1xRCwoCGVUiW669MEirVI=
github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elastic/go-grok v0.3.1 h1:WEhUxe2KrwycMnlvMimJXvzRa7DoByJB4PVUIE1ZD/U=
github.com/elastic/go-grok v0.3.1/go.mod h1:n38ls8ZgOboZRgKcjMY8eFeZFMmcL9n2lP0iHhIDk64=
github.com/go-jose/go-jose/v4 v4.0.4 h1:VsjPI33J0SB9vQM6PLmNjoHqMQNGPiZ0rHL7Ni7Q6/E=
github.com/go-jose/go-jose/v4 v4.0.4/go.mod h1:NKb5HO1EZccyMpiZNbdUw/14tiXNyUJh188dfnMCAfc=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
Expand All @@ -59,6 +61,8 @@ github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NB
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/mattermost/xml-roundtrip-validator v0.1.0 h1:RXbVD2UAl7A7nOTR4u7E3ILa4IbtvKBHw64LDsmu9hU=
github.com/mattermost/xml-roundtrip-validator v0.1.0/go.mod h1:qccnGMcpgwcNaBnxqpJpWWUiPNr5H3O8eDgGV9gT5To=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down
41 changes: 41 additions & 0 deletions pkg/observability/grok.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package observability

import (
"encoding/json"
"fmt"

"github.com/elastic/go-grok"
"github.com/in4it/go-devops-platform/storage"
)

func compilePatternDefinition(patternDefinition PatternDefinition) (*grok.Grok, error) {
g := grok.New()
g.AddPatterns(patternDefinition.Patterns)
err := g.Compile("%{"+patternDefinition.Name+"}", true)
if err != nil {
return nil, fmt.Errorf("grok compile error: %s", err)
}
return g, nil
}

func getPatternDefinitions(storage storage.Iface) ([]PatternDefinition, error) {
patternDefinition := []PatternDefinition{}
logConfig, err := getLogConfig(storage)
if err != nil {
return patternDefinition, fmt.Errorf("getLogConfig error: %s", err)
}
return logConfig.PatternDefinitions, nil
}

func getLogConfig(storage storage.Iface) (LogConfig, error) {
var logConfig LogConfig
contents, err := storage.ReadFile("log-config.json")
if err != nil {
return logConfig, fmt.Errorf("can't read log-config.json: %s", err)
}
err = json.Unmarshal(contents, &logConfig)
if err != nil {
return logConfig, fmt.Errorf("json unmarshal error: %s", err)
}
return logConfig, err
}
30 changes: 30 additions & 0 deletions pkg/observability/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func (o *Observability) getLogs(fromDate, endDate time.Time, pos int64, maxLogLi
Tags: KeyValueInt{},
}

patternDefinitions, err := getPatternDefinitions(o.Storage)
if err != nil {
return logEntryResponse, fmt.Errorf("get pattern definitions error: %s", err)
}

keys := make(map[KeyValue]int)

logFiles := []string{}
Expand Down Expand Up @@ -78,6 +83,13 @@ func (o *Observability) getLogs(fromDate, endDate time.Time, pos int64, maxLogLi
}
}
if !filterMessage {
// check if we need to apply grok filter
for _, patternDefinition := range patternDefinitions {
if logEntryHasTags(patternDefinition.Tags, tags) { // apply grok filter

}
}
// add log entry
logEntry := LogEntry{
Timestamp: timestamp.Format(TIMESTAMP_FORMAT),
Data: logline,
Expand Down Expand Up @@ -114,3 +126,21 @@ func (o *Observability) getLogs(fromDate, endDate time.Time, pos int64, maxLogLi

return logEntryResponse, nil
}

func logEntryHasTags(a []KeyValue, b []KeyValue) bool {
tags := make([]bool, len(b))
for _, a1 := range a {
for k, b1 := range b {
if b1.Key == a1.Key && b1.Value == a1.Value {
tags[k] = true
break
}
}
}
for _, v := range tags {
if !v {
return false
}
}
return len(tags) > 0
}
79 changes: 79 additions & 0 deletions pkg/observability/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,82 @@ func TestKeyValue(t *testing.T) {
t.Fatalf("wrong output: %s", out)
}
}

func TestLogEntryHasTags1(t *testing.T) {
a := []KeyValue{
{
Key: "a",
Value: "b",
},
}
b := []KeyValue{
{
Key: "a",
Value: "b",
},
}
res := logEntryHasTags(a, b)
if !res {
t.Fatalf("res is false")
}
}

func TestLogEntryHasTags2(t *testing.T) {
a := []KeyValue{
{
Key: "a",
Value: "b",
},
}
b := []KeyValue{}
res := logEntryHasTags(a, b)
if res {
t.Fatalf("res is true")
}
}

func TestLogEntryHasTags3(t *testing.T) {
a := []KeyValue{
{
Key: "a",
Value: "b",
},
}
b := []KeyValue{
{
Key: "a",
Value: "c",
},
}
res := logEntryHasTags(a, b)
if res {
t.Fatalf("res is true")
}
}

func TestLogEntryHasTags4(t *testing.T) {
a := []KeyValue{
{
Key: "a",
Value: "b",
},
{
Key: "b",
Value: "c",
},
}
b := []KeyValue{
{
Key: "b",
Value: "c",
},
{
Key: "a",
Value: "b",
},
}
res := logEntryHasTags(a, b)
if !res {
t.Fatalf("res is false")
}
}
12 changes: 12 additions & 0 deletions pkg/observability/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync/atomic"
"time"

"github.com/elastic/go-grok"
"github.com/in4it/go-devops-platform/storage"
)

Expand All @@ -27,6 +28,7 @@ type Observability struct {
ActiveBufferWriters sync.WaitGroup
WriteLock sync.Mutex
MaxBufferSize int
GrokPatterns map[string]*grok.Grok
}

type ConcurrentRWBuffer struct {
Expand Down Expand Up @@ -87,3 +89,13 @@ func (kv KeyValueInt) Less(i, j int) bool {
func (kv KeyValueInt) Swap(i, j int) {
kv[i], kv[j] = kv[j], kv[i]
}

type LogConfig struct {
PatternDefinitions []PatternDefinition `json:"patternDefinitions"`
}

type PatternDefinition struct {
Name string `json:"name"`
Tags []KeyValue `json:"tags"`
Patterns map[string]string `json:"patterns"`
}