Skip to content

[BREAKING] feat: unified metric abstraction and allow to register custom metrics #24

Merged
mingrammer merged 5 commits intomainfrom
feat/unified-metric-abstraction
Apr 23, 2026
Merged

[BREAKING] feat: unified metric abstraction and allow to register custom metrics #24
mingrammer merged 5 commits intomainfrom
feat/unified-metric-abstraction

Conversation

@mingrammer
Copy link
Copy Markdown
Contributor

What's new

  • Metric interface unifies CPU / Mem / Goroutine / custom metrics behind
    one contract (Name, Threshold, Interval, Query, Collect). Built-in
    watchers become pre-defined Metric implementations.
  • Runtime Register / Unregister API for adding / removing user metrics
    after Start — useful when a metric lives inside a struct whose lifecycle
    begins after autopprof.Start() (connection pools, queue handlers, …).
  • Option.Metrics for start-time registration of user metrics.
  • Option.App becomes the single source for the <app> filename segment.
  • Reporter interface collapses to a single Report(ctx, r, ReportInfo)
    method. ReportInfo.MetricName lets implementations route without parsing
    filenames.
  • CollectResult.Reader == nil lets Metric.Collect opt out of the Reporter
    call for side-effect-only hooks; empty Filename / Comment fall back to
    autopprof-generated defaults.
  • New convenience constructor NewMetric for ad-hoc metrics.

Removed / breaking

  • Option.ReportBoth (use ReportAll).
  • SlackReporterOption.App (moved to Option.App).
  • SlackReporterOption.Channel (Slack API dropped channel-name uploads).
  • Reporter interface's ReportCPUProfile / ReportHeapProfile /
    ReportGoroutineProfile + CPUInfo / MemInfo / GoroutineInfo.
  • report.CPUProfileFilenameFmt / HeapProfileFilenameFmt /
    GoroutineProfileFilenameFmt public constants (internalized).

It has breaking changes. We must release it with the major version update.

@mingrammer mingrammer requested a review from a team April 22, 2026 11:22
@mingrammer mingrammer force-pushed the feat/unified-metric-abstraction branch from 8a1ece6 to 8f224a7 Compare April 23, 2026 02:46
BREAKING CHANGE: Moves module to v2 path (github.com/daangn/autopprof/v2).

## What's new

- `Metric` interface unifies CPU / Mem / Goroutine / custom metrics behind
  one contract (`Name`, `Threshold`, `Interval`, `Query`, `Collect`). Built-in
  watchers become pre-defined Metric implementations.
- Runtime `Register` API for adding user metrics after `Start` — useful
  when a metric lives inside a struct whose lifecycle begins after
  `autopprof.Start()` (connection pools, queue handlers, …).
- `Option.Metrics` for start-time registration of user metrics.
- `Option.App` becomes the single source for the `<app>` filename segment;
  defaults to `"autopprof"` when left empty.
- `Reporter` interface collapses to a single `Report(ctx, r, ReportInfo)`
  method. `ReportInfo.MetricName` lets implementations route without parsing
  filenames.
- `CollectResult.Reader == nil` lets Metric.Collect opt out of the Reporter
  call for side-effect-only hooks; empty Filename / Comment fall back to
  autopprof-generated defaults.
- New convenience constructor `NewMetric` for ad-hoc metrics.

## Removed / breaking

- `Option.ReportBoth` (use `ReportAll`).
- `SlackReporterOption.App` (moved to `Option.App`).
- `SlackReporterOption.Channel` (Slack API dropped channel-name uploads).
- `Reporter` interface's `ReportCPUProfile` / `ReportHeapProfile` /
  `ReportGoroutineProfile` + `CPUInfo` / `MemInfo` / `GoroutineInfo`.
- `report.CPUProfileFilenameFmt` / `HeapProfileFilenameFmt` /
  `GoroutineProfileFilenameFmt` public constants (internalized).
@mingrammer mingrammer force-pushed the feat/unified-metric-abstraction branch from 8f224a7 to af168d6 Compare April 23, 2026 02:47
Comment thread autopprof.go
Comment thread queryer/cgroupv2.go Outdated
Comment thread autopprof.go Outdated
Both cgroupV1 and cgroupV2 held a qMu to serialize concurrent access
to their cpuUsageSnapshotQueue. Pull that responsibility into the
queue itself so callers don't have to know about its concurrency
model. enqueue/head/tail/isFull each take an internal mutex; the
cgroup queryers lose qMu entirely. cap()/len() methods (test-only)
are dropped — tests read cap(q.list)/len(q.list) directly.
Previously Start and Stop were documented as "not guarded against
concurrent use". Wrap each with a package-level sync.Once so repeated
or concurrent invocations are race-free — Start executes at most once
per process and caches its error for later callers; Stop collapses
multiple callers to a single shutdown. Tests use resetGlobal() to
reset the sentinels between cases.
BREAKING CHANGE: Option.ReportAll is removed. Cascade (reporting every
enabled built-in profile when any of them breaches its threshold) is
now always on. Users who want to opt a built-in out of the cascade use
the existing DisableCPUProf / DisableMemProf / DisableGoroutineProf
flags — disabling a built-in removes it from the watcher and the
cascade in one shot.
@mingrammer mingrammer requested a review from proost April 23, 2026 07:39
Copy link
Copy Markdown

@proost proost left a comment

Choose a reason for hiding this comment

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

LGTM!

@mingrammer mingrammer merged commit 1437c52 into main Apr 23, 2026
16 of 20 checks passed
@mingrammer mingrammer deleted the feat/unified-metric-abstraction branch April 23, 2026 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants