[BREAKING] feat: unified metric abstraction and allow to register custom metrics #24
Merged
mingrammer merged 5 commits intomainfrom Apr 23, 2026
Merged
Conversation
8a1ece6 to
8f224a7
Compare
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).
8f224a7 to
af168d6
Compare
proost
reviewed
Apr 23, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's new
Metricinterface unifies CPU / Mem / Goroutine / custom metrics behindone contract (
Name,Threshold,Interval,Query,Collect). Built-inwatchers become pre-defined Metric implementations.
Register/UnregisterAPI for adding / removing user metricsafter
Start— useful when a metric lives inside a struct whose lifecyclebegins after
autopprof.Start()(connection pools, queue handlers, …).Option.Metricsfor start-time registration of user metrics.Option.Appbecomes the single source for the<app>filename segment.Reporterinterface collapses to a singleReport(ctx, r, ReportInfo)method.
ReportInfo.MetricNamelets implementations route without parsingfilenames.
CollectResult.Reader == nillets Metric.Collect opt out of the Reportercall for side-effect-only hooks; empty Filename / Comment fall back to
autopprof-generated defaults.
NewMetricfor ad-hoc metrics.Removed / breaking
Option.ReportBoth(useReportAll).SlackReporterOption.App(moved toOption.App).SlackReporterOption.Channel(Slack API dropped channel-name uploads).Reporterinterface'sReportCPUProfile/ReportHeapProfile/ReportGoroutineProfile+CPUInfo/MemInfo/GoroutineInfo.report.CPUProfileFilenameFmt/HeapProfileFilenameFmt/GoroutineProfileFilenameFmtpublic constants (internalized).It has breaking changes. We must release it with the major version update.