Skip to content

fix: register explicit bucket boundaries for duration histograms - #2255

Open
cowsking wants to merge 1 commit into
GoogleContainerTools:mainfrom
cowsking:fix-duration-histogram-buckets
Open

fix: register explicit bucket boundaries for duration histograms#2255
cowsking wants to merge 1 commit into
GoogleContainerTools:mainfrom
cowsking:fix-duration-histogram-buckets

Conversation

@cowsking

@cowsking cowsking commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Following the OpenCensus to OpenTelemetry metrics migration, duration histogram instruments in Config Sync lacked explicit bucket boundaries. This caused the OpenTelemetry Go SDK to fall back to default millisecond-scale boundaries {0, 5, 10, 25, ...}, which for second-unit metrics resulted in all sub-5-second values falling into the (0, 5] bucket and skewing Prometheus histogram_quantile queries (e.g. static 4.75s).

This change:

  • Configures explicit sub-second bucket boundaries for core duration metrics (APICallDuration, ReconcileDuration, RemediateDuration) and long duration metrics (ParserDuration, ApplyDuration).
  • Configures explicit bucket boundaries for ResourceGroup ReconcileDuration.
  • Fixes pkg/kmetrics kustomize_build_latency: the histogram recorded raw nanosecond values while declaring a millisecond unit, so every recording landed in the overflow (+Inf) bucket regardless of bucket configuration. It now records the duration in seconds (consistent with all other Config Sync duration metrics), declares the unit as s, and registers explicit bucket boundaries. Note this changes the scale of the exported values (previously nanosecond-scale numbers labeled as ms).
  • Adds unit tests to verify histogram bucket bounds and sub-second data point placement.

@google-oss-prow
google-oss-prow Bot requested review from Camila-B and janetkuo August 12, 2026 22:41
@google-oss-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from cowsking. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cowsking

Copy link
Copy Markdown
Contributor Author

/test kpt-config-sync-presubmit

Comment thread pkg/metrics/metrics.go
@@ -51,6 +51,14 @@ const (
InternalErrorsName = "internal_errors_total"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for working on the fix, could you check if the fix apply to pkg/kmetrics as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks! Pushed an update to include pkg/kmetrics: added explicit second-scale bucket bounds for kustomize_build_latency, fixed a pre-existing bug in exec.go where raw nanoseconds were recorded as duration and added unit test coverage.

@cowsking
cowsking force-pushed the fix-duration-histogram-buckets branch from 1a1956e to 535a861 Compare August 15, 2026 00:40
Comment thread pkg/kmetrics/exec.go Outdated
if err == nil && sendMetrics {
RecordKustomizeResourceCount(ctx, resourceCount)
RecordKustomizeExecutionTime(ctx, float64(executionTime))
RecordKustomizeExecutionTime(ctx, executionTime.Seconds())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Record in ms, to align with pkg/kmetrics/metrics.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Updated to float64(executionTime.Milliseconds())

Comment thread pkg/kmetrics/metrics.go Outdated
metric.WithDescription("Kustomize build latency"),
metric.WithUnit("ms"),
metric.WithDescription("Kustomize build latency in seconds"),
metric.WithUnit("s"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Keep metric.WithUnit("ms"), set KustomizeBuildLatencyBounds to match its original millisecond exponential buckets. Context go/config-sync-monarch-metrics some metrics are predefined in Monarch, changes in unit might cause error like INVALID_ARGUMENT: The bucket options / unit do not match the metric descriptor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Kept the "ms" unit and restored the exponential buckets ([0, 10, 20, ..., 10240]) to match the Monarch descriptor.

Comment thread pkg/metrics/metrics.go Outdated
DistributionBounds = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}

// LongDistributionBounds defines the bounds for a histogram distribution measuring long durations.
LongDistributionBounds = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 5, 10, 30, 60, 300, 600, 1200, 1800, 3600, 5400}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you check whether LongDistributionBounds (parser_duration_seconds, apply_duration_seconds) should start at 1 instead of .005 to match Monarch's pre-registered descriptor ([1, 5, 10, ...]).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated LongDistributionBounds to start at 1 ([1, 5, 10, ..., 5400]) to align

Following the OpenCensus to OpenTelemetry metrics migration, duration
histogram instruments in Config Sync lacked explicit bucket boundaries.
This caused the OpenTelemetry Go SDK to fall back to default
millisecond-scale boundaries {0, 5, 10, 25, ...}, which for second-unit
metrics resulted in all sub-5-second values falling into the (0, 5] bucket
and skewing Prometheus histogram_quantile queries (e.g. static 4.75s).

This change:
- Configures explicit sub-second bucket boundaries for core duration
  metrics (APICallDuration, ReconcileDuration, RemediateDuration) and
  long duration metrics (ParserDuration, ApplyDuration).
- Configures explicit bucket boundaries for ResourceGroup ReconcileDuration.
- Adds unit tests to verify histogram bucket bounds and sub-second data
  point placement.

Fixes b/545631804
@cowsking
cowsking force-pushed the fix-duration-histogram-buckets branch from 535a861 to 3e1eeff Compare August 19, 2026 07:27
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