From 3f057205b1acbefb1fa9dbd9369875271fec827b Mon Sep 17 00:00:00 2001 From: Gustavo Caso Date: Mon, 20 Nov 2023 18:00:30 +0100 Subject: [PATCH] [ASCII-145] Extract render subpackge from status (#20776) move format functions and templates to status `pkg/status/render` sub-package --- cmd/agent/gui/render.go | 4 +- cmd/agent/subcommands/status/command.go | 4 +- .../subcommands/metamap/command.go | 4 +- .../subcommands/status/command.go | 4 +- .../subcommands/status/status.go | 6 +- .../subcommands/status/status_test.go | 4 +- .../subcommands/status/command.go | 4 +- pkg/cli/subcommands/check/command.go | 28 ++++---- pkg/flare/archive_dca.go | 5 +- .../{ => render}/fixtures/agent_status.json | 0 pkg/status/{ => render}/helpers.go | 4 +- pkg/status/{ => render}/helpers_test.go | 2 +- pkg/status/{ => render}/render.go | 70 ++++++++++--------- pkg/status/{ => render}/render_test.go | 6 +- .../{ => render}/templates/aggregator.tmpl | 0 .../{ => render}/templates/autodiscovery.tmpl | 0 .../{ => render}/templates/clusteragent.tmpl | 0 .../{ => render}/templates/collector.tmpl | 0 .../{ => render}/templates/compliance.tmpl | 0 .../templates/custommetricsprovider.tmpl | 0 .../{ => render}/templates/dogstatsd.tmpl | 0 .../{ => render}/templates/endpoints.tmpl | 0 .../{ => render}/templates/forwarder.tmpl | 0 pkg/status/{ => render}/templates/header.tmpl | 0 .../{ => render}/templates/jmxfetch.tmpl | 0 .../{ => render}/templates/logsagent.tmpl | 0 .../templates/metadatamapper.tmpl | 0 .../{ => render}/templates/netflow.tmpl | 0 .../{ => render}/templates/orchestrator.tmpl | 0 pkg/status/{ => render}/templates/otlp.tmpl | 0 .../{ => render}/templates/process-agent.tmpl | 0 .../{ => render}/templates/remoteconfig.tmpl | 0 .../{ => render}/templates/rendererrors.tmpl | 0 .../templates/runtimesecurity.tmpl | 0 .../{ => render}/templates/snmp-traps.tmpl | 0 .../{ => render}/templates/systemprobe.tmpl | 0 .../{ => render}/templates/trace-agent.tmpl | 0 pkg/status/status.go | 43 ++---------- 38 files changed, 79 insertions(+), 109 deletions(-) rename pkg/status/{ => render}/fixtures/agent_status.json (100%) rename pkg/status/{ => render}/helpers.go (99%) rename pkg/status/{ => render}/helpers_test.go (97%) rename pkg/status/{ => render}/render.go (80%) rename pkg/status/{ => render}/render_test.go (86%) rename pkg/status/{ => render}/templates/aggregator.tmpl (100%) rename pkg/status/{ => render}/templates/autodiscovery.tmpl (100%) rename pkg/status/{ => render}/templates/clusteragent.tmpl (100%) rename pkg/status/{ => render}/templates/collector.tmpl (100%) rename pkg/status/{ => render}/templates/compliance.tmpl (100%) rename pkg/status/{ => render}/templates/custommetricsprovider.tmpl (100%) rename pkg/status/{ => render}/templates/dogstatsd.tmpl (100%) rename pkg/status/{ => render}/templates/endpoints.tmpl (100%) rename pkg/status/{ => render}/templates/forwarder.tmpl (100%) rename pkg/status/{ => render}/templates/header.tmpl (100%) rename pkg/status/{ => render}/templates/jmxfetch.tmpl (100%) rename pkg/status/{ => render}/templates/logsagent.tmpl (100%) rename pkg/status/{ => render}/templates/metadatamapper.tmpl (100%) rename pkg/status/{ => render}/templates/netflow.tmpl (100%) rename pkg/status/{ => render}/templates/orchestrator.tmpl (100%) rename pkg/status/{ => render}/templates/otlp.tmpl (100%) rename pkg/status/{ => render}/templates/process-agent.tmpl (100%) rename pkg/status/{ => render}/templates/remoteconfig.tmpl (100%) rename pkg/status/{ => render}/templates/rendererrors.tmpl (100%) rename pkg/status/{ => render}/templates/runtimesecurity.tmpl (100%) rename pkg/status/{ => render}/templates/snmp-traps.tmpl (100%) rename pkg/status/{ => render}/templates/systemprobe.tmpl (100%) rename pkg/status/{ => render}/templates/trace-agent.tmpl (100%) diff --git a/cmd/agent/gui/render.go b/cmd/agent/gui/render.go index 490b64030f609..76a7e0184981c 100644 --- a/cmd/agent/gui/render.go +++ b/cmd/agent/gui/render.go @@ -16,10 +16,10 @@ import ( "github.com/DataDog/datadog-agent/pkg/autodiscovery" "github.com/DataDog/datadog-agent/pkg/collector" "github.com/DataDog/datadog-agent/pkg/collector/check/stats" - "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/render" ) -var fmap = status.Fmap() +var fmap = render.Fmap() func init() { fmap["lastErrorTraceback"] = lastErrorTraceback diff --git a/cmd/agent/subcommands/status/command.go b/cmd/agent/subcommands/status/command.go index b140b092a5dce..9846fecf0dfba 100644 --- a/cmd/agent/subcommands/status/command.go +++ b/cmd/agent/subcommands/status/command.go @@ -26,7 +26,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/sysprobeconfig/sysprobeconfigimpl" "github.com/DataDog/datadog-agent/pkg/api/util" pkgconfig "github.com/DataDog/datadog-agent/pkg/config" - "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/util/fxutil" "github.com/DataDog/datadog-agent/pkg/util/scrubber" @@ -179,7 +179,7 @@ func requestStatus(config config.Component, cliParams *cliParams) error { } else if cliParams.jsonStatus { s = string(r) } else { - formattedStatus, err := status.FormatStatus(r) + formattedStatus, err := render.FormatStatus(r) if err != nil { return err } diff --git a/cmd/cluster-agent/subcommands/metamap/command.go b/cmd/cluster-agent/subcommands/metamap/command.go index 7a4c5b2ee7199..9d7a3f69c60af 100644 --- a/cmd/cluster-agent/subcommands/metamap/command.go +++ b/cmd/cluster-agent/subcommands/metamap/command.go @@ -20,7 +20,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/log" "github.com/DataDog/datadog-agent/pkg/api/util" pkgconfig "github.com/DataDog/datadog-agent/pkg/config" - "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/util/fxutil" ) @@ -88,7 +88,7 @@ func getMetadataMap(nodeName string) error { return e } - formattedMetadataMap, err := status.FormatMetadataMapCLI(r) + formattedMetadataMap, err := render.FormatMetadataMapCLI(r) if err != nil { return err } diff --git a/cmd/cluster-agent/subcommands/status/command.go b/cmd/cluster-agent/subcommands/status/command.go index effde5dd4f009..71da99cd8e7eb 100644 --- a/cmd/cluster-agent/subcommands/status/command.go +++ b/cmd/cluster-agent/subcommands/status/command.go @@ -23,7 +23,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/log" "github.com/DataDog/datadog-agent/pkg/api/util" pkgconfig "github.com/DataDog/datadog-agent/pkg/config" - "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/util/fxutil" ) @@ -97,7 +97,7 @@ func run(log log.Component, config config.Component, cliParams *cliParams) error } else if cliParams.jsonStatus { s = string(r) } else { - formattedStatus, err := status.FormatDCAStatus(r) + formattedStatus, err := render.FormatDCAStatus(r) if err != nil { return err } diff --git a/cmd/process-agent/subcommands/status/status.go b/cmd/process-agent/subcommands/status/status.go index cbfa41c908b7e..c0e771c44aba3 100644 --- a/cmd/process-agent/subcommands/status/status.go +++ b/cmd/process-agent/subcommands/status/status.go @@ -23,7 +23,7 @@ import ( apiutil "github.com/DataDog/datadog-agent/pkg/api/util" ddconfig "github.com/DataDog/datadog-agent/pkg/config" "github.com/DataDog/datadog-agent/pkg/process/util/status" - ddstatus "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/util/fxutil" ) @@ -93,7 +93,7 @@ func writeNotRunning(log log.Component, w io.Writer) { } func writeError(log log.Component, w io.Writer, e error) { - tpl, err := template.New("").Funcs(ddstatus.Textfmap()).Parse(errorMessage) + tpl, err := template.New("").Funcs(render.Textfmap()).Parse(errorMessage) if err != nil { _ = log.Error(err) } @@ -146,7 +146,7 @@ func getAndWriteStatus(log log.Component, statusURL string, w io.Writer, options } } - stats, err := ddstatus.FormatProcessAgentStatus(body) + stats, err := render.FormatProcessAgentStatus(body) if err != nil { writeError(log, w, err) return diff --git a/cmd/process-agent/subcommands/status/status_test.go b/cmd/process-agent/subcommands/status/status_test.go index 56e1eac7528f2..6278cb086748e 100644 --- a/cmd/process-agent/subcommands/status/status_test.go +++ b/cmd/process-agent/subcommands/status/status_test.go @@ -22,7 +22,7 @@ import ( hostMetadataUtils "github.com/DataDog/datadog-agent/comp/metadata/host/utils" "github.com/DataDog/datadog-agent/pkg/config" "github.com/DataDog/datadog-agent/pkg/process/util/status" - ddstatus "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/trace/log" "github.com/DataDog/datadog-agent/pkg/util/fxutil" ) @@ -58,7 +58,7 @@ func TestStatus(t *testing.T) { // Build what the expected status should be j, err := json.Marshal(expectedStatus) require.NoError(t, err) - expectedOutput, err := ddstatus.FormatProcessAgentStatus(j) + expectedOutput, err := render.FormatProcessAgentStatus(j) require.NoError(t, err) // Build the actual status diff --git a/cmd/security-agent/subcommands/status/command.go b/cmd/security-agent/subcommands/status/command.go index 048d97094413c..0ae8c3f8cdb90 100644 --- a/cmd/security-agent/subcommands/status/command.go +++ b/cmd/security-agent/subcommands/status/command.go @@ -20,7 +20,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/config" "github.com/DataDog/datadog-agent/comp/core/log" "github.com/DataDog/datadog-agent/pkg/api/util" - "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/util/fxutil" ) @@ -96,7 +96,7 @@ func runStatus(log log.Component, config config.Component, params *cliParams) er } else if params.json { s = string(r) } else { - formattedStatus, err := status.FormatSecurityAgentStatus(r) + formattedStatus, err := render.FormatSecurityAgentStatus(r) if err != nil { return err } diff --git a/pkg/cli/subcommands/check/command.go b/pkg/cli/subcommands/check/command.go index fff9e38c07478..96868406879a5 100644 --- a/pkg/cli/subcommands/check/command.go +++ b/pkg/cli/subcommands/check/command.go @@ -40,11 +40,13 @@ import ( "github.com/DataDog/datadog-agent/pkg/cli/standalone" "github.com/DataDog/datadog-agent/pkg/collector" "github.com/DataDog/datadog-agent/pkg/collector/check" + checkid "github.com/DataDog/datadog-agent/pkg/collector/check/id" "github.com/DataDog/datadog-agent/pkg/collector/check/stats" pkgconfig "github.com/DataDog/datadog-agent/pkg/config" "github.com/DataDog/datadog-agent/pkg/config/model" "github.com/DataDog/datadog-agent/pkg/metadata/inventories" - "github.com/DataDog/datadog-agent/pkg/status" + statuscollector "github.com/DataDog/datadog-agent/pkg/status/collector" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/util/fxutil" "github.com/DataDog/datadog-agent/pkg/util/scrubber" ) @@ -370,6 +372,8 @@ func run(config config.Component, cliParams *cliParams, demultiplexer demultiple var checkFileOutput bytes.Buffer var instancesData []interface{} printer := aggregator.AgentDemultiplexerPrinter{DemultiplexerWithAggregator: demultiplexer} + collectorData := statuscollector.GetStatusInfo() + checkRuns := collectorData["runnerStats"].(map[string]interface{})["Checks"].(map[string]interface{}) for _, c := range cs { s := runCheck(cliParams, c, printer) @@ -378,20 +382,13 @@ func run(config config.Component, cliParams *cliParams, demultiplexer demultiple if cliParams.formatJSON { aggregatorData := printer.GetMetricsDataForPrint() - var collectorData map[string]interface{} - - collectorJSON, _ := status.GetCheckStatusJSON(c, s) - err = json.Unmarshal(collectorJSON, &collectorData) - if err != nil { - return err - } - - checkRuns := collectorData["runnerStats"].(map[string]interface{})["Checks"].(map[string]interface{})[cliParams.checkName].(map[string]interface{}) + checkRuns[c.String()] = make(map[checkid.ID]interface{}) + checkRuns[c.String()].(map[checkid.ID]interface{})[c.ID()] = s // There is only one checkID per run so we'll just access that - var runnerData map[string]interface{} + var runnerData map[checkid.ID]interface{} for _, checkIDData := range checkRuns { - runnerData = checkIDData.(map[string]interface{}) + runnerData = checkIDData.(map[checkid.ID]interface{}) break } @@ -462,8 +459,11 @@ func run(config config.Component, cliParams *cliParams, demultiplexer demultiple checkFileOutput.WriteString(data + "\n") } - checkStatus, _ := status.GetCheckStatus(c, s) - p(string(checkStatus)) + checkRuns[c.String()] = make(map[checkid.ID]interface{}) + checkRuns[c.String()].(map[checkid.ID]interface{})[c.ID()] = s + statusJSON, _ := json.Marshal(collectorData) + checkStatus, _ := render.FormatCheckStats(statusJSON) + p(checkStatus) metadata := inventories.GetCheckMetadata(c) if metadata != nil { diff --git a/pkg/flare/archive_dca.go b/pkg/flare/archive_dca.go index f34ba70195d05..5799b961860a9 100644 --- a/pkg/flare/archive_dca.go +++ b/pkg/flare/archive_dca.go @@ -21,6 +21,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/clusteragent/custommetrics" "github.com/DataDog/datadog-agent/pkg/config" "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver" "github.com/DataDog/datadog-agent/pkg/util/log" ) @@ -107,7 +108,7 @@ func getMetadataMap(fb flaretypes.FlareBuilder) error { return log.Errorf("Error while marshalling the cluster level metadata: %q", err) } - str, err := status.FormatMetadataMapCLI(metaBytes) + str, err := render.FormatMetadataMapCLI(metaBytes) if err != nil { return log.Errorf("Error while rendering the cluster level metadata: %q", err) } @@ -138,7 +139,7 @@ func getHPAStatus(fb flaretypes.FlareBuilder) error { return log.Errorf("Error while marshalling the cluster level metadata: %q", err) } - str, err := status.FormatHPAStatus(statsBytes) + str, err := render.FormatHPAStatus(statsBytes) if err != nil { return log.Errorf("Could not collect custommetricsprovider.log: %s", err) } diff --git a/pkg/status/fixtures/agent_status.json b/pkg/status/render/fixtures/agent_status.json similarity index 100% rename from pkg/status/fixtures/agent_status.json rename to pkg/status/render/fixtures/agent_status.json diff --git a/pkg/status/helpers.go b/pkg/status/render/helpers.go similarity index 99% rename from pkg/status/helpers.go rename to pkg/status/render/helpers.go index d0378971d5969..d32c41c70f439 100644 --- a/pkg/status/helpers.go +++ b/pkg/status/render/helpers.go @@ -3,7 +3,7 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. -package status +package render import ( "encoding/json" @@ -72,6 +72,8 @@ func Textfmap() ttemplate.FuncMap { } } +const timeFormat = "2006-01-02 15:04:05.999 MST" + func doNotEscape(value string) htemplate.HTML { return htemplate.HTML(value) } diff --git a/pkg/status/helpers_test.go b/pkg/status/render/helpers_test.go similarity index 97% rename from pkg/status/helpers_test.go rename to pkg/status/render/helpers_test.go index 166f87d3f069c..0ef5ab0850784 100644 --- a/pkg/status/helpers_test.go +++ b/pkg/status/render/helpers_test.go @@ -3,7 +3,7 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. -package status +package render import ( "testing" diff --git a/pkg/status/render.go b/pkg/status/render/render.go similarity index 80% rename from pkg/status/render.go rename to pkg/status/render/render.go index 57fe0c6d67f55..a8edf8dd03b88 100644 --- a/pkg/status/render.go +++ b/pkg/status/render/render.go @@ -3,7 +3,8 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. -package status +// Package render has all the formating options for status output +package render import ( "bytes" @@ -56,40 +57,40 @@ func FormatStatus(data []byte) (string, error) { stats["title"] = title var b = new(bytes.Buffer) - headerFunc := func() error { return RenderStatusTemplate(b, "/header.tmpl", stats) } + headerFunc := func() error { return renderStatusTemplate(b, "/header.tmpl", stats) } checkStatsFunc := func() error { - return RenderStatusTemplate(b, "/collector.tmpl", stats) + return renderStatusTemplate(b, "/collector.tmpl", stats) } - jmxFetchFunc := func() error { return RenderStatusTemplate(b, "/jmxfetch.tmpl", stats) } - forwarderFunc := func() error { return RenderStatusTemplate(b, "/forwarder.tmpl", forwarderStats) } - endpointsFunc := func() error { return RenderStatusTemplate(b, "/endpoints.tmpl", endpointsInfos) } - logsAgentFunc := func() error { return RenderStatusTemplate(b, "/logsagent.tmpl", logsStats) } + jmxFetchFunc := func() error { return renderStatusTemplate(b, "/jmxfetch.tmpl", stats) } + forwarderFunc := func() error { return renderStatusTemplate(b, "/forwarder.tmpl", forwarderStats) } + endpointsFunc := func() error { return renderStatusTemplate(b, "/endpoints.tmpl", endpointsInfos) } + logsAgentFunc := func() error { return renderStatusTemplate(b, "/logsagent.tmpl", logsStats) } systemProbeFunc := func() error { if systemProbeStats != nil { - return RenderStatusTemplate(b, "/systemprobe.tmpl", systemProbeStats) + return renderStatusTemplate(b, "/systemprobe.tmpl", systemProbeStats) } return nil } - processAgentFunc := func() error { return RenderStatusTemplate(b, "/process-agent.tmpl", processAgentStatus) } - traceAgentFunc := func() error { return RenderStatusTemplate(b, "/trace-agent.tmpl", stats["apmStats"]) } - aggregatorFunc := func() error { return RenderStatusTemplate(b, "/aggregator.tmpl", aggregatorStats) } - dogstatsdFunc := func() error { return RenderStatusTemplate(b, "/dogstatsd.tmpl", dogstatsdStats) } + processAgentFunc := func() error { return renderStatusTemplate(b, "/process-agent.tmpl", processAgentStatus) } + traceAgentFunc := func() error { return renderStatusTemplate(b, "/trace-agent.tmpl", stats["apmStats"]) } + aggregatorFunc := func() error { return renderStatusTemplate(b, "/aggregator.tmpl", aggregatorStats) } + dogstatsdFunc := func() error { return renderStatusTemplate(b, "/dogstatsd.tmpl", dogstatsdStats) } clusterAgentFunc := func() error { if config.Datadog.GetBool("cluster_agent.enabled") || config.Datadog.GetBool("cluster_checks.enabled") { - return RenderStatusTemplate(b, "/clusteragent.tmpl", dcaStats) + return renderStatusTemplate(b, "/clusteragent.tmpl", dcaStats) } return nil } snmpTrapFunc := func() error { if traps.IsEnabled(config.Datadog) { - return RenderStatusTemplate(b, "/snmp-traps.tmpl", snmpTrapsStats) + return renderStatusTemplate(b, "/snmp-traps.tmpl", snmpTrapsStats) } return nil } netflowFunc := func() error { if server.IsEnabled() { - return RenderStatusTemplate(b, "/netflow.tmpl", netflowStats) + return renderStatusTemplate(b, "/netflow.tmpl", netflowStats) } return nil } @@ -102,11 +103,11 @@ func FormatStatus(data []byte) (string, error) { return nil } remoteConfigFunc := func() error { - return RenderStatusTemplate(b, "/remoteconfig.tmpl", stats) + return renderStatusTemplate(b, "/remoteconfig.tmpl", stats) } otlpFunc := func() error { if otlp.IsDisplayed() { - return RenderStatusTemplate(b, "/otlp.tmpl", stats) + return renderStatusTemplate(b, "/otlp.tmpl", stats) } return nil } @@ -153,20 +154,20 @@ func FormatDCAStatus(data []byte) (string, error) { var b = new(bytes.Buffer) var errs []error - if err := RenderStatusTemplate(b, "/header.tmpl", stats); err != nil { + if err := renderStatusTemplate(b, "/header.tmpl", stats); err != nil { errs = append(errs, err) } - if err := RenderStatusTemplate(b, "/collector.tmpl", stats); err != nil { + if err := renderStatusTemplate(b, "/collector.tmpl", stats); err != nil { errs = append(errs, err) } - if err := RenderStatusTemplate(b, "/forwarder.tmpl", forwarderStats); err != nil { + if err := renderStatusTemplate(b, "/forwarder.tmpl", forwarderStats); err != nil { errs = append(errs, err) } - if err := RenderStatusTemplate(b, "/endpoints.tmpl", endpointsInfos); err != nil { + if err := renderStatusTemplate(b, "/endpoints.tmpl", endpointsInfos); err != nil { errs = append(errs, err) } if config.Datadog.GetBool("compliance_config.enabled") { - if err := RenderStatusTemplate(b, "/logsagent.tmpl", logsStats); err != nil { + if err := renderStatusTemplate(b, "/logsagent.tmpl", logsStats); err != nil { errs = append(errs, err) } } @@ -174,7 +175,7 @@ func FormatDCAStatus(data []byte) (string, error) { errs = append(errs, err) } if config.Datadog.GetBool("orchestrator_explorer.enabled") { - if err := RenderStatusTemplate(b, "/orchestrator.tmpl", orchestratorStats); err != nil { + if err := renderStatusTemplate(b, "/orchestrator.tmpl", orchestratorStats); err != nil { errs = append(errs, err) } } @@ -193,7 +194,7 @@ func FormatHPAStatus(data []byte) (string, error) { } var b = new(bytes.Buffer) var errs []error - if err := RenderStatusTemplate(b, "/custommetricsprovider.tmpl", stats); err != nil { + if err := renderStatusTemplate(b, "/custommetricsprovider.tmpl", stats); err != nil { errs = append(errs, err) } if err := renderErrors(b, errs); err != nil { @@ -216,7 +217,7 @@ func FormatSecurityAgentStatus(data []byte) (string, error) { var b = new(bytes.Buffer) var errs []error - if err := RenderStatusTemplate(b, "/header.tmpl", stats); err != nil { + if err := renderStatusTemplate(b, "/header.tmpl", stats); err != nil { errs = append(errs, err) } if err := renderRuntimeSecurityStats(b, stats["runtimeSecurityStatus"]); err != nil { @@ -240,7 +241,7 @@ func FormatProcessAgentStatus(data []byte) (string, error) { } var b = new(bytes.Buffer) var errs []error - if err := RenderStatusTemplate(b, "/process-agent.tmpl", stats); err != nil { + if err := renderStatusTemplate(b, "/process-agent.tmpl", stats); err != nil { errs = append(errs, err) } if err := renderErrors(b, errs); err != nil { @@ -258,7 +259,7 @@ func FormatMetadataMapCLI(data []byte) (string, error) { } var b = new(bytes.Buffer) var errs []error - if err := RenderStatusTemplate(b, "/metadatamapper.tmpl", stats); err != nil { + if err := renderStatusTemplate(b, "/metadatamapper.tmpl", stats); err != nil { errs = append(errs, err) } if err := renderErrors(b, errs); err != nil { @@ -267,7 +268,8 @@ func FormatMetadataMapCLI(data []byte) (string, error) { return b.String(), nil } -func renderCheckStats(data []byte, checkName string) (string, error) { +// FormatCheckStats takes a json bytestring and prints out the formatted collector template. +func FormatCheckStats(data []byte) (string, error) { stats, renderError, err := unmarshalStatus(data) if renderError != "" || err != nil { return renderError, err @@ -275,7 +277,7 @@ func renderCheckStats(data []byte, checkName string) (string, error) { var b = new(bytes.Buffer) var errs []error - if err := RenderStatusTemplate(b, "/collector.tmpl", stats); err != nil { + if err := renderStatusTemplate(b, "/collector.tmpl", stats); err != nil { errs = append(errs, err) } if err := renderErrors(b, errs); err != nil { @@ -290,13 +292,13 @@ func renderComplianceChecksStats(w io.Writer, runnerStats interface{}, complianc checkStats["RunnerStats"] = runnerStats checkStats["ComplianceStatus"] = complianceStatus checkStats["ComplianceChecks"] = complianceChecks - return RenderStatusTemplate(w, "/compliance.tmpl", checkStats) + return renderStatusTemplate(w, "/compliance.tmpl", checkStats) } func renderRuntimeSecurityStats(w io.Writer, runtimeSecurityStatus interface{}) error { status := make(map[string]interface{}) status["RuntimeSecurityStatus"] = runtimeSecurityStatus - return RenderStatusTemplate(w, "/runtimesecurity.tmpl", status) + return renderStatusTemplate(w, "/runtimesecurity.tmpl", status) } func renderAutodiscoveryStats(w io.Writer, adEnabledFeatures interface{}, adConfigErrors interface{}, filterErrors interface{}) error { @@ -304,13 +306,13 @@ func renderAutodiscoveryStats(w io.Writer, adEnabledFeatures interface{}, adConf autodiscoveryStats["adEnabledFeatures"] = adEnabledFeatures autodiscoveryStats["adConfigErrors"] = adConfigErrors autodiscoveryStats["filterErrors"] = filterErrors - return RenderStatusTemplate(w, "/autodiscovery.tmpl", autodiscoveryStats) + return renderStatusTemplate(w, "/autodiscovery.tmpl", autodiscoveryStats) } //go:embed templates var templatesFS embed.FS -func RenderStatusTemplate(w io.Writer, templateName string, stats interface{}) error { +func renderStatusTemplate(w io.Writer, templateName string, stats interface{}) error { tmpl, tmplErr := templatesFS.ReadFile(path.Join("templates", templateName)) if tmplErr != nil { return tmplErr @@ -321,7 +323,7 @@ func RenderStatusTemplate(w io.Writer, templateName string, stats interface{}) e func renderErrors(w io.Writer, errs []error) error { if len(errs) > 0 { - return RenderStatusTemplate(w, "/rendererrors.tmpl", errs) + return renderStatusTemplate(w, "/rendererrors.tmpl", errs) } return nil } diff --git a/pkg/status/render_test.go b/pkg/status/render/render_test.go similarity index 86% rename from pkg/status/render_test.go rename to pkg/status/render/render_test.go index 3c7e474207063..850922e8dece7 100644 --- a/pkg/status/render_test.go +++ b/pkg/status/render/render_test.go @@ -3,7 +3,7 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2021-present Datadog, Inc. -package status +package render import ( "os" @@ -14,7 +14,7 @@ import ( ) func TestFormatStatus(t *testing.T) { - agentJson, err := os.ReadFile("fixtures/agent_status.json") + agentJSON, err := os.ReadFile("fixtures/agent_status.json") require.NoError(t, err) const statusRenderErrors = "Status render errors" @@ -25,7 +25,7 @@ func TestFormatStatus(t *testing.T) { }) t.Run("no render errors", func(t *testing.T) { - actual, err := FormatStatus(agentJson) + actual, err := FormatStatus(agentJSON) require.NoError(t, err) assert.NotContains(t, actual, statusRenderErrors) }) diff --git a/pkg/status/templates/aggregator.tmpl b/pkg/status/render/templates/aggregator.tmpl similarity index 100% rename from pkg/status/templates/aggregator.tmpl rename to pkg/status/render/templates/aggregator.tmpl diff --git a/pkg/status/templates/autodiscovery.tmpl b/pkg/status/render/templates/autodiscovery.tmpl similarity index 100% rename from pkg/status/templates/autodiscovery.tmpl rename to pkg/status/render/templates/autodiscovery.tmpl diff --git a/pkg/status/templates/clusteragent.tmpl b/pkg/status/render/templates/clusteragent.tmpl similarity index 100% rename from pkg/status/templates/clusteragent.tmpl rename to pkg/status/render/templates/clusteragent.tmpl diff --git a/pkg/status/templates/collector.tmpl b/pkg/status/render/templates/collector.tmpl similarity index 100% rename from pkg/status/templates/collector.tmpl rename to pkg/status/render/templates/collector.tmpl diff --git a/pkg/status/templates/compliance.tmpl b/pkg/status/render/templates/compliance.tmpl similarity index 100% rename from pkg/status/templates/compliance.tmpl rename to pkg/status/render/templates/compliance.tmpl diff --git a/pkg/status/templates/custommetricsprovider.tmpl b/pkg/status/render/templates/custommetricsprovider.tmpl similarity index 100% rename from pkg/status/templates/custommetricsprovider.tmpl rename to pkg/status/render/templates/custommetricsprovider.tmpl diff --git a/pkg/status/templates/dogstatsd.tmpl b/pkg/status/render/templates/dogstatsd.tmpl similarity index 100% rename from pkg/status/templates/dogstatsd.tmpl rename to pkg/status/render/templates/dogstatsd.tmpl diff --git a/pkg/status/templates/endpoints.tmpl b/pkg/status/render/templates/endpoints.tmpl similarity index 100% rename from pkg/status/templates/endpoints.tmpl rename to pkg/status/render/templates/endpoints.tmpl diff --git a/pkg/status/templates/forwarder.tmpl b/pkg/status/render/templates/forwarder.tmpl similarity index 100% rename from pkg/status/templates/forwarder.tmpl rename to pkg/status/render/templates/forwarder.tmpl diff --git a/pkg/status/templates/header.tmpl b/pkg/status/render/templates/header.tmpl similarity index 100% rename from pkg/status/templates/header.tmpl rename to pkg/status/render/templates/header.tmpl diff --git a/pkg/status/templates/jmxfetch.tmpl b/pkg/status/render/templates/jmxfetch.tmpl similarity index 100% rename from pkg/status/templates/jmxfetch.tmpl rename to pkg/status/render/templates/jmxfetch.tmpl diff --git a/pkg/status/templates/logsagent.tmpl b/pkg/status/render/templates/logsagent.tmpl similarity index 100% rename from pkg/status/templates/logsagent.tmpl rename to pkg/status/render/templates/logsagent.tmpl diff --git a/pkg/status/templates/metadatamapper.tmpl b/pkg/status/render/templates/metadatamapper.tmpl similarity index 100% rename from pkg/status/templates/metadatamapper.tmpl rename to pkg/status/render/templates/metadatamapper.tmpl diff --git a/pkg/status/templates/netflow.tmpl b/pkg/status/render/templates/netflow.tmpl similarity index 100% rename from pkg/status/templates/netflow.tmpl rename to pkg/status/render/templates/netflow.tmpl diff --git a/pkg/status/templates/orchestrator.tmpl b/pkg/status/render/templates/orchestrator.tmpl similarity index 100% rename from pkg/status/templates/orchestrator.tmpl rename to pkg/status/render/templates/orchestrator.tmpl diff --git a/pkg/status/templates/otlp.tmpl b/pkg/status/render/templates/otlp.tmpl similarity index 100% rename from pkg/status/templates/otlp.tmpl rename to pkg/status/render/templates/otlp.tmpl diff --git a/pkg/status/templates/process-agent.tmpl b/pkg/status/render/templates/process-agent.tmpl similarity index 100% rename from pkg/status/templates/process-agent.tmpl rename to pkg/status/render/templates/process-agent.tmpl diff --git a/pkg/status/templates/remoteconfig.tmpl b/pkg/status/render/templates/remoteconfig.tmpl similarity index 100% rename from pkg/status/templates/remoteconfig.tmpl rename to pkg/status/render/templates/remoteconfig.tmpl diff --git a/pkg/status/templates/rendererrors.tmpl b/pkg/status/render/templates/rendererrors.tmpl similarity index 100% rename from pkg/status/templates/rendererrors.tmpl rename to pkg/status/render/templates/rendererrors.tmpl diff --git a/pkg/status/templates/runtimesecurity.tmpl b/pkg/status/render/templates/runtimesecurity.tmpl similarity index 100% rename from pkg/status/templates/runtimesecurity.tmpl rename to pkg/status/render/templates/runtimesecurity.tmpl diff --git a/pkg/status/templates/snmp-traps.tmpl b/pkg/status/render/templates/snmp-traps.tmpl similarity index 100% rename from pkg/status/templates/snmp-traps.tmpl rename to pkg/status/render/templates/snmp-traps.tmpl diff --git a/pkg/status/templates/systemprobe.tmpl b/pkg/status/render/templates/systemprobe.tmpl similarity index 100% rename from pkg/status/templates/systemprobe.tmpl rename to pkg/status/render/templates/systemprobe.tmpl diff --git a/pkg/status/templates/trace-agent.tmpl b/pkg/status/render/templates/trace-agent.tmpl similarity index 100% rename from pkg/status/templates/trace-agent.tmpl rename to pkg/status/render/templates/trace-agent.tmpl diff --git a/pkg/status/status.go b/pkg/status/status.go index ea110dae7e2e1..9b8455a578e97 100644 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -24,8 +24,6 @@ import ( "github.com/DataDog/datadog-agent/pkg/clusteragent/custommetrics" "github.com/DataDog/datadog-agent/pkg/clusteragent/externalmetrics" "github.com/DataDog/datadog-agent/pkg/clusteragent/orchestrator" - "github.com/DataDog/datadog-agent/pkg/collector/check" - checkid "github.com/DataDog/datadog-agent/pkg/collector/check/id" checkstats "github.com/DataDog/datadog-agent/pkg/collector/check/stats" "github.com/DataDog/datadog-agent/pkg/collector/python" "github.com/DataDog/datadog-agent/pkg/config" @@ -33,6 +31,7 @@ import ( logsStatus "github.com/DataDog/datadog-agent/pkg/logs/status" "github.com/DataDog/datadog-agent/pkg/snmp/traps" "github.com/DataDog/datadog-agent/pkg/status/collector" + "github.com/DataDog/datadog-agent/pkg/status/render" "github.com/DataDog/datadog-agent/pkg/util/containers" "github.com/DataDog/datadog-agent/pkg/util/flavor" httputils "github.com/DataDog/datadog-agent/pkg/util/http" @@ -41,8 +40,6 @@ import ( "github.com/DataDog/datadog-agent/pkg/version" ) -var timeFormat = "2006-01-02 15:04:05.999 MST" - // GetStatus grabs the status from expvar and puts it into a map func GetStatus(verbose bool, invAgent inventoryagent.Component) (map[string]interface{}, error) { stats, err := getCommonStatus(invAgent) @@ -116,39 +113,7 @@ func GetAndFormatStatus(invAgent inventoryagent.Component) ([]byte, error) { return nil, err } - st, err := FormatStatus(statusJSON) - if err != nil { - return nil, err - } - - return []byte(st), nil -} - -// GetCheckStatusJSON gets the status of a single check as JSON -func GetCheckStatusJSON(c check.Check, cs *checkstats.Stats) ([]byte, error) { - s := collector.GetStatusInfo() - - checks := s["runnerStats"].(map[string]interface{})["Checks"].(map[string]interface{}) - - checks[c.String()] = make(map[checkid.ID]interface{}) - checks[c.String()].(map[checkid.ID]interface{})[c.ID()] = cs - - statusJSON, err := json.Marshal(s) - if err != nil { - return nil, err - } - - return statusJSON, nil -} - -// GetCheckStatus gets the status of a single check as human-readable text -func GetCheckStatus(c check.Check, cs *checkstats.Stats) ([]byte, error) { - statusJSON, err := GetCheckStatusJSON(c, cs) - if err != nil { - return nil, err - } - - st, err := renderCheckStats(statusJSON, c.String()) + st, err := render.FormatStatus(statusJSON) if err != nil { return nil, err } @@ -230,7 +195,7 @@ func GetAndFormatDCAStatus() ([]byte, error) { log.Infof("Error while marshalling %q", err) return nil, err } - st, err := FormatDCAStatus(statusJSON) + st, err := render.FormatDCAStatus(statusJSON) if err != nil { log.Infof("Error formatting the status %q", err) return nil, err @@ -253,7 +218,7 @@ func GetAndFormatSecurityAgentStatus(runtimeStatus, complianceStatus map[string] return nil, err } - st, err := FormatSecurityAgentStatus(statusJSON) + st, err := render.FormatSecurityAgentStatus(statusJSON) if err != nil { return nil, err }