Skip to content

Commit

Permalink
simplify flare requests for system-probe (#31293)
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle authored Dec 3, 2024
1 parent 2d20432 commit f15536c
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 694 deletions.
1 change: 0 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@
/pkg/flare/*_win.go @Datadog/windows-agent
/pkg/flare/*_windows.go @Datadog/windows-agent
/pkg/flare/*_windows_test.go @Datadog/windows-agent
/pkg/flare/sysprobe @DataDog/Networks @Datadog/windows-agent @DataDog/agent-security @DataDog/universal-service-monitoring @DataDog/ebpf-platform
/pkg/fleet/ @DataDog/fleet @DataDog/windows-agent
/pkg/pidfile/ @DataDog/agent-shared-components
/pkg/persistentcache/ @DataDog/agent-metrics-logs
Expand Down
36 changes: 24 additions & 12 deletions cmd/agent/subcommands/flare/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ package flare
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/DataDog/datadog-agent/cmd/agent/command"
"github.com/DataDog/datadog-agent/cmd/agent/common"
"github.com/DataDog/datadog-agent/cmd/agent/subcommands/streamlogs"
sysprobeclient "github.com/DataDog/datadog-agent/cmd/system-probe/api/client"
"github.com/DataDog/datadog-agent/comp/aggregator/diagnosesendermanager/diagnosesendermanagerimpl"
authtokenimpl "github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
"github.com/DataDog/datadog-agent/comp/collector/collector"
Expand Down Expand Up @@ -53,7 +55,6 @@ import (
"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/config/settings"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
procnet "github.com/DataDog/datadog-agent/pkg/process/net"
"github.com/DataDog/datadog-agent/pkg/serializer"
"github.com/DataDog/datadog-agent/pkg/util/defaultpaths"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
Expand Down Expand Up @@ -252,21 +253,32 @@ func readProfileData(seconds int) (flare.ProfileData, error) {
}

if pkgconfigsetup.SystemProbe().GetBool("system_probe_config.enabled") {
probeUtil, probeUtilErr := procnet.GetRemoteSystemProbeUtil(pkgconfigsetup.SystemProbe().GetString("system_probe_config.sysprobe_socket"))
client := &http.Client{
Transport: &http.Transport{
DialContext: sysprobeclient.DialContextFunc(pkgconfigsetup.SystemProbe().GetString("system_probe_config.sysprobe_socket")),
},
}

if !errors.Is(probeUtilErr, procnet.ErrNotImplemented) {
sysProbeGet := func() pprofGetter {
return func(path string) ([]byte, error) {
if probeUtilErr != nil {
return nil, probeUtilErr
}
sysProbeGet := func() pprofGetter {
return func(path string) ([]byte, error) {
var buf bytes.Buffer
pprofURL := sysprobeclient.DebugURL("/pprof" + path)
req, err := http.NewRequest(http.MethodGet, pprofURL, &buf)
if err != nil {
return nil, err
}

return probeUtil.GetPprof(path)
res, err := client.Do(req)
if err != nil {
return nil, err
}
}
defer res.Body.Close()

agentCollectors["system-probe"] = serviceProfileCollector(sysProbeGet(), seconds)
return io.ReadAll(res.Body)
}
}

agentCollectors["system-probe"] = serviceProfileCollector(sysProbeGet(), seconds)
}

var errs error
Expand Down
30 changes: 30 additions & 0 deletions cmd/system-probe/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package client

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -81,7 +82,36 @@ func constructURL(module string, endpoint string) string {
return u.String()
}

// URL constructs a system-probe URL for a module-less endpoint.
func URL(endpoint string) string {
return constructURL("", endpoint)
}

// DebugURL constructs a system-probe URL for the debug module and endpoint.
func DebugURL(endpoint string) string {
return constructURL("debug", endpoint)
}

// ModuleURL constructs a system-probe ModuleURL given the specified module and endpoint.
func ModuleURL(module types.ModuleName, endpoint string) string {
return constructURL(string(module), endpoint)
}

// ReadAllResponseBody reads the entire HTTP response body as a byte slice
func ReadAllResponseBody(resp *http.Response) ([]byte, error) {
// if we are not able to determine the content length
// we read the whole body without pre-allocation
if resp.ContentLength <= 0 {
return io.ReadAll(resp.Body)
}

// if we know the content length we pre-allocate the buffer
var buf bytes.Buffer
buf.Grow(int(resp.ContentLength))

_, err := buf.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
49 changes: 29 additions & 20 deletions pkg/flare/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (

"github.com/fatih/color"

sysprobeclient "github.com/DataDog/datadog-agent/cmd/system-probe/api/client"
flaretypes "github.com/DataDog/datadog-agent/comp/core/flare/types"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/api/security"
apiutil "github.com/DataDog/datadog-agent/pkg/api/util"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/diagnose"
"github.com/DataDog/datadog-agent/pkg/diagnose/diagnosis"
"github.com/DataDog/datadog-agent/pkg/flare/sysprobe"
"github.com/DataDog/datadog-agent/pkg/status/health"
systemprobeStatus "github.com/DataDog/datadog-agent/pkg/status/systemprobe"
"github.com/DataDog/datadog-agent/pkg/util/ecs"
Expand Down Expand Up @@ -143,18 +143,11 @@ func provideConfigDump(fb flaretypes.FlareBuilder) error {
}

func provideSystemProbe(fb flaretypes.FlareBuilder) error {
systemProbeConfigBPFDir := pkgconfigsetup.SystemProbe().GetString("system_probe_config.bpf_dir")
if systemProbeConfigBPFDir != "" {
fb.RegisterDirPerm(systemProbeConfigBPFDir)
}
addSystemProbePlatformSpecificEntries(fb)

if pkgconfigsetup.SystemProbe().GetBool("system_probe_config.enabled") {
fb.AddFileFromFunc(filepath.Join("expvar", "system-probe"), getSystemProbeStats) //nolint:errcheck
fb.AddFileFromFunc(filepath.Join("system-probe", "system_probe_telemetry.log"), getSystemProbeTelemetry) // nolint:errcheck
fb.AddFileFromFunc(filepath.Join("system-probe", "conntrack_cached.log"), getSystemProbeConntrackCached) // nolint:errcheck
fb.AddFileFromFunc(filepath.Join("system-probe", "conntrack_host.log"), getSystemProbeConntrackHost) // nolint:errcheck
fb.AddFileFromFunc(filepath.Join("system-probe", "ebpf_btf_loader.log"), getSystemProbeBTFLoaderInfo) // nolint:errcheck
_ = fb.AddFileFromFunc(filepath.Join("expvar", "system-probe"), getSystemProbeStats)
_ = fb.AddFileFromFunc(filepath.Join("system-probe", "system_probe_telemetry.log"), getSystemProbeTelemetry)
}
return nil
}
Expand Down Expand Up @@ -261,16 +254,9 @@ func getSystemProbeStats() ([]byte, error) {
}

func getSystemProbeTelemetry() ([]byte, error) {
return sysprobe.GetSystemProbeTelemetry(getSystemProbeSocketPath())
}
func getSystemProbeConntrackCached() ([]byte, error) {
return sysprobe.GetSystemProbeConntrackCached(getSystemProbeSocketPath())
}
func getSystemProbeConntrackHost() ([]byte, error) {
return sysprobe.GetSystemProbeConntrackHost(getSystemProbeSocketPath())
}
func getSystemProbeBTFLoaderInfo() ([]byte, error) {
return sysprobe.GetSystemProbeBTFLoaderInfo(getSystemProbeSocketPath())
sysProbeClient := sysprobeclient.Get(getSystemProbeSocketPath())
url := sysprobeclient.URL("/telemetry")
return getHTTPData(sysProbeClient, url)
}

// getProcessAgentFullConfig fetches process-agent runtime config as YAML and returns it to be added to process_agent_runtime_config_dump.yaml
Expand Down Expand Up @@ -533,3 +519,26 @@ func functionOutputToBytes(fct func(writer io.Writer) error) []byte {

return buffer.Bytes()
}

func getHTTPData(client *http.Client, url string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}

resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("non-ok status code: url: %s, status_code: %d, response: `%s`", req.URL, resp.StatusCode, string(data))
}
return data, nil
}
33 changes: 32 additions & 1 deletion pkg/flare/archive_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ import (

"github.com/DataDog/ebpf-manager/tracefs"

sysprobeclient "github.com/DataDog/datadog-agent/cmd/system-probe/api/client"
sysconfig "github.com/DataDog/datadog-agent/cmd/system-probe/config"
flaretypes "github.com/DataDog/datadog-agent/comp/core/flare/types"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
)

func addSystemProbePlatformSpecificEntries(fb flaretypes.FlareBuilder) {
sysprobeSocketLocation := pkgconfigsetup.SystemProbe().GetString("system_probe_config.sysprobe_socket")
systemProbeConfigBPFDir := pkgconfigsetup.SystemProbe().GetString("system_probe_config.bpf_dir")
if systemProbeConfigBPFDir != "" {
fb.RegisterDirPerm(systemProbeConfigBPFDir)
}

sysprobeSocketLocation := getSystemProbeSocketPath()
if sysprobeSocketLocation != "" {
fb.RegisterDirPerm(filepath.Dir(sysprobeSocketLocation))
}

if pkgconfigsetup.SystemProbe().GetBool("system_probe_config.enabled") {
_ = fb.AddFileFromFunc(filepath.Join("system-probe", "conntrack_cached.log"), getSystemProbeConntrackCached)
_ = fb.AddFileFromFunc(filepath.Join("system-probe", "conntrack_host.log"), getSystemProbeConntrackHost)
_ = fb.AddFileFromFunc(filepath.Join("system-probe", "ebpf_btf_loader.log"), getSystemProbeBTFLoaderInfo)
}
}

func getLinuxKernelSymbols(fb flaretypes.FlareBuilder) error {
Expand Down Expand Up @@ -117,3 +130,21 @@ func getLinuxTracingAvailableFilterFunctions(fb flaretypes.FlareBuilder) error {
}
return fb.CopyFile(filepath.Join(traceFSPath, "available_filter_functions"))
}

func getSystemProbeConntrackCached() ([]byte, error) {
sysProbeClient := sysprobeclient.Get(getSystemProbeSocketPath())
url := sysprobeclient.ModuleURL(sysconfig.NetworkTracerModule, "/debug/conntrack/cached")
return getHTTPData(sysProbeClient, url)
}

func getSystemProbeConntrackHost() ([]byte, error) {
sysProbeClient := sysprobeclient.Get(getSystemProbeSocketPath())
url := sysprobeclient.ModuleURL(sysconfig.NetworkTracerModule, "/debug/conntrack/host")
return getHTTPData(sysProbeClient, url)
}

func getSystemProbeBTFLoaderInfo() ([]byte, error) {
sysProbeClient := sysprobeclient.Get(getSystemProbeSocketPath())
url := sysprobeclient.DebugURL("/ebpf_btf_loader_info")
return getHTTPData(sysProbeClient, url)
}
20 changes: 0 additions & 20 deletions pkg/flare/sysprobe/archive.go

This file was deleted.

38 changes: 0 additions & 38 deletions pkg/flare/sysprobe/archive_linux.go

This file was deleted.

25 changes: 0 additions & 25 deletions pkg/flare/sysprobe/archive_nolinux.go

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/flare/sysprobe/archive_unsupported.go

This file was deleted.

Loading

0 comments on commit f15536c

Please sign in to comment.