Skip to content

Commit

Permalink
Support CustomLogEvent parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
loafoe committed Jun 19, 2023
1 parent c296b58 commit 6fe2d4a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
34 changes: 27 additions & 7 deletions logger/storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"fmt"
"os"
"regexp"
"strings"
"time"

Expand All @@ -13,6 +14,10 @@ import (
"github.com/philips-software/go-hsdp-api/logging"
)

var (
CustomLogEventRegex = regexp.MustCompile(`^(?P<severity>[^\|\s]+)\s*\|\s*CustomLogEvent\s*\|\s*(?P<transaction_id>[^\|\s]*)\s*\|\s*(?P<trace_id>[^\|\s]*)\s*\|\s*(?P<span_id>[^\|\s]*)\s*\|\s*(?P<component_name>[^\|\s]*)\s*\|\s*(?P<logdata_message>.*)$`)
)

type Storer interface {
StoreResources(messages []logging.Resource, count int) (*logging.StoreResponse, error)
}
Expand Down Expand Up @@ -43,7 +48,7 @@ func Setup(p models.Payload, taskID string) (chan string, string, func(), error)
os.Stdout = old // Reset
return nil, marker, func() {}, err
}
err = startStorerWorker(r, storer, logging.Resource{
err = StartStorerWorker(r, storer, logging.Resource{
ResourceType: "LogEvent",
ApplicationInstance: taskID,
EventID: "1",
Expand All @@ -67,11 +72,13 @@ func Setup(p models.Payload, taskID string) (chan string, string, func(), error)
}, nil
}

func startStorerWorker(fd *os.File, client Storer, template logging.Resource, control chan string, marker string) error {
func StartStorerWorker(fd *os.File, client Storer, template logging.Resource, control chan string, marker string) error {
fdReader := bufio.NewReader(fd)

go func() {
_, _ = fmt.Fprintf(os.Stderr, "[siderite] logging worker started\n")
names := CustomLogEventRegex.SubexpNames()
md := map[string]string{}
for {
text, err := fdReader.ReadString('\n')
if err != nil {
Expand All @@ -83,15 +90,28 @@ func startStorerWorker(fd *os.File, client Storer, template logging.Resource, co
control <- marker // Notify task/function runner
return
}

// Prepare message
template.ID = uuid.New().String()
template.TransactionID = template.ID
template.LogData.Message = base64.StdEncoding.EncodeToString([]byte(text))
template.LogTime = time.Now().Format("2006-01-02T15:04:05.000Z07:00")
msg := template
msg.ID = uuid.New().String()
msg.TransactionID = template.ID
msg.LogData.Message = base64.StdEncoding.EncodeToString([]byte(text))
msg.LogTime = time.Now().Format("2006-01-02T15:04:05.000Z07:00")

// Check for CustomLogEvent
match := CustomLogEventRegex.FindStringSubmatch(text)
for i, n := range match {
md[names[i]] = n
}
if len(match) > 0 {
msg.Severity = md["severity"]
msg.TransactionID = md["transaction_id"]
msg.TraceID = md["trace_id"]
}

if text != "" {
resp, err := client.StoreResources([]logging.Resource{
template,
msg,
}, 1)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error storing: %v [%+v]\n", err, resp)
Expand Down
21 changes: 19 additions & 2 deletions logger/storer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package logger
package logger_test

import (
"fmt"
Expand All @@ -8,6 +8,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/philips-labs/siderite/logger"
"github.com/philips-software/go-hsdp-api/logging"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func TestToHSDP(t *testing.T) {
if !assert.Nil(t, err) {
return
}
err = startStorerWorker(r, ds, logging.Resource{
err = logger.StartStorerWorker(r, ds, logging.Resource{
ResourceType: "LogEvent",
ApplicationInstance: "foo",
EventID: "1",
Expand Down Expand Up @@ -72,3 +73,19 @@ func TestToHSDP(t *testing.T) {
quit <- true
assert.Equal(t, marker, data)
}

func TestCustomLogEvent(t *testing.T) {
testLog := `ERROR|CustomLogEvent|386a881c-de7a-4ba6-9acc-778e9897e997|a7629ac1d152517466d6ea17a499f3c4|4408ca86d9ce19d6|AuditClientResponseHandler|Error in persisting the audit message to audit repository`

names := logger.CustomLogEventRegex.SubexpNames()
md := map[string]string{}

match := logger.CustomLogEventRegex.FindStringSubmatch(testLog)
for i, n := range match {
md[names[i]] = n
}

assert.Equal(t, "ERROR", md["severity"])
assert.Equal(t, "4408ca86d9ce19d6", md["span_id"])
assert.Equal(t, "a7629ac1d152517466d6ea17a499f3c4", md["trace_id"])
}

0 comments on commit 6fe2d4a

Please sign in to comment.