Skip to content

Commit

Permalink
[8.4](backport #1286) Fix: Windows Agent Left Unhealthy After Removin…
Browse files Browse the repository at this point in the history
…g Endpoint Integration (#1597)

* Fix: Windows Agent Left Unhealthy After Removing Endpoint Integration (#1286)

* Add stop timeout to the endpoint spec
* Service watcher
* Wire in the service watcher
* Remove waiting on the service stop since Endpoint should not be stopped

(cherry picked from commit a8ad2da)

# Conflicts:
#	internal/pkg/agent/program/supported.go

* Regen buildSpec

* Make linter happy

Co-authored-by: Aleksandr Maus <[email protected]>
  • Loading branch information
mergify[bot] and aleksmaus authored Oct 25, 2022
1 parent 35d105b commit cd109a5
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 19 deletions.
4 changes: 4 additions & 0 deletions changelog/fragments/1666611696-fix_service_stop_timeout.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: bug-fix
summary: "Fix: Windows Agent Left Unhealthy After Removing Endpoint Integration"
pr: 1286
issue: 1262
3 changes: 2 additions & 1 deletion internal/pkg/agent/operation/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ func (o *Operator) getMonitoringMetricbeatConfig(outputType string, output inter
if len(hosts) == 0 {
return nil, false
}
//nolint:prealloc // false positive
var modules []interface{}
fixedAgentName := strings.ReplaceAll(agentName, "-", "_")

Expand Down Expand Up @@ -668,7 +669,7 @@ func normalizeHTTPCopyRules(name string) []map[string]interface{} {
return fromToMap
}

for _, exportedMetric := range spec.ExprtedMetrics {
for _, exportedMetric := range spec.ExportedMetrics {
fromToMap = append(fromToMap, map[string]interface{}{
"from": fmt.Sprintf("http.agent.%s", exportedMetric),
"to": exportedMetric,
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/operation/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
func TestExportedMetrics(t *testing.T) {
programName := "testing"
expectedMetricsName := "metric_name"
program.SupportedMap[programName] = program.Spec{ExprtedMetrics: []string{expectedMetricsName}}
program.SupportedMap[programName] = program.Spec{ExportedMetrics: []string{expectedMetricsName}}

exportedMetrics := normalizeHTTPCopyRules(programName)

Expand Down
17 changes: 16 additions & 1 deletion internal/pkg/agent/program/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"time"

"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -40,11 +41,25 @@ type Spec struct {
When string `yaml:"when"`
Constraints string `yaml:"constraints"`
RestartOnOutputChange bool `yaml:"restart_on_output_change,omitempty"`
ExprtedMetrics []string `yaml:"exported_metrics,omitempty"`
ExportedMetrics []string `yaml:"exported_metrics,omitempty"`
Process *ProcessSettings `yaml:"process,omitempty"`
}

// ProcessSettings process specific settings
type ProcessSettings struct {
// Allows to override the agent stop timeout settings and specify a different stop timeout for Endpoint service
StopTimeout time.Duration `yaml:"stop_timeout"`
}

// Service info
type ServiceInfo struct {
Name string `yaml:"name"`
Label string `yaml:"label"`
}

// ReadSpecs reads all the specs that match the provided globbing path.
func ReadSpecs(path string) ([]Spec, error) {
//nolint:prealloc // false positive
var specs []Spec
files, err := filepath.Glob(path)
if err != nil {
Expand Down
46 changes: 45 additions & 1 deletion internal/pkg/agent/program/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
package program

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -143,7 +146,48 @@ func TestExport(t *testing.T) {
for _, spec := range Supported {
b, err := yaml.Marshal(spec)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(dir, strings.ToLower(spec.Name)+".yml"), b, 0666)
err = ioutil.WriteFile(filepath.Join(dir, strings.ToLower(spec.Name)+".yml"), b, 0600)
require.NoError(t, err)
}
}

func TestSerializationProcessSettings(t *testing.T) {
ymlTmpl := `name: "Foobar"
process:
stop_timeout: %v`

tests := []struct {
name string
tonum int
to time.Duration
}{
{"zero", 0, 0},
{"180ns", 180, 0},
{"180s", 0, 120 * time.Second},
{"3m", 0, 3 * time.Minute},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var (
yml string
wantTimeout time.Duration
)
if tc.to == 0 {
yml = fmt.Sprintf(ymlTmpl, tc.tonum)
wantTimeout = time.Duration(tc.tonum)
} else {
yml = fmt.Sprintf(ymlTmpl, tc.to)
wantTimeout = tc.to
}
var spec Spec
err := yaml.Unmarshal([]byte(yml), &spec)
if err != nil {
t.Fatal(err)
}
diff := cmp.Diff(wantTimeout, spec.Process.StopTimeout)
if diff != "" {
t.Fatal(diff)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/pkg/agent/program/supported.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cd109a5

Please sign in to comment.