|
| 1 | +// Command otlp_dump is a tiny, dependency-free OTLP/HTTP profiles receiver that |
| 2 | +// writes each received export request verbatim to a ".otlp" file. |
| 3 | +// |
| 4 | +// The datadog-agent host-profiler only exports profiles over OTLP (no local |
| 5 | +// file/pprof exporter). The prof-correctness analyzer reads the OTLP format |
| 6 | +// natively (analysis/otlp.go), so this sidecar does not parse the payload - it |
| 7 | +// just persists the raw protobuf bytes so the analyzer can read them from |
| 8 | +// /app/data. |
| 9 | +// |
| 10 | +// otlphttpexporter POSTs the (unstable) profiles signal to |
| 11 | +// "<endpoint>/v1development/profiles" as protobuf, optionally gzip-encoded. |
| 12 | +package main |
| 13 | + |
| 14 | +import ( |
| 15 | + "compress/gzip" |
| 16 | + "fmt" |
| 17 | + "io" |
| 18 | + "net/http" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "sync/atomic" |
| 22 | +) |
| 23 | + |
| 24 | +var seq atomic.Uint64 |
| 25 | + |
| 26 | +func writeDump(outDir string, w http.ResponseWriter, r *http.Request) { |
| 27 | + if r.Method != http.MethodPost { |
| 28 | + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + var body io.Reader = r.Body |
| 33 | + if r.Header.Get("Content-Encoding") == "gzip" { |
| 34 | + gz, err := gzip.NewReader(r.Body) |
| 35 | + if err != nil { |
| 36 | + http.Error(w, "bad gzip", http.StatusBadRequest) |
| 37 | + return |
| 38 | + } |
| 39 | + defer gz.Close() |
| 40 | + body = gz |
| 41 | + } |
| 42 | + |
| 43 | + data, err := io.ReadAll(body) |
| 44 | + if err != nil { |
| 45 | + http.Error(w, "read error", http.StatusBadRequest) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + n := seq.Add(1) |
| 50 | + name := fmt.Sprintf("profiles_%03d.otlp", n) |
| 51 | + if err := os.WriteFile(filepath.Join(outDir, name), data, 0o644); err != nil { |
| 52 | + fmt.Fprintf(os.Stderr, "otlp_dump: write %s: %v\n", name, err) |
| 53 | + http.Error(w, "write error", http.StatusInternalServerError) |
| 54 | + return |
| 55 | + } |
| 56 | + fmt.Printf("otlp_dump: wrote %s (%d bytes)\n", name, len(data)) |
| 57 | + |
| 58 | + // An empty body is a valid empty ExportProfilesServiceResponse. |
| 59 | + w.Header().Set("Content-Type", "application/x-protobuf") |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | +} |
| 62 | + |
| 63 | +func main() { |
| 64 | + addr := os.Getenv("SINK_ADDR") |
| 65 | + if addr == "" { |
| 66 | + addr = "0.0.0.0:4318" |
| 67 | + } |
| 68 | + outDir := os.Getenv("SINK_OUT_DIR") |
| 69 | + if outDir == "" { |
| 70 | + outDir = "/app/data" |
| 71 | + } |
| 72 | + if err := os.MkdirAll(outDir, 0o755); err != nil { |
| 73 | + fmt.Fprintf(os.Stderr, "otlp_dump: mkdir %s: %v\n", outDir, err) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + |
| 77 | + h := func(w http.ResponseWriter, r *http.Request) { writeDump(outDir, w, r) } |
| 78 | + mux := http.NewServeMux() |
| 79 | + mux.HandleFunc("/v1development/profiles", h) |
| 80 | + mux.HandleFunc("/v1/profiles", h) // tolerate a signal-version bump |
| 81 | + |
| 82 | + fmt.Printf("otlp_dump listening on %s, writing .otlp to %s\n", addr, outDir) |
| 83 | + if err := http.ListenAndServe(addr, mux); err != nil { |
| 84 | + fmt.Fprintf(os.Stderr, "otlp_dump: serve: %v\n", err) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | +} |
0 commit comments