Skip to content

refactor: diagnostics server — replace manual JSON write! with serde_json #2627

Open

Description

@strawgate

Refactor

logfwd-diagnostics/server.rs has two manually-built JSON structures:

  1. build_traces_body (line 746) — writes to String with_capacity(64*1024) and manual write! calls
  2. build_logs_body (line 954-972) — same pattern

The codebase uses serde_json everywhere else. These manual constructions are:

  • Error-prone (JSON escaping edge cases)
  • Hard to maintain (add a field, update the write! chain)
  • Inconsistent with the rest of the codebase

Fix

Replace with serde_json::to_string or a serde_json::Serializer. The overhead of a temporary Vec is negligible in an HTTP response handler.

Related

Activity

  1. added
    enhancementNew feature or request
    P2: mediumMinor bug, missing validation, metric inaccuracy
    component:diagnosticsDiagnostics endpoints, dashboard, health, probes, and trace explorer
    on Apr 25, 2026
  2. strawgate commented on Apr 27, 2026

    @strawgate
    OwnerAuthor

    Verification (2026-04-27)

    CONFIRMED: Issue still exists on main.

    Verified on current main:

    • ffwd-diagnostics/src/diagnostics/server.rs:737 - build_traces_body() still uses manual String + write!
    • ffwd-diagnostics/src/diagnostics/server.rs:964 - build_logs_body() same pattern

    Current Code:

    fn build_traces_body(state: &DiagnosticsState) -> String {
        // ...
        let mut out = String::with_capacity(64 * 1024);
        out.push_str(r#"{"traces":["#);
        // ... manual write! chain continues
    }

    Reproduction:

    No test needed - code is clearly visible at lines 737-900+ and 964+

    Fix Plan:

    1. Replace manual JSON with serde_json::to_string()
    2. Create struct for traces response:
    #[derive(Serialize)]
    struct TracesResponse {
        traces: Vec<TraceData>,
    }
    1. Use serde_json::to_string(&response)

    Priority: MEDIUM - Technical debt, but low risk


    Verified by code inspection of server.rs:737 and 964

  3. strawgate commented on Apr 27, 2026

    @strawgate
    OwnerAuthor

    Verification (2026-04-27)

    STATUS: CONFIRMED OPEN

    Manual JSON construction found in ffwd-diagnostics/src/diagnostics/server.rs:

    • Line 758: build_traces_body uses out.push_str(r#"{"traces":["#)
    • Line 844, 914: write! macros for JSON string building
    • Line 967: build_lines_body uses body.push_str(r#"{"lines":["#)
    • Line 977: manual string building for stderr state

    42 serde_json usages found, but these manual string operations are error-prone and hard to maintain.

    Fix needed:

    Replace manual write!/push_str JSON building with serde_json serialization to struct types.

    Priority: MEDIUM (code quality, maintainability)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2: mediumMinor bug, missing validation, metric inaccuracycomponent:diagnosticsDiagnostics endpoints, dashboard, health, probes, and trace explorerenhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions