From a998f2dcec309392625405d9e8e690da468855bb Mon Sep 17 00:00:00 2001 From: Gustavo Caso Date: Wed, 28 Feb 2024 11:35:25 +0100 Subject: [PATCH] Fix logs agent status output (#23217) Fix logs agent status output (cherry picked from commit ef93081e026043c3c0a245a8fcd29c57463f66d9) --- comp/logs/agent/agent_test.go | 62 +++++++- comp/logs/agent/status.go | 5 +- .../agent/status_templates/logsagent.tmpl | 2 +- .../agent/status_templates/logsagentHTML.tmpl | 145 +++++++++--------- 4 files changed, 132 insertions(+), 82 deletions(-) diff --git a/comp/logs/agent/agent_test.go b/comp/logs/agent/agent_test.go index 72df981e78c08..441ead529dea9 100644 --- a/comp/logs/agent/agent_test.go +++ b/comp/logs/agent/agent_test.go @@ -12,6 +12,7 @@ import ( "context" "fmt" "os" + "strings" "testing" "time" @@ -36,6 +37,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/logs/metrics" "github.com/DataDog/datadog-agent/pkg/logs/service" "github.com/DataDog/datadog-agent/pkg/logs/sources" + logsStatus "github.com/DataDog/datadog-agent/pkg/logs/status" "github.com/DataDog/datadog-agent/pkg/logs/tailers" "github.com/DataDog/datadog-agent/pkg/util/fxutil" "github.com/DataDog/datadog-agent/pkg/util/testutil" @@ -247,6 +249,34 @@ func (suite *AgentTestSuite) TestStatusProvider() { } func (suite *AgentTestSuite) TestStatusOut() { + originalProvider := logsProvider + + mockResult := logsStatus.Status{ + IsRunning: true, + Endpoints: []string{"foo", "bar"}, + StatusMetrics: map[string]int64{ + "hello": 12, + "world": 13, + }, + ProcessFileStats: map[string]uint64{ + "CoreAgentProcessOpenFiles": 27, + "OSFileLimit": 1048576, + }, + Integrations: []logsStatus.Integration{}, + Tailers: []logsStatus.Tailer{}, + Errors: []string{}, + Warnings: []string{}, + UseHTTP: true, + } + + logsProvider = func(verbose bool) logsStatus.Status { + return mockResult + } + + defer func() { + logsProvider = originalProvider + }() + suite.configOverrides["logs_enabled"] = true deps := fxutil.Test[dependencies](suite.T(), fx.Options( @@ -267,15 +297,26 @@ func (suite *AgentTestSuite) TestStatusOut() { stats := make(map[string]interface{}) headerProvider.JSON(false, stats) - assert.NotEmpty(t, stats) + assert.Equal(t, mockResult, stats["logsStats"]) }}, {"Text", func(t *testing.T) { b := new(bytes.Buffer) err := headerProvider.Text(false, b) assert.NoError(t, err) - - assert.NotEmpty(t, b.String()) + result := ` + foo + bar + hello: 12 + world: 13 + CoreAgentProcessOpenFiles: 27 + OSFileLimit: 1048576 +` + // We replace windows line break by linux so the tests pass on every OS + expectedResult := strings.Replace(result, "\r\n", "\n", -1) + output := strings.Replace(b.String(), "\r\n", "\n", -1) + + assert.Equal(t, expectedResult, output) }}, {"HTML", func(t *testing.T) { b := new(bytes.Buffer) @@ -283,7 +324,20 @@ func (suite *AgentTestSuite) TestStatusOut() { assert.NoError(t, err) - assert.NotEmpty(t, b.String()) + result := `
+ Logs Agent + + foo
+ bar
+ hello: 12
+ world: 13
+
+` + // We replace windows line break by linux so the tests pass on every OS + expectedResult := strings.Replace(result, "\r\n", "\n", -1) + output := strings.Replace(b.String(), "\r\n", "\n", -1) + + assert.Equal(t, expectedResult, output) }}, } diff --git a/comp/logs/agent/status.go b/comp/logs/agent/status.go index 5e2228de1a89d..6ee301155b530 100644 --- a/comp/logs/agent/status.go +++ b/comp/logs/agent/status.go @@ -17,6 +17,9 @@ import ( //go:embed status_templates var templatesFS embed.FS +// Only use for testing +var logsProvider = logsStatus.Get + type statusProvider struct{} func (p statusProvider) Name() string { @@ -36,7 +39,7 @@ func (p statusProvider) getStatusInfo(verbose bool) map[string]interface{} { } func (p statusProvider) populateStatus(verbose bool, stats map[string]interface{}) { - stats["logsStats"] = logsStatus.Get(verbose) + stats["logsStats"] = logsProvider(verbose) } func (p statusProvider) JSON(verbose bool, stats map[string]interface{}) error { diff --git a/comp/logs/agent/status_templates/logsagent.tmpl b/comp/logs/agent/status_templates/logsagent.tmpl index da436bdeb3447..2da805ecc9d64 100644 --- a/comp/logs/agent/status_templates/logsagent.tmpl +++ b/comp/logs/agent/status_templates/logsagent.tmpl @@ -24,7 +24,7 @@ {{- if .ProcessFileStats }} {{- range $metric_name, $metric_value := .ProcessFileStats }} - {{$metric_name}}: {{printf "%.0f" $metric_value}} + {{$metric_name}}: {{$metric_value}} {{- end}} {{- end}} diff --git a/comp/logs/agent/status_templates/logsagentHTML.tmpl b/comp/logs/agent/status_templates/logsagentHTML.tmpl index 417d8538d096c..5008f55232a13 100644 --- a/comp/logs/agent/status_templates/logsagentHTML.tmpl +++ b/comp/logs/agent/status_templates/logsagentHTML.tmpl @@ -1,85 +1,78 @@
- Logs Agent - - {{- with .logsStats -}} - {{- if eq .IsRunning false }} - - Logs Agent is not running
+ Logs Agent + + {{- with .logsStats -}} + {{- if eq .IsRunning false }} + Logs Agent is not running
+ {{- end }} + {{- if .Endpoints }} + {{- range $endpoint := .Endpoints }} + {{ $endpoint }}
{{- end }} - {{- if .Endpoints }} - - {{- range $endpoint := .Endpoints }} - {{ $endpoint }}
- {{- end }} + {{- end }} + {{- if and (eq .UseHTTP false) (.IsRunning) }} + You are currently sending Logs to Datadog through TCP (either because logs_config.force_use_tcp or logs_config.socks5_proxy_address is set or the HTTP connectivity test has failed). To benefit from increased reliability and better network performances, we strongly encourage switching over to compressed HTTPS which is now the default protocol.
+ {{- end }} + {{- if .StatusMetrics }} + {{- range $metric_name, $metric_value := .StatusMetrics }} + {{$metric_name}}: {{$metric_value}}
{{- end }} - {{- if and (eq .UseHTTP false) (.IsRunning) }} - - You are currently sending Logs to Datadog through TCP (either because logs_config.force_use_tcp or logs_config.socks5_proxy_address is set or the HTTP connectivity test has failed). To benefit from increased reliability and better network performances, we strongly encourage switching over to compressed HTTPS which is now the default protocol.
+ {{- end }} + {{- if .Errors }} + Errors + + {{- range $error := .Errors }} + {{ $error }}
{{- end }} - {{- if .StatusMetrics }} - - {{- range $metric_name, $metric_value := .StatusMetrics }} - {{$metric_name}}: {{$metric_value}}
- {{- end }} +
+ {{- end}} + {{- if .Warnings }} + Warnings + + {{- range $warning := .Warnings }} + {{ $warning }}
{{- end }} - {{- if .Errors }} - - Errors - - {{- range $error := .Errors }} - {{ $error }}
- {{- end }} -
- {{- end}} - {{- if .Warnings }} - - Warnings - - {{- range $warning := .Warnings }} - {{ $warning }}
- {{- end }} -
- {{- end}} - {{- range .Integrations }} - - {{ .Name }} - - {{- range .Sources }} - - Type: {{ .Type }}
- {{- range $key, $value := .Configuration }} - {{$key}}: {{$value}}
- {{- end }} - {{- if .Status }} - Status: {{ .Status }}
- {{- range $message := .Messages }} - {{ $message }}
- {{- end }} +
+ {{- end}} + {{- range .Integrations }} + {{ .Name }} + + {{- range .Sources }} + + Type: {{ .Type }}
+ {{- range $key, $value := .Configuration }} + {{$key}}: {{$value}}
+ {{- end }} + {{- if .Status }} + Status: {{ .Status }}
+ {{- range $message := .Messages }} + {{ $message }}
+ {{- end }} + {{- end }} + {{- if .Inputs }} + Inputs: +
    + {{- range $input := .Inputs }} +
  • {{$input}} {{- end }} - {{- if .Inputs }} - Inputs: -
      - {{- range $input := .Inputs }} -
    • {{$input}} +
    + {{- end }} + {{- if .Info }} + {{- range $key, $value := .Info }} {{ $len := len $value }} {{ if eq $len 1 }} + {{$key}}: {{index $value 0}}
    {{ else }} + {{$key}}:
    + + {{- range $inf := $value }} + {{ $inf }}
    {{- end }} -
- {{- end }} - {{- if .Info }} - {{- range $key, $value := .Info }} {{ $len := len $value }} {{ if eq $len 1 }} - {{$key}}: {{index $value 0}}
{{ else }} - {{$key}}:
- - {{- range $inf := $value }} - {{ $inf }}
- {{- end }} -
- {{- end }} - {{- end }} - {{- end }}
{{- end }} -
- {{- end }} - {{- end -}} -
-
+ {{- end }} + {{- end }} + + {{- end }} + + {{- end }} + {{- end -}} + +