Skip to content

Commit f257d7f

Browse files
guyarbgrantseltzer
authored andcommitted
system-probe: Allow pretty print for endpoints (#36237)
1 parent 9f18f5d commit f257d7f

File tree

13 files changed

+69
-28
lines changed

13 files changed

+69
-28
lines changed

cmd/system-probe/api/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func StartServer(cfg *sysconfigtypes.Config, settings settings.Component, teleme
4545
// agent checks as a means to check if system-probe is ready to serve
4646
// requests, see pkg/system-probe/api/client.
4747
mux.HandleFunc("/debug/stats", utils.WithConcurrencyLimit(utils.DefaultMaxConcurrentRequests, func(w http.ResponseWriter, _ *http.Request) {
48-
utils.WriteAsJSON(w, module.GetStats())
48+
utils.WriteAsJSON(w, module.GetStats(), utils.CompactOutput)
4949
}))
5050

5151
setupConfigHandlers(mux, settings)

cmd/system-probe/modules/crashdetect_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (wcdm *winCrashDetectModule) Register(httpMux *module.Router) error {
4848
httpMux.HandleFunc("/check", utils.WithConcurrencyLimit(1, func(w http.ResponseWriter, _ *http.Request) {
4949
log.Infof("Got check request in crashDetect")
5050
results := wcdm.WinCrashProbe.Get()
51-
utils.WriteAsJSON(w, results)
51+
utils.WriteAsJSON(w, results, utils.CompactOutput)
5252
}))
5353

5454
return nil

cmd/system-probe/modules/ebpf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (o *ebpfModule) Register(httpMux *module.Router) error {
5555
httpMux.HandleFunc("/check", utils.WithConcurrencyLimit(1, func(w http.ResponseWriter, _ *http.Request) {
5656
o.lastCheck.Store(time.Now().Unix())
5757
stats := o.Probe.GetAndFlush()
58-
utils.WriteAsJSON(w, stats)
58+
utils.WriteAsJSON(w, stats, utils.CompactOutput)
5959
}))
6060

6161
return nil

cmd/system-probe/modules/gpu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (t *GPUMonitoringModule) Register(httpMux *module.Router) error {
102102
return
103103
}
104104

105-
utils.WriteAsJSON(w, stats)
105+
utils.WriteAsJSON(w, stats, utils.CompactOutput)
106106
})
107107

108108
httpMux.HandleFunc("/debug/traced-programs", usm.GetTracedProgramsEndpoint(gpuconfigconsts.GpuModuleName))

cmd/system-probe/modules/network_tracer.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
148148
return
149149
}
150150

151-
utils.WriteAsJSON(w, stats)
151+
utils.WriteAsJSON(w, stats, utils.CompactOutput)
152152
})
153153

154154
httpMux.HandleFunc("/debug/http_monitoring", func(w http.ResponseWriter, req *http.Request) {
@@ -165,7 +165,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
165165
}
166166
defer cleanup()
167167

168-
utils.WriteAsJSON(w, httpdebugging.HTTP(cs.HTTP, cs.DNS))
168+
utils.WriteAsJSON(w, httpdebugging.HTTP(cs.HTTP, cs.DNS), utils.GetPrettyPrintFromQueryParams(req))
169169
})
170170

171171
httpMux.HandleFunc("/debug/kafka_monitoring", func(w http.ResponseWriter, req *http.Request) {
@@ -182,7 +182,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
182182
}
183183
defer cleanup()
184184

185-
utils.WriteAsJSON(w, kafkadebugging.Kafka(cs.Kafka))
185+
utils.WriteAsJSON(w, kafkadebugging.Kafka(cs.Kafka), utils.GetPrettyPrintFromQueryParams(req))
186186
})
187187

188188
httpMux.HandleFunc("/debug/postgres_monitoring", func(w http.ResponseWriter, req *http.Request) {
@@ -199,7 +199,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
199199
}
200200
defer cleanup()
201201

202-
utils.WriteAsJSON(w, postgresdebugging.Postgres(cs.Postgres))
202+
utils.WriteAsJSON(w, postgresdebugging.Postgres(cs.Postgres), utils.GetPrettyPrintFromQueryParams(req))
203203
})
204204

205205
httpMux.HandleFunc("/debug/redis_monitoring", func(w http.ResponseWriter, req *http.Request) {
@@ -216,7 +216,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
216216
}
217217
defer cleanup()
218218

219-
utils.WriteAsJSON(w, redisdebugging.Redis(cs.Redis))
219+
utils.WriteAsJSON(w, redisdebugging.Redis(cs.Redis), utils.GetPrettyPrintFromQueryParams(req))
220220
})
221221

222222
httpMux.HandleFunc("/debug/http2_monitoring", func(w http.ResponseWriter, req *http.Request) {
@@ -233,7 +233,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
233233
}
234234
defer cleanup()
235235

236-
utils.WriteAsJSON(w, httpdebugging.HTTP(cs.HTTP2, cs.DNS))
236+
utils.WriteAsJSON(w, httpdebugging.HTTP(cs.HTTP2, cs.DNS), utils.GetPrettyPrintFromQueryParams(req))
237237
})
238238

239239
// /debug/ebpf_maps as default will dump all registered maps/perfmaps
@@ -288,7 +288,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
288288
return
289289
}
290290

291-
utils.WriteAsJSON(w, cache)
291+
utils.WriteAsJSON(w, cache, utils.CompactOutput)
292292
})
293293

294294
httpMux.HandleFunc("/debug/usm_telemetry", telemetry.Handler)

cmd/system-probe/modules/oom_kill_probe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (o *oomKillModule) Register(httpMux *module.Router) error {
5454
httpMux.HandleFunc("/check", utils.WithConcurrencyLimit(utils.DefaultMaxConcurrentRequests, func(w http.ResponseWriter, _ *http.Request) {
5555
o.lastCheck.Store(time.Now().Unix())
5656
stats := o.Probe.GetAndFlush()
57-
utils.WriteAsJSON(w, stats)
57+
utils.WriteAsJSON(w, stats, utils.CompactOutput)
5858
}))
5959

6060
return nil

cmd/system-probe/modules/tcp_queue_tracer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (t *tcpQueueLengthModule) Register(httpMux *module.Router) error {
5353
httpMux.HandleFunc("/check", func(w http.ResponseWriter, _ *http.Request) {
5454
t.lastCheck.Store(time.Now().Unix())
5555
stats := t.Tracer.GetAndFlush()
56-
utils.WriteAsJSON(w, stats)
56+
utils.WriteAsJSON(w, stats, utils.CompactOutput)
5757
})
5858

5959
return nil

pkg/collector/corechecks/servicediscovery/module/impl_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func (s *discovery) handleStateEndpoint(w http.ResponseWriter, _ *http.Request)
329329
state.LastCPUTimeUpdate = s.lastCPUTimeUpdate.Unix()
330330
state.LastNetworkStatsUpdate = s.lastNetworkStatsUpdate.Unix()
331331

332-
utils.WriteAsJSON(w, state)
332+
utils.WriteAsJSON(w, state, utils.CompactOutput)
333333
}
334334

335335
func (s *discovery) handleDebugEndpoint(w http.ResponseWriter, _ *http.Request) {
@@ -341,7 +341,7 @@ func (s *discovery) handleDebugEndpoint(w http.ResponseWriter, _ *http.Request)
341341
procRoot := kernel.ProcFSRoot()
342342
pids, err := process.Pids()
343343
if err != nil {
344-
utils.WriteAsJSON(w, "could not get PIDs")
344+
utils.WriteAsJSON(w, "could not get PIDs", utils.CompactOutput)
345345
return
346346
}
347347

@@ -362,7 +362,7 @@ func (s *discovery) handleDebugEndpoint(w http.ResponseWriter, _ *http.Request)
362362
services = append(services, *service)
363363
}
364364

365-
utils.WriteAsJSON(w, services)
365+
utils.WriteAsJSON(w, services, utils.CompactOutput)
366366
}
367367

368368
// handleCheck is the handler for the /check endpoint.
@@ -382,7 +382,7 @@ func (s *discovery) handleCheck(w http.ResponseWriter, req *http.Request) {
382382
return
383383
}
384384

385-
utils.WriteAsJSON(w, services)
385+
utils.WriteAsJSON(w, services, utils.CompactOutput)
386386
}
387387

388388
const prefix = "socket:["

pkg/collector/corechecks/system/wincrashdetect/wincrashdetect_windows_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestWinCrashReporting(t *testing.T) {
7272
var p *probe.WinCrashStatus
7373

7474
mux.Handle("/windows_crash_detection/check", http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
75-
utils.WriteAsJSON(rw, p)
75+
utils.WriteAsJSON(rw, p, utils.CompactOutput)
7676
}))
7777
mux.Handle("/debug/stats", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
7878
}))
@@ -224,7 +224,7 @@ func TestCrashReportingStates(t *testing.T) {
224224

225225
mux.Handle("/windows_crash_detection/check", http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
226226
results := cp.Get()
227-
utils.WriteAsJSON(rw, results)
227+
utils.WriteAsJSON(rw, results, utils.CompactOutput)
228228
}))
229229
mux.Handle("/debug/stats", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
230230
}))

pkg/dynamicinstrumentation/module/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (m *Module) Register(httpMux *module.Router) error {
7777
httpMux.HandleFunc("/check", utils.WithConcurrencyLimit(utils.DefaultMaxConcurrentRequests,
7878
func(w http.ResponseWriter, _ *http.Request) {
7979
stats := []string{}
80-
utils.WriteAsJSON(w, stats)
80+
utils.WriteAsJSON(w, stats, utils.CompactOutput)
8181
}))
8282

8383
log.Info("Registering dynamic instrumentation module")

pkg/network/protocols/telemetry/endpoint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (mm MarshableMetric) MarshalJSON() ([]byte, error) {
4343

4444
// Handler is meant to be used in conjunction with a HTTP server for exposing the
4545
// state of all metrics currently tracked by this library
46-
func Handler(w http.ResponseWriter, _ *http.Request) {
46+
func Handler(w http.ResponseWriter, req *http.Request) {
4747
metrics := globalRegistry.GetMetrics()
4848

4949
// sort entries by name it easier to read the output
@@ -56,5 +56,5 @@ func Handler(w http.ResponseWriter, _ *http.Request) {
5656
marshableMetrics[i] = MarshableMetric{m}
5757
}
5858

59-
utils.WriteAsJSON(w, marshableMetrics)
59+
utils.WriteAsJSON(w, marshableMetrics, utils.GetPrettyPrintFromQueryParams(req))
6060
}

pkg/network/usm/utils/debugger.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ type PathIdentifierWithSamplePath struct {
5252
// generates a summary of all active uprobe-based programs along with their file paths and PIDs.
5353
// This is used for debugging purposes only.
5454
func GetTracedProgramsEndpoint(moduleName string) func(http.ResponseWriter, *http.Request) {
55-
return func(w http.ResponseWriter, _ *http.Request) {
56-
otherutils.WriteAsJSON(w, debugger.GetTracedPrograms(moduleName))
55+
return func(w http.ResponseWriter, req *http.Request) {
56+
otherutils.WriteAsJSON(w, debugger.GetTracedPrograms(moduleName), otherutils.GetPrettyPrintFromQueryParams(req))
5757
}
5858
}
5959

@@ -62,8 +62,8 @@ func GetTracedProgramsEndpoint(moduleName string) func(http.ResponseWriter, *htt
6262
// registry along with their device and inode numbers, and sample path.
6363
// This is used for debugging purposes only.
6464
func GetBlockedPathIDEndpoint(moduleName string) func(http.ResponseWriter, *http.Request) {
65-
return func(w http.ResponseWriter, _ *http.Request) {
66-
otherutils.WriteAsJSON(w, debugger.GetAllBlockedPathIDs(moduleName))
65+
return func(w http.ResponseWriter, req *http.Request) {
66+
otherutils.WriteAsJSON(w, debugger.GetAllBlockedPathIDs(moduleName), otherutils.GetPrettyPrintFromQueryParams(req))
6767
}
6868
}
6969

pkg/system-probe/utils/utils.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,54 @@ package utils
88
import (
99
"encoding/json"
1010
"net/http"
11+
"strings"
1112

1213
"github.com/DataDog/datadog-agent/pkg/util/log"
1314
)
1415

16+
// FormatOptions represents formatting options for WriteAsJson.
17+
type FormatOptions bool
18+
19+
const (
20+
// CompactOutput indicates that the output should be compact (no indentation).
21+
CompactOutput = false
22+
// PrettyPrint indicates that the output should be pretty-printed (with indentation).
23+
PrettyPrint = true
24+
)
25+
26+
func isTruthy(s string) bool {
27+
switch strings.ToLower(strings.TrimSpace(s)) {
28+
case "1", "t", "true", "yes", "y", "on":
29+
return true
30+
default:
31+
return false
32+
}
33+
}
34+
35+
const (
36+
// PrettyPrintQueryParam is the query parameter used to request pretty-printed JSON output
37+
PrettyPrintQueryParam = "pretty_print"
38+
)
39+
40+
// GetPrettyPrintFromQueryParams returns true if the pretty_print query parameter is set to "true" in the request URL
41+
func GetPrettyPrintFromQueryParams(req *http.Request) FormatOptions {
42+
if prettyPrint := req.URL.Query().Get(PrettyPrintQueryParam); isTruthy(prettyPrint) {
43+
return PrettyPrint
44+
}
45+
return CompactOutput
46+
}
47+
1548
// WriteAsJSON marshals the give data argument into JSON and writes it to the `http.ResponseWriter`
16-
func WriteAsJSON(w http.ResponseWriter, data interface{}) {
17-
buf, err := json.Marshal(data)
49+
func WriteAsJSON(w http.ResponseWriter, data interface{}, outputOptions FormatOptions) {
50+
var buf []byte
51+
var err error
52+
// Keeping the condition explicit for readability
53+
//nolint:gosimple
54+
if outputOptions == PrettyPrint {
55+
buf, err = json.MarshalIndent(data, "", " ")
56+
} else {
57+
buf, err = json.Marshal(data)
58+
}
1859
if err != nil {
1960
log.Errorf("unable to marshal data into JSON: %s", err)
2061
w.WriteHeader(500)

0 commit comments

Comments
 (0)