Skip to content

Commit

Permalink
[Heartbeat] Normalize state location names (elastic#35416)
Browse files Browse the repository at this point in the history
We currently allow any chars in state location names, this normalizes
them to [A-Za-z0-9_-_] for easier handling.
  • Loading branch information
andrewvc committed May 10, 2023
1 parent 29fcd59 commit b0ce603
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion heartbeat/monitors/wrappers/monitorstate/monitorstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package monitorstate

import (
"fmt"
"regexp"
"time"

"github.com/elastic/beats/v7/heartbeat/monitors/stdfields"
Expand Down Expand Up @@ -169,10 +170,13 @@ func (s *State) copy() *State {
return &copied
}

var normalizeRunFromIDRegexp = regexp.MustCompile("[^A-Za-z0-9_-]")

func LoaderDBKey(sf stdfields.StdMonitorFields, at time.Time, ctr int) string {
rfid := "default"
if sf.RunFrom != nil {
rfid = sf.RunFrom.ID
rfid = normalizeRunFromIDRegexp.ReplaceAllString(sf.RunFrom.ID, "_")

}
return fmt.Sprintf("%s-%x-%x", rfid, at.UnixMilli(), ctr)
}
46 changes: 46 additions & 0 deletions heartbeat/monitors/wrappers/monitorstate/monitorstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
package monitorstate

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/heartbeat/config"
"github.com/elastic/beats/v7/heartbeat/monitors/stdfields"
)

Expand Down Expand Up @@ -94,3 +96,47 @@ func TestTransitionTo(t *testing.T) {
// Ensure No infinite storage of states
require.Nil(t, s.Ends.Ends)
}

func TestLoaderDBKey(t *testing.T) {
tests := []struct {
name string
runFromID string
at time.Time
ctr int
expected string
}{
{
"simple - no rfid",
"",
time.Unix(0, 0),
0,
"default-0-0",
},
{
"simple - other time / count",
"",
time.Unix(12345, 0),
98765,
fmt.Sprintf("default-%x-%x", 12345000, 98765),
},
{
"Service location, weird chars",
"Asia/Pacific - Japan",
time.Unix(0, 0),
0,
"Asia_Pacific_-_Japan-0-0",
},
}

for _, tt := range tests {
sf := stdfields.StdMonitorFields{}
if tt.runFromID != "" {
sf.RunFrom = &config.LocationWithID{
ID: tt.runFromID,
}
}

key := LoaderDBKey(sf, tt.at, tt.ctr)
require.Equal(t, tt.expected, key)
}
}

0 comments on commit b0ce603

Please sign in to comment.