Skip to content

Conversation

@asteurer
Copy link

@asteurer asteurer commented Dec 12, 2025

Overview

This is a feature that enables the collection and emission of CPU and memory usage for each component.
Closes #2933

Notes about the collected metrics

This measures 3 things:

  • Memory usage from component instantiation
  • Memory usage from component execution
  • The total CPU time elapsed actively running guest code in this instance.

Although it was proposed to measure CPU cycles, I think that that measuring execution time is sufficient for now.

Usage

Prerequisites

Generate usage metrics

cd spin-component-metrics-example
spin otel setup
spin otel open grafana
spin build
spin otel up

# In a different terminal
curl localhost:3000

Copy link
Collaborator

@lann lann left a comment

Choose a reason for hiding this comment

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

Looking good overall; some mostly-superficial suggestions.

I wonder if we should enable this by default for spin up; there is a small performance penalty but I'm not sure anyone will actually use this otherwise... 🤔

…sage

Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com>
@asteurer
Copy link
Author

asteurer commented Dec 12, 2025

Benchmarking

I used the oha tool to benchmark. The performance differences seem to be small enough to justify including the cpu-time-metrics flag by default.

Spin v3.5.1

  • Requests: 10,000
  • Connections: 1

Summary

Success rate: 100.00%
Total: 3567.8610 ms
Slowest: 2.9617 ms
Fastest: 0.3008 ms
Average: 0.3454 ms
Requests/sec: 2802.7998

Spin v3.6.0-pre0 with cpu-time-metrics flag

  • Requests: 10,000
  • Connections: 1

Summary

Success rate: 100.00%
Total: 3699.8777 ms
Slowest: 3.2799 ms
Fastest: 0.3153 ms
Average: 0.3586 ms
Requests/sec: 2702.7

@asteurer asteurer requested a review from lann December 12, 2025 16:58
@lann
Copy link
Collaborator

lann commented Dec 12, 2025

Would you mind doing one more comparison just between the branch as-is and with the lines that set up the call hook commented out? If most of the overhead is in actually running the call hook then we could maybe have the feature enabled by default but still opt-in at runtime.

@asteurer
Copy link
Author

Benchmarking Part II

With the lines that set up the call hook

  • Requests: 10,000
  • Connections: 1

Summary

Success rate: 100.00%
Total: 3744.0815 ms
Slowest: 2.9720 ms
Fastest: 0.3169 ms
Average: 0.3622 ms
Requests/sec: 2670.8820

Without the lines that set up the call hook

  • Requests: 10,000
  • Connections: 1

Summary

Success rate: 100.00%
Total: 3616.5206 ms
Slowest: 2.9669 ms
Fastest: 0.3033 ms
Average: 0.3499 ms
Requests/sec: 2765.0886

@lann
Copy link
Collaborator

lann commented Dec 12, 2025

OK not exactly scientific but it seems like the bulk of the latency comes from actually running the call hook. I think it probably makes sense to enable the cargo feature by default for spin but require opt-in to actually enable the call hook. I'd be fine with doing that in this PR or a followup; up to you.

@asteurer
Copy link
Author

Yeah, I would prefer to tackle the runtime flag in another PR

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose CPU and memory usage on per-component execution spans

2 participants