Skip to content

Report native histograms to M3 as sketches - #288

Open
adamjeanlaurent wants to merge 2 commits into
masterfrom
ajeanlaurent/native-histogram-sketch
Open

adamjeanlaurent wants to merge 2 commits into
masterfrom
ajeanlaurent/native-histogram-sketch

Conversation

@adamjeanlaurent

@adamjeanlaurent adamjeanlaurent commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

tally v7 has native histograms, but the default accumulator could not marshal them, and the M3 reporter dropped every payload. m3collector now accepts native histograms as SKETCH metrics. This PR makes tally send them.

Changes

  • native_histogram_expohisto.go (new): the default NativeHistogramData. It wraps the upstream OpenTelemetry pkg/expohisto histogram (structure.Float64). MarshalBinary writes the proto3 ExponentialHistogram message that m3collector decodes, with google.golang.org/protobuf/encoding/protowire. No generated code is needed.

    • It drops NaN and ±Inf. Upstream does not filter them, and they make the sum and the bucket indexes invalid.
    • It limits the bucket count to 160. m3collector rejects more buckets than that on one side.
    • It returns an error for a scale below -4. m3collector rejects that scale.
  • scope.go, native_histogram.go, types.go: a scope without NativeHistogramFactory now uses this accumulator. ScopeOptions.NativeHistogramFactory still overrides it. ErrNativeHistogramBackendNotConfigured stays exported because v7.0.0 has it, but it is deprecated, because nothing returns it now.

  • m3/thrift/v2/v2.thrift: add SKETCH = 4 to MetricType and 5: optional binary sketch to MetricValue. This is the same schema as m3collector.

  • m3/thrift/v2/ttypes.go: add the new enum value and field. The Thrift 0.9.3 compiler was not available, so I edited the file by hand to match the 0.9.3 generator output for an optional binary field.

  • m3/thrift/thrift.diff: rebuilt from a real pre-image, so make gen-thrift still applies cleanly. Only the index hashes and the hunk offsets change.

  • m3/reporter.go: AllocateNativeHistogram allocates a SKETCH metric. ReportNativeHistogram sets the payload and computes the size on each report, because the payload size changes from one interval to the next. A payload that does not fit in one packet is dropped and counted in tally.internal.num-native-histograms-dropped. flush() clears the payloads, so the reused slice does not keep them alive.

  • version.go: Version goes from 7.0.0 to 7.1.0. The reporter sends it as the version tag on internal metrics. Tag the merge commit as v7.1.0.

Go version

The upstream pkg/expohisto module requires Go 1.26, so the go line moves from 1.20 to 1.26.0. This also needs these changes:

  • tools/go.mod: staticcheck v0.3.2 to v0.8.2. The old version cannot read Go 1.26.
  • thirdparty/.../thrift/protocol_test.go: on Go 1.26, vet rejects %q with integer arguments. Ten format verbs change to %v. This is a test-only change.

Compatibility

  • Services that pick up this version send SKETCH metrics by default. A collector without sketch support, or in strict mode, can reject the whole batch, which also loses the counters and gauges in it. All m3collector deployments must support sketches before services upgrade.
  • Consumers need Go 1.26 or newer.

Testing

  • make test (with -race) and make cover pass.
  • TestExpoHistogramDataGoldenBytes compares the payload with bytes worked out by hand from the proto3 rules.
  • TestExpoHistogramDataMixedValues uses the same input as the m3collector TestSketchPayloadRoundTrip. It decodes the payload independently of the encoder and checks scale, counts, sum, min, max, bucket offsets, and that the count equals the zero count plus the bucket counts.
  • Other tests cover NaN and Inf, the bucket limit, the scale floor, and Clear.
  • TestMetricValueSketchRoundTrip writes and reads the new field, set and unset, for binary and compact protocol.
  • TestReporterNativeHistogram and TestScopeNativeHistogram check that the fake M3 server receives SKETCH metrics with the exact payload.
  • TestReporterNativeHistogramTooLargeIsDropped checks the drop and the drop counter.
  • TestBatchSizes now mixes in sketch payloads of random size, and every packet stays under the size limit.
  • I will test manually against a real m3collector.

Known issues not fixed here

  • The newer staticcheck reports 6 deprecation warnings in code that this PR does not change (io/ioutil, atomic.Bool.CAS, rand.Seed).
  • The CI lint step never runs. It has if: matrix.latest, and the matrix has no latest key.

🤖 Generated with Claude Code

adamjeanlaurent and others added 2 commits October 22, 2026 17:56
tally v7 has native histograms, but the default accumulator could not
marshal them, and the M3 reporter dropped every payload. m3collector now
accepts native histograms as SKETCH metrics. This change makes tally
send them.

The default accumulator is now an OpenTelemetry base-2 exponential
histogram from the upstream pkg/expohisto module. It marshals to the
proto3 ExponentialHistogram message that m3collector decodes. The
accumulator drops NaN and infinities, limits the bucket count to 160,
and refuses a scale below -4, because m3collector rejects those payloads.

The M3 thrift schema gets MetricType SKETCH = 4 and the optional
MetricValue field 5: binary sketch. The reporter sends each payload as a
SKETCH metric and computes its size on each report. It drops a payload
that does not fit in one packet and counts it in
tally.internal.num-native-histograms-dropped.

The upstream module requires Go 1.26, so the go line moves from 1.20 to
1.26. This also needs a newer staticcheck and a fix to format verbs in a
vendored thrift test that vet now rejects.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

1 participant