sdk/metric: refactor expoBuckets to fixed-position circular storage (#9277)

Part of https://github.com/open-telemetry/opentelemetry-go/issues/7796

This is the first follow-up step after #8077 toward lockless exponential
histogram bucket recording (split out from prototype #7535).

### Changes

- **Fixed-Position Circular Buffer (`expoBuckets`)**:
- Replaces dynamic slice prepend/append shifting in `expoBuckets` with a
power-of-2 circular buffer (`counts []atomic.Uint64`) indexed via
bitwise mask (`int(bin) & (len(counts) - 1)`), tracking the logical
range via `[startBin, startBin + length - 1]` with `at(i)`, `bin(bin)`,
and `endBin()` accessors.
- Eliminates $O(N)$ element shifts when measurements expand the bucket
range toward smaller bins (`bin < startBin`), making in-capacity range
expansion $O(1)$ in both directions.
- Preserves the invariant that all physical slots outside `[startBin,
startBin + length - 1]` are zero across capacity doubling, `downscale`,
and `clear`.
- Preserves #8077's behavior of retaining the allocated `counts` backing
slice across `clear()` calls for cumulative cold-buffer
(`cp.points[coldIdx]`) reuse.
- **Zero-Allocation Default Downscaling**:
- Uses a stack-allocated scratch buffer (`var stackScratch
[defaultMaxSize/2 + 1]uint64`) in `downscale` to avoid aliasing when
remapping wrapped circular slots without heap allocations for the
default `maxSize = 160`.
- **Memory Trade-offs**:
- **Expected memory overhead in normal use**: **0%** (when buckets grow
across multiple measurements, Go's `append` in `main` already doubles
the slice capacity from 128 to 256 slots = `2,048 B`, identical to
`nextPowerOf2`).
- **Worst-case memory overhead under default settings (`maxSize =
160`)**: **~38%–64% higher memory per affected data point** (up to `+896
B` per bucket: `2,048 B` vs. `1,152–1,408 B` in `main`), which only
occurs if a bucket's first two measurements jump directly to a 129–160
bin span in a single step without intermediate growth.
- **Worst-case memory overhead with custom `maxSize > 256`**: **up to
~60%–100% higher bucket slice memory** when the active span lands just
above a power of 2 (since `nextPowerOf2` doubles capacity whereas Go's
`append` tapers to `~1.25x` growth above 256 elements).
- **Benchmarks & Tests**:
- Updates `BenchmarkPrepend` and `BenchmarkAppend` to use the default
`maxSize` (`160`) and adds unit and property tests covering wraparound,
capacity growth, wrapped downscaling, wrapped merges, and `clear()`
capacity retention.

### Benchmarks

Comparing `BenchmarkPrepend`, `BenchmarkAppend` (both at `maxSize =
160`), and `BenchmarkExponentialHistogram` (`-count=6
-benchtime=200ms`):

```text
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/sdk/metric/internal/aggregate
cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
                                                                │   base.txt    │               new.txt               │
                                                                │    sec/op     │    sec/op     vs base               │
Prepend-24                                                        257.13µ ±  7%   21.87µ ±  6%  -91.49% (p=0.002 n=6)
Append-24                                                          57.14µ ±  9%   23.93µ ±  7%  -58.12% (p=0.002 n=6)
ExponentialHistogram/Int64/Cumulative/1/Measure-24                 234.4n ± 12%   201.1n ±  9%  -14.17% (p=0.002 n=6)
ExponentialHistogram/Int64/Cumulative/1/ComputeAggregation-24     1176.0n ±  8%   912.6n ± 15%  -22.40% (p=0.002 n=6)
ExponentialHistogram/Int64/Cumulative/10/Measure-24                2.505µ ± 10%   1.979µ ± 10%  -20.98% (p=0.002 n=6)
ExponentialHistogram/Int64/Cumulative/10/ComputeAggregation-24     8.093µ ± 20%   6.839µ ± 15%        ~ (p=0.065 n=6)
ExponentialHistogram/Int64/Cumulative/100/Measure-24               27.28µ ± 14%   19.90µ ± 21%  -27.07% (p=0.004 n=6)
ExponentialHistogram/Int64/Cumulative/100/ComputeAggregation-24    90.37µ ± 15%   71.32µ ± 19%  -21.09% (p=0.009 n=6)
ExponentialHistogram/Int64/Delta/1/Measure-24                      241.2n ±  8%   170.3n ± 11%  -29.39% (p=0.002 n=6)
ExponentialHistogram/Int64/Delta/1/ComputeAggregation-24           814.9n ±  8%   644.5n ± 14%  -20.90% (p=0.002 n=6)
ExponentialHistogram/Int64/Delta/10/Measure-24                     1.949µ ± 13%   1.916µ ±  7%        ~ (p=0.937 n=6)
ExponentialHistogram/Int64/Delta/10/ComputeAggregation-24          3.265µ ± 17%   2.620µ ± 14%  -19.75% (p=0.009 n=6)
ExponentialHistogram/Int64/Delta/100/Measure-24                    25.52µ ± 14%   20.94µ ± 10%  -17.94% (p=0.002 n=6)
ExponentialHistogram/Int64/Delta/100/ComputeAggregation-24         32.60µ ±  9%   23.59µ ± 26%  -27.64% (p=0.002 n=6)
geomean                                                            6.439µ         4.183µ        -35.04%

                                                                │    base.txt     │                new.txt                 │
                                                                │      B/op       │     B/op       vs base                 │
Prepend-24                                                        3.602Ki ±  0%     2.227Ki ±  0%  -38.18% (p=0.002 n=6)
Append-24                                                         4.727Ki ±  0%     2.227Ki ±  0%  -52.89% (p=0.002 n=6)
ExponentialHistogram/Int64/Cumulative/1/Measure-24                  0.000 ±  0%       0.000 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/1/ComputeAggregation-24       40.00 ±  0%       40.00 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/10/Measure-24                 0.000 ±  0%       0.000 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/10/ComputeAggregation-24      112.0 ±  0%       112.0 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/100/Measure-24                9.000 ± 22%       7.000 ± 14%  -22.22% (p=0.006 n=6)
ExponentialHistogram/Int64/Cumulative/100/ComputeAggregation-24     832.0 ±  0%       832.0 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/1/Measure-24                       0.000 ±  0%       0.000 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/1/ComputeAggregation-24            192.0 ±  0%       192.0 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/10/Measure-24                      0.000 ±  0%       0.000 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/10/ComputeAggregation-24           192.0 ±  0%       192.0 ±  0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/100/Measure-24                     3.000 ± 33%       3.000 ± 33%        ~ (p=0.273 n=6)
ExponentialHistogram/Int64/Delta/100/ComputeAggregation-24          192.0 ±  0%       192.0 ±  0%        ~ (p=1.000 n=6) ¹
geomean                                                                         ²                  -10.06%               ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                                                │   base.txt   │               new.txt               │
                                                                │  allocs/op   │ allocs/op   vs base                 │
Prepend-24                                                        4.000 ± 0%     3.000 ± 0%  -25.00% (p=0.002 n=6)
Append-24                                                         6.000 ± 0%     3.000 ± 0%  -50.00% (p=0.002 n=6)
ExponentialHistogram/Int64/Cumulative/1/Measure-24                0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/1/ComputeAggregation-24     2.000 ± 0%     2.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/10/Measure-24               0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/10/ComputeAggregation-24    11.00 ± 0%     11.00 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/100/Measure-24              0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Cumulative/100/ComputeAggregation-24   101.0 ± 0%     101.0 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/1/Measure-24                     0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/1/ComputeAggregation-24          2.000 ± 0%     2.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/10/Measure-24                    0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/10/ComputeAggregation-24         2.000 ± 0%     2.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/100/Measure-24                   0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=6) ¹
ExponentialHistogram/Int64/Delta/100/ComputeAggregation-24        2.000 ± 0%     2.000 ± 0%        ~ (p=1.000 n=6) ¹
geomean                                                                      ²                -6.77%               ²
¹ all samples are equal
² summaries must be >0 to compute geomean
```
3 files changed
tree: 65636741b16c7a4c71868f60aee487460c45eb8a
  1. .github/
  2. attribute/
  3. baggage/
  4. bridge/
  5. codes/
  6. exporters/
  7. internal/
  8. log/
  9. metric/
  10. propagation/
  11. schema/
  12. sdk/
  13. semconv/
  14. trace/
  15. .clomonitor.yml
  16. .codespellignore
  17. .codespellrc
  18. .gitattributes
  19. .gitignore
  20. .golangci.yml
  21. .lycheeignore
  22. .markdownlint.yaml
  23. AGENTS.md
  24. CHANGELOG.md
  25. CODEOWNERS
  26. CONTRIBUTING.md
  27. dependencies.Dockerfile
  28. doc.go
  29. error_handler.go
  30. go.mod
  31. go.sum
  32. handler.go
  33. handler_test.go
  34. internal_logging.go
  35. internal_logging_test.go
  36. LICENSE
  37. log.go
  38. log_test.go
  39. Makefile
  40. metric.go
  41. metric_test.go
  42. propagation.go
  43. README.md
  44. RELEASING.md
  45. renovate.json
  46. requirements.txt
  47. SECURITY-INSIGHTS.yml
  48. trace.go
  49. trace_test.go
  50. verify_internal_boundary_compat.sh
  51. verify_released_changelog.sh
  52. version.go
  53. version_test.go
  54. VERSIONING.md
  55. versions.yaml
README.md

OpenTelemetry-Go

ci codecov.io PkgGoDev OpenSSF Scorecard OpenSSF Best Practices Fuzzing Status FOSSA Status Slack

OpenTelemetry-Go is the Go implementation of OpenTelemetry. It provides a set of APIs to directly measure performance and behavior of your software and send this data to observability platforms.

Project Status

SignalStatus
TracesStable
MetricsStable
LogsStable

Progress and status specific to this repository is tracked in our project boards and milestones.

Project versioning information and stability guarantees can be found in the versioning documentation.

Compatibility

OpenTelemetry-Go ensures compatibility with the current supported versions of the Go language:

Each major Go release is supported until there are two newer major releases. For example, Go 1.5 was supported until the Go 1.7 release, and Go 1.6 was supported until the Go 1.8 release.

For versions of Go that are no longer supported upstream, opentelemetry-go will stop ensuring compatibility with these versions in the following manner:

  • A minor release of opentelemetry-go will be made to add support for the new supported release of Go.
  • The following minor release of opentelemetry-go will remove compatibility testing for the oldest (now archived upstream) version of Go. This, and future, releases of opentelemetry-go may include features only supported by the currently supported versions of Go.

Currently, this project supports the following environments.

OSGo VersionArchitecture
Ubuntu1.27amd64
Ubuntu1.26amd64
Ubuntu1.27386
Ubuntu1.26386
Ubuntu1.27arm64
Ubuntu1.26arm64
macOS1.27amd64
macOS1.26amd64
macOS1.27arm64
macOS1.26arm64
Windows1.27amd64
Windows1.26amd64
Windows1.27386
Windows1.26386

While this project should work for other systems, no compatibility guarantees are made for those systems currently.

Getting Started

You can find a getting started guide on opentelemetry.io.

OpenTelemetry's goal is to provide a single set of APIs to capture distributed traces and metrics from your application and send them to an observability platform. This project allows you to do just that for applications written in Go. There are two steps to this process: instrument your application, and configure an exporter.

Instrumentation

To start capturing distributed traces and metric events from your application it first needs to be instrumented. The easiest way to do this is by using an instrumentation library for your code. Be sure to check out the officially supported instrumentation libraries.

If you need to extend the telemetry an instrumentation library provides or want to build your own instrumentation for your application directly you will need to use the Go otel package. The examples are a good way to see some practical uses of this process.

Export

Now that your application is instrumented to collect telemetry, it needs an export pipeline to send that telemetry to an observability platform.

All officially supported exporters for the OpenTelemetry project are contained in the exporters directory.

ExporterLogsMetricsTraces
OTLP✓✓✓
Prometheus✓
stdout✓✓✓
Zipkin✓

Contributing

See the contributing documentation.