Skip to content

Commit bd6b5dc

Browse files
committed
timestamps and sequence numbers for monitor data and lifecycle events
Signed-off-by: Kyle Quest <kcq.public@gmail.com>
1 parent 608d006 commit bd6b5dc

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

pkg/app/sensor/execution/hook.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"context"
55
"fmt"
66
"os/exec"
7+
"time"
78

89
log "github.com/sirupsen/logrus"
10+
11+
"github.com/slimtoolkit/slim/pkg/acounter"
912
"github.com/slimtoolkit/slim/pkg/util/errutil"
1013
)
1114

@@ -20,10 +23,19 @@ const (
2023
monitorFailed kind = "monitor-failed"
2124
)
2225

26+
// todo:
27+
// add 'kind' to the lifecycle event and
28+
// pass the whole event to the lifecycle hook command
29+
type LifecycleEvent struct {
30+
Timestamp int64 `json:"ts"`
31+
SeqNumber uint64 `json:"sn"`
32+
}
33+
2334
type hookExecutor struct {
24-
ctx context.Context
25-
cmd string
26-
lastHook kind
35+
ctx context.Context
36+
cmd string
37+
lastHook kind
38+
seqNumber acounter.Type
2739
}
2840

2941
func (h *hookExecutor) State() string {
@@ -59,11 +71,19 @@ func (h *hookExecutor) doHook(k kind) {
5971
return
6072
}
6173

74+
event := LifecycleEvent{
75+
Timestamp: time.Now().UTC().UnixNano(),
76+
SeqNumber: h.seqNumber.Inc(),
77+
}
78+
6279
h.lastHook = k
80+
//todo: pass event as a base64 encoded json string as an extra param to 'cmd'
6381
cmd := exec.CommandContext(h.ctx, h.cmd, string(k))
6482
out, err := cmd.CombinedOutput()
6583

6684
logger := log.
85+
WithField("kind", string(k)).
86+
WithField("event", fmt.Sprintf("%+v", event)).
6787
WithField("command", h.cmd).
6888
WithField("exit_code", cmd.ProcessState.ExitCode()).
6989
WithField("output", string(out))

pkg/appbom/appbom.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,5 @@ func Get() *Info {
387387

388388
return info
389389
}
390+
391+
//TODO: have an option to add package metadata (from deps.dev and osv.dev)

pkg/mondel/mondel.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"errors"
1010
"fmt"
1111
"os"
12+
"time"
1213

1314
log "github.com/sirupsen/logrus"
1415

16+
"github.com/slimtoolkit/slim/pkg/acounter"
1517
"github.com/slimtoolkit/slim/pkg/report"
1618
)
1719

@@ -31,6 +33,7 @@ type publisher struct {
3133
outputFile string
3234
output *os.File
3335
eventCh chan *report.MonitorDataEvent
36+
seqNumber acounter.Type
3437
}
3538

3639
func NewPublisher(ctx context.Context, enable bool, outputFile string) *publisher {
@@ -66,10 +69,13 @@ func NewPublisher(ctx context.Context, enable bool, outputFile string) *publishe
6669
}
6770

6871
func (ref *publisher) Publish(event *report.MonitorDataEvent) error {
69-
if !ref.enable {
72+
if !ref.enable || event == nil {
7073
return nil
7174
}
7275

76+
event.Timestamp = time.Now().UTC().UnixNano()
77+
event.SeqNumber = ref.seqNumber.Inc()
78+
7379
select {
7480
case ref.eventCh <- event:
7581
return nil

pkg/report/mondel_report.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ const (
2626
)
2727

2828
type MonitorDataEvent struct {
29+
Timestamp int64 `json:"ts"`
30+
SeqNumber uint64 `json:"sn"`
2931
Source string `json:"s"`
3032
Type string `json:"t"`
3133
Pid int32 `json:"p,omitempty"`
3234
ParentPid int32 `json:"pp,omitempty"`
33-
Artifact string `json:"a,omitempty"` //used for exe path for process events
34-
OpType string `json:"o,omitempty"` //operation type
35-
Op string `json:"op,omitempty"` //operation
35+
Artifact string `json:"a,omitempty"` // used for exe path for process events
36+
OpType string `json:"o,omitempty"` // operation type
37+
Op string `json:"op,omitempty"` // operation
3638
OpNum uint32 `json:"n,omitempty"`
3739
WorkDir string `json:"w,omitempty"`
3840
Root string `json:"r,omitempty"`
3941
Cmd string `json:"c,omitempty"`
42+
State string `json:"st,omitempty"`
4043
}

0 commit comments

Comments
 (0)