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 ```
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.
| Signal | Status |
|---|---|
| Traces | Stable |
| Metrics | Stable |
| Logs | Stable |
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.
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:
Currently, this project supports the following environments.
| OS | Go Version | Architecture |
|---|---|---|
| Ubuntu | 1.27 | amd64 |
| Ubuntu | 1.26 | amd64 |
| Ubuntu | 1.27 | 386 |
| Ubuntu | 1.26 | 386 |
| Ubuntu | 1.27 | arm64 |
| Ubuntu | 1.26 | arm64 |
| macOS | 1.27 | amd64 |
| macOS | 1.26 | amd64 |
| macOS | 1.27 | arm64 |
| macOS | 1.26 | arm64 |
| Windows | 1.27 | amd64 |
| Windows | 1.26 | amd64 |
| Windows | 1.27 | 386 |
| Windows | 1.26 | 386 |
While this project should work for other systems, no compatibility guarantees are made for those systems currently.
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.
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.
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.
| Exporter | Logs | Metrics | Traces |
|---|---|---|---|
| OTLP | ✓ | ✓ | ✓ |
| Prometheus | ✓ | ||
| stdout | ✓ | ✓ | ✓ |
| Zipkin | ✓ |
See the contributing documentation.