Skip to content

Commit

Permalink
Fix logs agent status output (#23217)
Browse files Browse the repository at this point in the history
Fix logs agent status output

(cherry picked from commit ef93081)
  • Loading branch information
GustavoCaso authored and github-actions[bot] committed Feb 28, 2024
1 parent 0f2c9a3 commit a998f2d
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 82 deletions.
62 changes: 58 additions & 4 deletions comp/logs/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"

Expand All @@ -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"
Expand Down Expand Up @@ -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(
Expand All @@ -267,23 +297,47 @@ 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)
err := headerProvider.HTML(false, b)

assert.NoError(t, err)

assert.NotEmpty(t, b.String())
result := `<div class="stat">
<span class="stat_title">Logs Agent</span>
<span class="stat_data">
foo<br>
bar<br>
hello: 12<br>
world: 13<br></span>
</div>
`
// 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)
}},
}

Expand Down
5 changes: 4 additions & 1 deletion comp/logs/agent/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion comp/logs/agent/status_templates/logsagent.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

{{- if .ProcessFileStats }}
{{- range $metric_name, $metric_value := .ProcessFileStats }}
{{$metric_name}}: {{printf "%.0f" $metric_value}}
{{$metric_name}}: {{$metric_value}}
{{- end}}
{{- end}}

Expand Down
145 changes: 69 additions & 76 deletions comp/logs/agent/status_templates/logsagentHTML.tmpl
Original file line number Diff line number Diff line change
@@ -1,85 +1,78 @@
<div class="stat">
<span class="stat_title">Logs Agent</span>
<span class="stat_data">
{{- with .logsStats -}}
{{- if eq .IsRunning false }}

Logs Agent is not running </br>
<span class="stat_title">Logs Agent</span>
<span class="stat_data">
{{- with .logsStats -}}
{{- if eq .IsRunning false }}
Logs Agent is not running </br>
{{- end }}
{{- if .Endpoints }}
{{- range $endpoint := .Endpoints }}
{{ $endpoint }}<br>
{{- end }}
{{- if .Endpoints }}

{{- range $endpoint := .Endpoints }}
{{ $endpoint }}<br>
{{- 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.</br>
{{- end }}
{{- if .StatusMetrics }}
{{- range $metric_name, $metric_value := .StatusMetrics }}
{{$metric_name}}: {{$metric_value}}<br>
{{- 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.</br>
{{- end }}
{{- if .Errors }}
<span class="error stat_subtitle">Errors</span>
<span class="stat_subdata">
{{- range $error := .Errors }}
{{ $error }}</br>
{{- end }}
{{- if .StatusMetrics }}

{{- range $metric_name, $metric_value := .StatusMetrics }}
{{$metric_name}}: {{$metric_value}}<br>
{{- end }}
</span>
{{- end}}
{{- if .Warnings }}
<span class="warning stat_subtitle">Warnings</span>
<span class="stat_subdata">
{{- range $warning := .Warnings }}
{{ $warning }}</br>
{{- end }}
{{- if .Errors }}

<span class="error stat_subtitle">Errors</span>
<span class="stat_subdata">
{{- range $error := .Errors }}
{{ $error }}</br>
{{- end }}
</span>
{{- end}}
{{- if .Warnings }}

<span class="warning stat_subtitle">Warnings</span>
<span class="stat_subdata">
{{- range $warning := .Warnings }}
{{ $warning }}</br>
{{- end }}
</span>
{{- end}}
{{- range .Integrations }}

<span class="stat_subtitle">{{ .Name }}</span>
<span class="stat_subdata">
{{- range .Sources }}
<span class="stat_subdata">
Type: {{ .Type }}</br>
{{- range $key, $value := .Configuration }}
{{$key}}: {{$value}}</br>
{{- end }}
{{- if .Status }}
Status: {{ .Status }}</br>
{{- range $message := .Messages }}
{{ $message }}</br>
{{- end }}
</span>
{{- end}}
{{- range .Integrations }}
<span class="stat_subtitle">{{ .Name }}</span>
<span class="stat_subdata">
{{- range .Sources }}
<span class="stat_subdata">
Type: {{ .Type }}</br>
{{- range $key, $value := .Configuration }}
{{$key}}: {{$value}}</br>
{{- end }}
{{- if .Status }}
Status: {{ .Status }}</br>
{{- range $message := .Messages }}
{{ $message }}</br>
{{- end }}
{{- end }}
{{- if .Inputs }}
Inputs:
<ul>
{{- range $input := .Inputs }}
<li>{{$input}}
{{- end }}
{{- if .Inputs }}
Inputs:
<ul>
{{- range $input := .Inputs }}
<li>{{$input}}
</ul>
{{- end }}
{{- if .Info }}
{{- range $key, $value := .Info }} {{ $len := len $value }} {{ if eq $len 1 }}
{{$key}}: {{index $value 0}}</br> {{ else }}
{{$key}}:</br>
<span class="stat_subdata">
{{- range $inf := $value }}
{{ $inf }}</br>
{{- end }}
</ul>
{{- end }}
{{- if .Info }}
{{- range $key, $value := .Info }} {{ $len := len $value }} {{ if eq $len 1 }}
{{$key}}: {{index $value 0}}</br> {{ else }}
{{$key}}:</br>
<span class="stat_subdata">
{{- range $inf := $value }}
{{ $inf }}</br>
{{- end }}
</span>
{{- end }}
{{- end }}
{{- end }}
</span>
{{- end }}
</span>
{{- end }}
{{- end -}}
</span>
</div>
{{- end }}
{{- end }}
</span>
{{- end }}
</span>
{{- end }}
{{- end -}}
</span>
</div>

0 comments on commit a998f2d

Please sign in to comment.