Skip to content

Commit

Permalink
[HA Agent] Log leader state change (DataDog#31734)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreYang authored Dec 4, 2024
1 parent 072227e commit 5973bf5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
11 changes: 9 additions & 2 deletions comp/haagent/impl/haagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@ func (h *haAgentImpl) IsLeader() bool {
func (h *haAgentImpl) SetLeader(leaderAgentHostname string) {
agentHostname, err := hostname.Get(context.TODO())
if err != nil {
h.log.Warnf("Error getting the hostname: %v", err)
h.log.Warnf("error getting the hostname: %v", err)
return
}
h.isLeader.Store(agentHostname == leaderAgentHostname)
newIsLeader := agentHostname == leaderAgentHostname
prevIsLeader := h.isLeader.Load()
if newIsLeader != prevIsLeader {
h.log.Infof("agent role switched from %s to %s", leaderStateToRole(prevIsLeader), leaderStateToRole(newIsLeader))
h.isLeader.Store(newIsLeader)
} else {
h.log.Debugf("agent role not changed (current role: %s)", leaderStateToRole(prevIsLeader))
}
}

// ShouldRunIntegration return true if the agent integrations should to run.
Expand Down
13 changes: 13 additions & 0 deletions comp/haagent/impl/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

package haagentimpl

func leaderStateToRole(isLeader bool) string {
if isLeader {
return "leader"
}
return "follower"
}
17 changes: 17 additions & 0 deletions comp/haagent/impl/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

package haagentimpl

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_leaderStatusToRole(t *testing.T) {
assert.Equal(t, "leader", leaderStateToRole(true))
assert.Equal(t, "follower", leaderStateToRole(false))
}

0 comments on commit 5973bf5

Please sign in to comment.