Skip to content

Reduce recordMetric allocations - #550

Merged
XSAM merged 4 commits into
XSAM:mainfrom
boekkooi-impossiblecloud:record-metrics-tweaks
Oct 23, 2025
Merged

Reduce recordMetric allocations#550
XSAM merged 4 commits into
XSAM:mainfrom
boekkooi-impossiblecloud:record-metrics-tweaks

Conversation

@boekkooi-impossiblecloud

Copy link
Copy Markdown
Contributor

Good day!

This PR improves the performance of the recordMetric func by reducing the amount of allocations being done.

Thanks for reviewing and maintaining the library and have a great day!


The result is the following benchstat report.

goos: linux
goarch: amd64
pkg: github.com/XSAM/otelsql
cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics     
                                              │ old-record.txt │           new-record.txt            │
                                              │     sec/op     │   sec/op     vs base                │
RecordMetric/InstrumentAttributesGetter/5-16      431.4n ±  6%   300.6n ± 9%  -30.32% (p=0.000 n=10)
RecordMetric/InstrumentAttributesGetter/10-16    1333.0n ± 10%   991.4n ± 7%  -25.63% (p=0.000 n=10)
CreateSpan/AttributesGetter/5-16                  263.8n ±  6%   260.0n ± 4%        ~ (p=0.669 n=10)
CreateSpan/AttributesGetter/10-16                 518.7n ±  3%   510.9n ± 7%        ~ (p=0.579 n=10)
geomean                                           529.6n         446.1n       -15.78%

                                              │ old-record.txt │             new-record.txt             │
                                              │      B/op      │     B/op      vs base                  │
RecordMetric/InstrumentAttributesGetter/5-16       1248.0 ± 0%     864.0 ± 0%  -30.77% (p=0.000 n=10)
RecordMetric/InstrumentAttributesGetter/10-16     3.719Ki ± 0%   2.406Ki ± 0%  -35.29% (p=0.000 n=10)
CreateSpan/AttributesGetter/5-16                    824.0 ± 0%     824.0 ± 0%        ~ (p=1.000 n=10) ¹
CreateSpan/AttributesGetter/10-16                 1.805Ki ± 0%   1.805Ki ± 0%        ~ (p=1.000 n=10) ¹
geomean                                           1.602Ki        1.310Ki       -18.19%
¹ all samples are equal

                                              │ old-record.txt │            new-record.txt            │
                                              │   allocs/op    │ allocs/op   vs base                  │
RecordMetric/InstrumentAttributesGetter/5-16        5.000 ± 0%   4.000 ± 0%  -20.00% (p=0.000 n=10)
RecordMetric/InstrumentAttributesGetter/10-16       7.000 ± 0%   5.000 ± 0%  -28.57% (p=0.000 n=10)
CreateSpan/AttributesGetter/5-16                    8.000 ± 0%   8.000 ± 0%        ~ (p=1.000 n=10) ¹
CreateSpan/AttributesGetter/10-16                   9.000 ± 0%   9.000 ± 0%        ~ (p=1.000 n=10) ¹
geomean                                             7.085        6.160       -13.06%
¹ all samples are equal

Benchmarks where run using go test -run '^$' -bench . -count 10

Before (7ca8256) ``` goos: linux goarch: amd64 pkg: github.com/XSAM/otelsql cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 3478968 388.2 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2576029 404.7 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2980551 425.8 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2988195 416.3 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2841171 430.5 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2625727 434.1 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2739985 432.3 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2739862 456.1 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2583672 460.2 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 2936508 433.2 ns/op 1248 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 916376 1330 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 967496 1329 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 843298 1370 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 946387 1272 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 970816 1585 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 938580 1336 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 990020 1302 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 859711 1358 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 952374 1301 ns/op 3808 B/op 7 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 891291 1460 ns/op 3808 B/op 7 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4768020 245.0 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4813402 267.0 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4592091 254.7 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4528048 270.0 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4833096 248.3 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4665340 276.3 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4903068 260.5 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4998130 269.5 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4446578 259.9 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4621431 270.4 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2277591 507.8 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2298198 540.1 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2350687 528.1 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2304780 531.4 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2445901 535.0 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2313202 501.3 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2361778 518.8 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2466976 505.9 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2155660 516.0 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2485371 518.6 ns/op 1848 B/op 9 allocs/op PASS ok github.com/XSAM/otelsql 61.655s ```
After (eec0d05) ``` goos: linux goarch: amd64 pkg: github.com/XSAM/otelsql cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 4389051 255.5 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 4510778 295.7 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 4080192 274.8 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 3928474 327.7 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 3772546 300.0 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 3317569 315.1 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 4186486 290.7 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 3900781 338.8 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 3985352 301.2 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/5-16 3302559 314.3 ns/op 864 B/op 4 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1345795 943.2 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1000000 1025 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1367355 948.7 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1214510 926.3 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1000000 1023 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1263034 887.6 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1000000 1095 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 999145 1002 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1272502 1001 ns/op 2464 B/op 5 allocs/op BenchmarkRecordMetric/InstrumentAttributesGetter/10-16 1330045 981.8 ns/op 2464 B/op 5 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4377711 269.4 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4913751 255.4 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4558237 262.3 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4593020 263.5 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4131170 257.7 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4871282 253.5 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4422524 279.8 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4552953 253.5 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4324983 262.9 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/5-16 4956482 257.1 ns/op 824 B/op 8 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2204943 545.3 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2431120 515.3 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2239924 497.3 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2321678 541.3 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2406284 487.6 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2315420 551.2 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2245003 503.1 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2214315 506.5 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2494950 524.0 ns/op 1848 B/op 9 allocs/op BenchmarkCreateSpan/AttributesGetter/10-16 2107402 497.8 ns/op 1848 B/op 9 allocs/op PASS ok github.com/XSAM/otelsql 65.040s ```

@codecov

codecov Bot commented Oct 16, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.1%. Comparing base (111e537) to head (66f0e2e).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
utils.go 77.7% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main    #550     +/-   ##
=======================================
- Coverage   85.6%   79.1%   -6.6%     
=======================================
  Files         16      16             
  Lines        928     968     +40     
=======================================
- Hits         795     766     -29     
- Misses       107     145     +38     
- Partials      26      57     +31     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@boekkooi-impossiblecloud

boekkooi-impossiblecloud commented Oct 20, 2025

Copy link
Copy Markdown
Contributor Author

I rebased the PR as #549 was merged which cause estimatedAttributesOfGettersCount to become unused.