-
Notifications
You must be signed in to change notification settings - Fork 59
fix: register explicit bucket boundaries for duration histograms #2255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cowsking
wants to merge
1
commit into
GoogleContainerTools:main
Choose a base branch
from
cowsking:fix-duration-histogram-buckets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package kmetrics | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "go.opentelemetry.io/otel" | ||
| sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
| "go.opentelemetry.io/otel/sdk/resource" | ||
| ) | ||
|
|
||
| func TestInitializeOTelKustomizeMetrics_HistogramBuckets(t *testing.T) { | ||
| reader := sdkmetric.NewManualReader() | ||
| meterProvider := sdkmetric.NewMeterProvider( | ||
| sdkmetric.WithResource(resource.NewSchemaless()), | ||
| sdkmetric.WithReader(reader), | ||
| ) | ||
| otel.SetMeterProvider(meterProvider) | ||
|
|
||
| if err := InitializeOTelKustomizeMetrics(); err != nil { | ||
| t.Fatalf("InitializeOTelKustomizeMetrics() failed: %v", err) | ||
| } | ||
|
|
||
| ctx := t.Context() | ||
| // Record a 350ms build duration in milliseconds | ||
| executionTime := 350 * time.Millisecond | ||
| RecordKustomizeExecutionTime(ctx, float64(executionTime.Milliseconds())) | ||
|
|
||
| var rm metricdata.ResourceMetrics | ||
| if err := reader.Collect(ctx, &rm); err != nil { | ||
| t.Fatalf("reader.Collect() failed: %v", err) | ||
| } | ||
|
|
||
| var foundPoint *metricdata.HistogramDataPoint[float64] | ||
| for _, sm := range rm.ScopeMetrics { | ||
| for _, m := range sm.Metrics { | ||
| if m.Name == "kustomize_build_latency" { | ||
| if hist, ok := m.Data.(metricdata.Histogram[float64]); ok && len(hist.DataPoints) > 0 { | ||
| foundPoint = &hist.DataPoints[0] | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if foundPoint == nil { | ||
| t.Fatalf("Histogram %q was not found in collected metrics", "kustomize_build_latency") | ||
| } | ||
|
|
||
| if diff := cmp.Diff(KustomizeBuildLatencyBounds, foundPoint.Bounds); diff != "" { | ||
| t.Errorf("Histogram %q bucket boundaries mismatch (-want +got):\n%s", "kustomize_build_latency", diff) | ||
| } | ||
|
|
||
| // For 350ms and KustomizeBuildLatencyBounds [0, 10, 20, 40, 80, 160, 320, 640, ...]: | ||
| // Index 7: (320, 640] <-- 350ms must fall in this bucket | ||
| if len(foundPoint.BucketCounts) <= 7 { | ||
| t.Fatalf("Expected at least 8 bucket counts, got %d", len(foundPoint.BucketCounts)) | ||
| } | ||
| if foundPoint.BucketCounts[7] != 1 { | ||
| t.Errorf("BucketCounts[7] (interval (320, 640]) = %d, want 1. Full BucketCounts: %v", | ||
| foundPoint.BucketCounts[7], foundPoint.BucketCounts) | ||
| } | ||
|
|
||
| // Verify the overflow (+Inf) bucket is empty. Before the fix, durations were | ||
| // recorded in nanoseconds against millisecond-scale default buckets, so every | ||
| // recording landed in the overflow bucket. | ||
| overflowIdx := len(foundPoint.BucketCounts) - 1 | ||
| if foundPoint.BucketCounts[overflowIdx] != 0 { | ||
| t.Errorf("BucketCounts[%d] (+Inf overflow bucket) = %d, want 0", overflowIdx, foundPoint.BucketCounts[overflowIdx]) | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package metrics | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "go.opentelemetry.io/otel" | ||
| sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
| "go.opentelemetry.io/otel/sdk/resource" | ||
| ) | ||
|
|
||
| func TestInitializeOTelMetrics_HistogramBuckets(t *testing.T) { | ||
| reader := sdkmetric.NewManualReader() | ||
| meterProvider := sdkmetric.NewMeterProvider( | ||
| sdkmetric.WithResource(resource.NewSchemaless()), | ||
| sdkmetric.WithReader(reader), | ||
| ) | ||
| otel.SetMeterProvider(meterProvider) | ||
|
|
||
| if err := InitializeOTelMetrics(); err != nil { | ||
| t.Fatalf("InitializeOTelMetrics() failed: %v", err) | ||
| } | ||
|
|
||
| ctx := t.Context() | ||
| startTime := time.Now().Add(-350 * time.Millisecond) | ||
|
|
||
| RecordAPICallDuration(ctx, "get", "success", startTime) | ||
| RecordReconcileDuration(ctx, "success", startTime) | ||
| RecordParserDuration(ctx, "git", "root", "success", startTime) | ||
| RecordApplyDuration(ctx, "success", "abcdef", startTime) | ||
| RecordRemediateDuration(ctx, "success", startTime) | ||
|
|
||
| var rm metricdata.ResourceMetrics | ||
| if err := reader.Collect(ctx, &rm); err != nil { | ||
| t.Fatalf("reader.Collect() failed: %v", err) | ||
| } | ||
|
|
||
| expectedHistograms := map[string][]float64{ | ||
| APICallDurationName: DistributionBounds, | ||
| ReconcileDurationName: DistributionBounds, | ||
| ParserDurationName: LongDistributionBounds, | ||
| ApplyDurationName: LongDistributionBounds, | ||
| RemediateDurationName: DistributionBounds, | ||
| } | ||
|
|
||
| foundHistograms := make(map[string][]float64) | ||
| for _, sm := range rm.ScopeMetrics { | ||
| for _, m := range sm.Metrics { | ||
| if hist, ok := m.Data.(metricdata.Histogram[float64]); ok { | ||
| if len(hist.DataPoints) > 0 { | ||
| foundHistograms[m.Name] = hist.DataPoints[0].Bounds | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for metricName, wantBounds := range expectedHistograms { | ||
| t.Run(metricName, func(t *testing.T) { | ||
| gotBounds, ok := foundHistograms[metricName] | ||
| if !ok { | ||
| t.Fatalf("Histogram %q was not found in collected metrics", metricName) | ||
| } | ||
| if diff := cmp.Diff(wantBounds, gotBounds); diff != "" { | ||
| t.Errorf("Histogram %q bucket boundaries mismatch (-want +got):\n%s", metricName, diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRecordDuration_SubsecondBucketPlacement(t *testing.T) { | ||
| reader := sdkmetric.NewManualReader() | ||
| meterProvider := sdkmetric.NewMeterProvider( | ||
| sdkmetric.WithResource(resource.NewSchemaless()), | ||
| sdkmetric.WithReader(reader), | ||
| ) | ||
| otel.SetMeterProvider(meterProvider) | ||
|
|
||
| if err := InitializeOTelMetrics(); err != nil { | ||
| t.Fatalf("InitializeOTelMetrics() failed: %v", err) | ||
| } | ||
|
|
||
| ctx := t.Context() | ||
| // Record an exact 350ms duration (0.35s) | ||
| startTime := time.Now().Add(-350 * time.Millisecond) | ||
| RecordAPICallDuration(ctx, "get", "success", startTime) | ||
|
|
||
| var rm metricdata.ResourceMetrics | ||
| if err := reader.Collect(ctx, &rm); err != nil { | ||
| t.Fatalf("reader.Collect() failed: %v", err) | ||
| } | ||
|
|
||
| var foundPoint *metricdata.HistogramDataPoint[float64] | ||
| for _, sm := range rm.ScopeMetrics { | ||
| for _, m := range sm.Metrics { | ||
| if m.Name == APICallDurationName { | ||
| if hist, ok := m.Data.(metricdata.Histogram[float64]); ok && len(hist.DataPoints) > 0 { | ||
| foundPoint = &hist.DataPoints[0] | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if foundPoint == nil { | ||
| t.Fatalf("Metric %q was not recorded", APICallDurationName) | ||
| } | ||
|
|
||
| // For 0.35s and DistributionBounds [.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10]: | ||
| // Index 0: <= 0.005 | ||
| // Index 1: (0.005, 0.01] | ||
| // Index 2: (0.01, 0.025] | ||
| // Index 3: (0.025, 0.05] | ||
| // Index 4: (0.05, 0.1] | ||
| // Index 5: (0.1, 0.25] | ||
| // Index 6: (0.25, 0.5] <-- 0.35s must fall in this sub-second bucket | ||
| // Index 9: (2.5, 5.0] <-- Before fix, this was where it fell (le=5) | ||
| if len(foundPoint.BucketCounts) <= 6 { | ||
| t.Fatalf("Expected at least 7 bucket counts, got %d", len(foundPoint.BucketCounts)) | ||
| } | ||
|
|
||
| if foundPoint.BucketCounts[6] != 1 { | ||
| t.Errorf("BucketCounts[6] (interval (0.25, 0.5]) = %d, want 1. Full BucketCounts: %v", | ||
| foundPoint.BucketCounts[6], foundPoint.BucketCounts) | ||
| } | ||
|
|
||
| // Verify that the coarse 5-second bucket (index 9) is 0, confirming it didn't fall into the 5s bucket | ||
| if foundPoint.BucketCounts[9] != 0 { | ||
| t.Errorf("BucketCounts[9] (interval (2.5, 5.0]) = %d, want 0", foundPoint.BucketCounts[9]) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 forkustomize_build_latency, fixed a pre-existing bug inexec.gowhere raw nanoseconds were recorded as duration and added unit test coverage.