Skip to content

Commit

Permalink
feat(am-dbg): add tx and log filtering (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta committed Jul 10, 2024
1 parent 6bad1fa commit 5020f99
Show file tree
Hide file tree
Showing 14 changed files with 1,866 additions and 1,183 deletions.
2 changes: 1 addition & 1 deletion pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ func (m *Machine) log(level LogLevel, msg string, args ...any) {
if t != nil {
// append the log msg to the current transition
// TODO not thread safe
t.LogEntries = append(t.LogEntries, out)
t.LogEntries = append(t.LogEntries, &LogEntry{level, out})
} else {

// append the log msg the machine and collect at the end of the next
Expand Down
5 changes: 5 additions & 0 deletions pkg/machine/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ type Logger func(level LogLevel, msg string, args ...any)
// LogLevel enum
type LogLevel int

type LogEntry struct {
Level LogLevel
Text string
}

const (
// LogNothing means no logging, including external msgs.
LogNothing LogLevel = iota
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Transition struct {
// Parent machine
Machine *Machine
// Log entries produced during the transition
LogEntries []string
LogEntries []*LogEntry
// start time of the transition

// Latest / current step of the transition
Expand Down
33 changes: 16 additions & 17 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
am "github.com/pancsta/asyncmachine-go/pkg/machine"
)

//////////////////
///// AM-DBG
//////////////////
// ////////////////
// /// AM-DBG
// ////////////////

const DbgHost = "localhost:6831"

Expand All @@ -42,8 +42,6 @@ type DbgMsgStruct struct {
StatesIndex am.S
// all the states with relations
States am.Struct
// log level of the machine
LogLevel am.LogLevel
}

func (d *DbgMsgStruct) Clock(_ am.S, _ string) uint64 {
Expand All @@ -70,8 +68,7 @@ type DbgMsgTx struct {
// all the transition steps
Steps []*am.Step
// log entries created during the transition
// TODO include log levels
LogEntries []string
LogEntries []*am.LogEntry
// log entries before the transition, which happened after the prev one
PreLogEntries []string
// transition was triggered by an auto state
Expand Down Expand Up @@ -247,7 +244,6 @@ func sendStructMsg(mach *am.Machine, client *dbgClient) error {
ID: mach.ID,
StatesIndex: mach.StateNames,
States: mach.GetStruct(),
LogLevel: mach.GetLogLevel(),
}

// TODO retries
Expand All @@ -264,11 +260,14 @@ func removeLogPrefix(msg *DbgMsgTx) {
addChars := 3 // "[] "
prefixLen := min(len(msg.MachineID)+addChars, maxIDlen+addChars)

for i := range msg.LogEntries {
if len(msg.LogEntries[i]) < prefixLen {
for i, le := range msg.LogEntries {
if len(msg.LogEntries[i].Text) < prefixLen {
continue
}
msg.LogEntries[i] = msg.LogEntries[i][prefixLen:]
msg.LogEntries[i] = &am.LogEntry{
Level: le.Level,
Text: le.Text[prefixLen:],
}
}

for i := range msg.PreLogEntries {
Expand All @@ -279,9 +278,9 @@ func removeLogPrefix(msg *DbgMsgTx) {
}
}

//////////////////
///// OPEN TELEMETRY
//////////////////
// ////////////////
// /// OPEN TELEMETRY
// ////////////////

// OtelMachTracer implements machine.Tracer for OpenTelemetry.
// Support tracing multiple machines
Expand Down Expand Up @@ -683,9 +682,9 @@ func (ot *OtelMachTracer) Inheritable() bool {
return true
}

//////////////////
///// UTILS
//////////////////
// ////////////////
// /// UTILS
// ////////////////

// j joins state names
func j(states []string) string {
Expand Down
10 changes: 6 additions & 4 deletions tools/cmd/am-dbg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pancsta/asyncmachine-go/pkg/telemetry"
"github.com/pancsta/asyncmachine-go/tools/debugger"
ss "github.com/pancsta/asyncmachine-go/tools/debugger/states"
"github.com/pancsta/asyncmachine-go/tools/debugger/server"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -155,6 +156,7 @@ func cliRun(cmd *cobra.Command, _ []string) {
CleanOnConnect: cleanOnConnect,
SelectConnected: selectConnected,
Clients: make(map[string]*debugger.Client),
LogLevel: am.LogChanges,
}
gob.Register(debugger.Exportable{})
gob.Register(am.Relation(0))
Expand All @@ -180,7 +182,7 @@ func cliRun(cmd *cobra.Command, _ []string) {
}

// rpc server
go debugger.StartRCP(dbg.Mach, serverURL)
go server.StartRCP(dbg.Mach, serverURL)

// start and wait till the end
dbg.Mach.Add1(ss.Start, am.A{
Expand All @@ -189,14 +191,14 @@ func cliRun(cmd *cobra.Command, _ []string) {
"dbgView": startupView,
})
<-dbg.Mach.WhenNot1(ss.Start, nil)
txes := 0
txs := 0
for _, c := range dbg.Clients {
txes += len(c.MsgTxs)
txs += len(c.MsgTxs)
}

// TODO format numbers
_, _ = dbg.P.Printf("Clients: %d\n", len(dbg.Clients))
_, _ = dbg.P.Printf("Transitions: %d\n", txes)
_, _ = dbg.P.Printf("Transitions: %d\n", txs)
}

// TODO extract to tools/debugger
Expand Down
Loading

0 comments on commit 5020f99

Please sign in to comment.