Skip to content

Optimisation: Add a SpanData dto to remove the resource field sent to the user - #1808

Merged
lbroudoux merged 13 commits into
microcks:1.13.xfrom
StartUpNationLabs:opti/remove-resource-field-from-span
Nov 21, 2025
Merged

lbroudoux merged 13 commits into
microcks:1.13.xfrom
StartUpNationLabs:opti/remove-resource-field-from-span

Conversation

@Apoorva64

@Apoorva64 Apoorva64 commented Oct 26, 2025 •

Copy link
Copy Markdown
Contributor

See #1807

Description

This PR creates a DTO of the SpanData to omit the repetitive resource field

A Span now looks like

    {
      "name": "computeDispatchCriteria",
      "attributes": {
        "explain-trace": true
      },
      "events": [
        {
          "name": "dispatch_criteria_computed",
          "attributes": {
            "data": [
              {
                "type": "STRING",
                "key": "dispatch.criteria",
                "keyUtf8": "ZGlzcGF0Y2guY3JpdGVyaWE="
              },
              "/name=Millefeuille",
              {
                "type": "STRING",
                "key": "dispatcher",
                "keyUtf8": "ZGlzcGF0Y2hlcg=="
              },
              "URI_PARTS",
              {
                "type": "STRING",
                "key": "dispatcher.rules",
                "keyUtf8": "ZGlzcGF0Y2hlci5ydWxlcw=="
              },
              "name",
              {
                "type": "STRING",
                "key": "message",
                "keyUtf8": "bWVzc2FnZQ=="
              },
              "Computed dispatch criteria using URI_PARTS dispatcher"
            ],
            "empty": false
          },
          "epochNanos": 1.761478323771033e+18,
          "totalAttributeCount": 4,
          "droppedAttributesCount": 0
        }
      ],
      "links": [],
      "spanContext": {
        "traceId": "f0403307cd19ec6a68246cd94a5c9f62",
        "spanId": "5ceb202ad4914bc2",
        "traceFlags": {
          "sampled": true
        },
        "traceState": {
          "empty": true
        },
        "remote": false,
        "valid": true,
        "sampled": true,
        "traceIdBytes": "8EAzB80Z7GpoJGzZSlyfYg==",
        "spanIdBytes": "XOsgKtSRS8I="
      },
      "kind": "INTERNAL",
      "status": {
        "statusCode": "UNSET",
        "description": ""
      },
      "spanId": "5ceb202ad4914bc2",
      "traceId": "f0403307cd19ec6a68246cd94a5c9f62",
      "instrumentationScopeInfo": {
        "name": "io.github.microcks.web.RestInvocationProcessor",
        "attributes": {
          "data": [],
          "empty": true
        }
      },
      "totalRecordedLinks": 0,
      "endEpochNanos": 1.7614783237710418e+18,
      "totalRecordedEvents": 1,
      "parentSpanId": "1cba8440104ed47c",
      "startEpochNanos": 1.76147832377088e+18,
      "parentSpanContext": {
        "traceId": "f0403307cd19ec6a68246cd94a5c9f62",
        "spanId": "1cba8440104ed47c",
        "traceFlags": {
          "sampled": true
        },
        "traceState": {
          "empty": true
        },
        "remote": false,
        "valid": true,
        "sampled": true,
        "traceIdBytes": "8EAzB80Z7GpoJGzZSlyfYg==",
        "spanIdBytes": "HLqEQBBO1Hw="
      },
      "totalAttributeCount": 1,
      "instrumentationLibraryInfo": {
        "name": "io.github.microcks.web.RestInvocationProcessor"
      }
    },

The size has been reduce by a factor of 2

Related issue(s)

#1807 and #1823

@Apoorva64 Apoorva64 changed the title feat: add dto to remove the resource field sent to the user Optimisation: Add a SpanData dto to remove the resource field sent to the user Oct 26, 2025
@Apoorva64
Apoorva64 marked this pull request as draft November 5, 2025 14:59
Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>
Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>
Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>
Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>
@Apoorva64

Apoorva64 commented Nov 11, 2025 •

Copy link
Copy Markdown
Contributor Author

1.13.0
before bench:
image

after bench (filled):
image

Current branch

before bench:
image

after bench (filled):
image

The change does not seem to have the expected memory reduction

The benchmark that was used:

import http from 'k6/http';
import { check, sleep } from 'k6';

export let options = {
  // total number of requests (one request per iteration)
  iterations: 10000,

  // concurrency: number of virtual users running in parallel.
  // Increase for higher concurrency, decrease to throttle.
  vus:  20,

  // optional thresholds to fail the test if too many errors/slow responses
  thresholds: {
    'http_req_failed': ['rate<0.01'], // <1% errors allowed
    'http_req_duration': ['p(95)<1000'] // 95% requests < 1000ms
  }
};

// target URL
const URL = 'http://localhost:8080/rest/FIWARE-NGSI+v2/1.0/v2';

export default function () {
  const res = http.get(URL, {
    headers: {
      'Accept': 'application/json',
    }
  });

  check(res, {
    'status is 2xx': (r) => r.status >= 200 && r.status < 300,
  });

  // small sleep to avoid hammering too tightly; remove or change if you want max throughput
  sleep(0.1);
}

I think there must be some java black magic somewhere

@lbroudoux

Copy link
Copy Markdown
Member

Hello,

I've built and run a micro-benchmark on my side and see the effects of not saving the resource attribute of the span.

So basically, the process is as follow:

  1. Start a test with fixed memory ergonomics (-Xms and -Xmx)
  2. Run the GC multiple times (3) to be sure we evict short-lived objects from the heap
  3. Save the Runtime.getRuntime().freeMemory() as "start free mem"
  4. Build and store 60000 spans (with or without resource attributes)
  5. Run the GC multiple times (3) to be sure we evict short-lived objects from the heap
  6. Save the Runtime.getRuntime().freeMemory() as "end free mem"
  7. Deduced memory used for storing the spans

Here are the results I get:

  • With resource attributes - time: 30 ms, mem used: 10075 KB
  • Without resource attributes - time: 34 ms, mem used: 6175 KB

So there's a little time penalty in filtering the span (re-creating and copying to another object to do so) but there's a nice memory usage enhancement here.

Note that for this test, I didn't change the implementation of the spansByTraceId but just put synchronized blocks on iterations (not unitary method calls).

Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>
Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>
Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>
@Apoorva64

Copy link
Copy Markdown
Contributor Author

@lbroudoux i have removed the unnecessary synchronized blocks. I also removed the method queryTraceIdsBySpanAttributes as it wasn't used.

Btw: What tool did you use to setup the test? For exemple to trigger the GC?

@Apoorva64
Apoorva64 marked this pull request as ready for review November 21, 2025 10:43
@lbroudoux

Copy link
Copy Markdown
Member

Btw: What tool did you use to setup the test? For exemple to trigger the GC?

IT was all hand-written in pure Java ;-) And it just calls System.gc() multiple times to guarantee objects move from one heap space to another, and finally become evicted and memory reclaimed.

@lbroudoux

Copy link
Copy Markdown
Member

@Apoorva64 For some reason I don't explain, I had some comments that were still in a pending review... I think you can remove even more synchronized blocks or switch back to a simple unitary command that makes them unnecessary. Can you check again please?

BTW Sorry for the trouble.

@lbroudoux lbroudoux added this to the 1.13.1 milestone Nov 21, 2025
@lbroudoux lbroudoux added component/mocks kind/bug java Pull requests that update Java code labels Nov 21, 2025
Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>
Signed-off-by: Apoorva Srinivas Appadoo <[email protected]>

@lbroudoux lbroudoux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Apoorva64

Copy link
Copy Markdown
Contributor Author

@lbroudoux thanks a lots for taking your time to review and giving advice on this PR, I learned a lot about java and memory management xD

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants