Skip to content

Commit

Permalink
fix(plc4go): Made the Tracer synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdutz authored and sruehl committed Jun 14, 2023
1 parent 22dbc15 commit 9cc2445
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions plc4go/pkg/api/cache/PlcConnectionCache_test.go
Expand Up @@ -456,6 +456,8 @@ func TestPlcConnectionCache_ReturningConnectionWithPingError(t *testing.T) {
t.Errorf("Expected %d connections in the cache but got %d", 0, len(cache.connections))
}

// In the connection string, we tell the driver to return an error with
// the given message on executing a ping operation.
connectionResultChan := cache.GetConnection("simulated://1.2.3.4:42?pingError=hurz&traceEnabled=true")
select {
case connectResult := <-connectionResultChan:
Expand Down Expand Up @@ -484,6 +486,8 @@ func TestPlcConnectionCache_ReturningConnectionWithPingError(t *testing.T) {
if traces[3].Operation+"-"+traces[3].Message != "ping-error: hurz" {
t.Errorf("Expected '%s' as fourth trace message, but got '%s'", "ping-error: hurz", traces[3])
}
} else {
t.Errorf("Expected a result, but got nil")
}
}
case <-time.After(20 * time.Second):
Expand Down
15 changes: 14 additions & 1 deletion plc4go/spi/tracer/Tracer.go
Expand Up @@ -21,6 +21,7 @@ package tracer

import (
"fmt"
"sync"
"time"

"github.com/apache/plc4x/plc4go/spi/options"
Expand Down Expand Up @@ -64,6 +65,7 @@ func NewTracer(connectionId string, _options ...options.WithOption) Tracer {
type tracer struct {
connectionId string
traceEntries []TraceEntry
m sync.Mutex

log zerolog.Logger
}
Expand All @@ -77,24 +79,32 @@ func (t *tracer) SetConnectionId(connectionId string) {
}

func (t *tracer) ResetTraces() {
t.m.Lock()
t.traceEntries = []TraceEntry{}
t.m.Unlock()
}

func (t *tracer) GetTraces() []TraceEntry {
return t.traceEntries
t.m.Lock()
entries := t.traceEntries
t.m.Unlock()
return entries
}

func (t *tracer) AddTrace(operation string, message string) {
t.m.Lock()
t.traceEntries = append(t.traceEntries, TraceEntry{
Timestamp: time.Now(),
ConnectionId: t.connectionId,
TransactionId: "",
Operation: operation,
Message: message,
})
t.m.Unlock()
}

func (t *tracer) AddTransactionalStartTrace(operation string, message string) string {
t.m.Lock()
transactionId := utils.GenerateId(t.log, 4)
t.traceEntries = append(t.traceEntries, TraceEntry{
Timestamp: time.Now(),
Expand All @@ -103,17 +113,20 @@ func (t *tracer) AddTransactionalStartTrace(operation string, message string) st
Operation: operation,
Message: message,
})
t.m.Unlock()
return transactionId
}

func (t *tracer) AddTransactionalTrace(transactionId string, operation string, message string) {
t.m.Lock()
t.traceEntries = append(t.traceEntries, TraceEntry{
Timestamp: time.Now(),
ConnectionId: t.connectionId,
TransactionId: transactionId,
Operation: operation,
Message: message,
})
t.m.Unlock()
}

func (t *tracer) FilterTraces(traces []TraceEntry, connectionIdFilter string, transactionIdFilter string, operationFilter string, messageFilter string) []TraceEntry {
Expand Down

0 comments on commit 9cc2445

Please sign in to comment.