Skip to content

Commit 98fc5bd

Browse files
knopers8teo
authored andcommitted
[OCTRL-661] [core] Add state enter timestamp to kafka plugin messages
1 parent 07df415 commit 98fc5bd

4 files changed

Lines changed: 274 additions & 242 deletions

File tree

core/environment/environment.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ func newEnvironment(userVars map[string]string) (env *Environment, err error) {
124124
)
125125
env.GlobalVars.Set("__fmq_cleanup_count", "0") // initialize to 0 the number of START transitions
126126

127+
// We start with STANDBY, which will not be preceded with enter_STANDBY, thus we set the value here.
128+
enterStateTimeMs := strconv.FormatInt(time.Now().UnixMilli(), 10)
129+
env.UserVars.Set("enter_state_time_ms", enterStateTimeMs)
130+
127131
env.Sm = fsm.NewFSM(
128132
"STANDBY",
129133
fsm.Events{
@@ -153,7 +157,7 @@ func newEnvironment(userVars map[string]string) (env *Environment, err error) {
153157
env.workflow.GetVars().Set("run_number", rnString)
154158
env.workflow.GetVars().Set("runNumber", rnString)
155159

156-
runStartTime := strconv.FormatInt(time.Now().UnixNano()/1000000, 10)
160+
runStartTime := strconv.FormatInt(time.Now().UnixMilli(), 10)
157161
env.workflow.SetRuntimeVar("run_start_time_ms", runStartTime)
158162
env.workflow.SetRuntimeVar("run_end_time_ms", "") // we delete previous EOR
159163

@@ -200,6 +204,9 @@ func newEnvironment(userVars map[string]string) (env *Environment, err error) {
200204
env.handlerFunc()(e)
201205
},
202206
"enter_state": func(e *fsm.Event) {
207+
enterStateTimeMs := strconv.FormatInt(time.Now().UnixMilli(), 10)
208+
env.workflow.SetRuntimeVar("enter_state_time_ms", enterStateTimeMs)
209+
203210
errHooks := env.handleHooks(env.Workflow(), fmt.Sprintf("enter_%s", e.Dst))
204211
if errHooks != nil {
205212
e.Cancel(errHooks)

core/integration/kafka/plugin.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ func (p *Plugin) newEnvStateObject(varStack map[string]string, call string) *kaf
195195
}
196196
}
197197

198+
enterStateTimeMsStr, ok := varStack["enter_state_time_ms"]
199+
if !ok {
200+
log.WithField("call", call).
201+
WithField("partition", envId).
202+
Error("cannot acquire enter_state_time_ms")
203+
return nil
204+
}
205+
enterStateTimeMs, err := strconv.ParseUint(enterStateTimeMsStr, 10, 64)
206+
if err != nil {
207+
log.WithError(err).
208+
WithField("call", call).
209+
WithField("partition", envId).
210+
Errorf("cannot convert enter_state_time_ms (%s) to an unsigned integer", enterStateTimeMsStr)
211+
return nil
212+
}
213+
198214
detectorsStr, ok := varStack["detectors"]
199215
if !ok {
200216
log.WithField("call", call).
@@ -211,11 +227,12 @@ func (p *Plugin) newEnvStateObject(varStack map[string]string, call string) *kaf
211227
}
212228

213229
return &kafkapb.EnvInfo{
214-
EnvironmentId: envId,
215-
RunNumber: runNumberOpt,
216-
RunType: runTypeOpt,
217-
State: state,
218-
Detectors: detectorsSlice,
230+
EnvironmentId: envId,
231+
RunNumber: runNumberOpt,
232+
RunType: runTypeOpt,
233+
State: state,
234+
Detectors: detectorsSlice,
235+
EnterStateTimestamp: enterStateTimeMs,
219236
}
220237
}
221238

0 commit comments

Comments
 (0)