-
Notifications
You must be signed in to change notification settings - Fork 10
feat(observability): measure gateway provision duration #243
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,83 @@ | ||
| package otel | ||
|
|
||
| import ( | ||
| "context" | ||
| "reflect" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "go.opentelemetry.io/otel" | ||
| sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
| ) | ||
|
|
||
| func TestRecordGatewayProvisionDuration(t *testing.T) { | ||
| previousProvider := otel.GetMeterProvider() | ||
| previousReconcileDuration := reconcileDuration | ||
| previousGatewayProvisionDuration := gatewayProvisionDuration | ||
| previousReconcileErrors := reconcileErrors | ||
| previousWatchReconnects := watchReconnects | ||
|
|
||
| reader := sdkmetric.NewManualReader() | ||
| provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) | ||
| otel.SetMeterProvider(provider) | ||
| reconcileDuration = nil | ||
| gatewayProvisionDuration = nil | ||
| reconcileErrors = nil | ||
| watchReconnects = nil | ||
| t.Cleanup(func() { | ||
| otel.SetMeterProvider(previousProvider) | ||
| reconcileDuration = previousReconcileDuration | ||
| gatewayProvisionDuration = previousGatewayProvisionDuration | ||
| reconcileErrors = previousReconcileErrors | ||
| watchReconnects = previousWatchReconnects | ||
| _ = provider.Shutdown(context.Background()) | ||
| }) | ||
|
|
||
| if err := registerMetrics(); err != nil { | ||
| t.Fatalf("registerMetrics() returned an error: %v", err) | ||
| } | ||
|
|
||
| RecordGatewayProvisionDuration(context.Background(), 37_500*time.Millisecond) | ||
| RecordGatewayProvisionDuration(context.Background(), -1*time.Second) | ||
|
|
||
| var collected metricdata.ResourceMetrics | ||
| if err := reader.Collect(context.Background(), &collected); err != nil { | ||
| t.Fatalf("Collect() returned an error: %v", err) | ||
| } | ||
|
|
||
| for _, scope := range collected.ScopeMetrics { | ||
| for _, gotMetric := range scope.Metrics { | ||
| if gotMetric.Name != "gateway.provision.duration" { | ||
| continue | ||
| } | ||
| if gotMetric.Unit != "s" { | ||
| t.Fatalf("metric unit = %q, want s", gotMetric.Unit) | ||
| } | ||
| histogram, ok := gotMetric.Data.(metricdata.Histogram[float64]) | ||
| if !ok { | ||
| t.Fatalf("metric data type = %T, want float64 histogram", gotMetric.Data) | ||
| } | ||
| if len(histogram.DataPoints) != 1 { | ||
| t.Fatalf("data point count = %d, want 1", len(histogram.DataPoints)) | ||
| } | ||
| point := histogram.DataPoints[0] | ||
| if point.Count != 1 { | ||
| t.Fatalf("sample count = %d, want 1", point.Count) | ||
| } | ||
| if point.Sum != 37.5 { | ||
| t.Fatalf("sample sum = %v, want 37.5", point.Sum) | ||
| } | ||
| if point.Attributes.Len() != 0 { | ||
| t.Fatalf("metric attributes = %v, want none", point.Attributes) | ||
| } | ||
| wantBounds := []float64{1, 5, 10, 15, 30, 45, 60, 90, 120, 180, 300, 600, 900} | ||
| if !reflect.DeepEqual(point.Bounds, wantBounds) { | ||
| t.Fatalf("bucket bounds = %v, want %v", point.Bounds, wantBounds) | ||
| } | ||
| return | ||
| } | ||
| } | ||
|
|
||
| t.Fatal("gateway.provision.duration metric was not collected") | ||
| } |
11 changes: 11 additions & 0 deletions
11
components/control-plane/internal/reconciler/gateway_vocabulary.go
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,11 @@ | ||
| package reconciler | ||
|
|
||
| // These values are the shared Gateway health vocabulary for the reconciler and | ||
| // health paths. Keep phase comparisons and phase updates on this vocabulary. | ||
| const ( | ||
| gatewayPhaseProvisioning = "Provisioning" | ||
| gatewayPhaseRunning = "Running" | ||
| gatewayPhaseDegraded = "Degraded" | ||
| gatewayPhaseFailed = "Failed" | ||
| gatewayStatusHealthy = "Healthy" | ||
| ) |
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,82 @@ | ||
| package reconciler | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
|
|
||
| pb "github.com/openshift-online/hypershell/components/api-server/pkg/api/grpc/hypershell/v1" | ||
| cpotel "github.com/openshift-online/hypershell/components/control-plane/internal/otel" | ||
| ) | ||
|
|
||
| // observedGatewayProvisions coordinates the event-driven and health reconcile | ||
| // paths. These paths can observe the same first Running transition at the same | ||
| // time. The Gateway identifier stays in process memory and is not an OTel | ||
| // attribute. | ||
| var observedGatewayProvisions sync.Map | ||
|
jsell-rh marked this conversation as resolved.
|
||
|
|
||
| // observeGatewayProvisionDuration records the time from API creation until the | ||
| // first successful transition to Running. It ignores incomplete or invalid | ||
| // timestamps so telemetry cannot change reconciliation behavior. | ||
| func observeGatewayProvisionDuration(ctx context.Context, gw *pb.Gateway) { | ||
| duration, ok := gatewayProvisionDuration(gw) | ||
| if !ok { | ||
| return | ||
| } | ||
| gatewayID := gw.GetMetadata().GetId() | ||
| if !claimGatewayProvisionObservation(gatewayID) { | ||
| return | ||
| } | ||
| cpotel.RecordGatewayProvisionDuration(ctx, duration) | ||
| } | ||
|
|
||
| func claimGatewayProvisionObservation(gatewayID string) bool { | ||
| if gatewayID == "" { | ||
| return false | ||
| } | ||
| _, loaded := observedGatewayProvisions.LoadOrStore(gatewayID, struct{}{}) | ||
| return !loaded | ||
| } | ||
|
|
||
| func forgetGatewayProvisionObservation(gatewayID string) { | ||
| observedGatewayProvisions.Delete(gatewayID) | ||
| } | ||
|
|
||
| // suppressGatewayProvisionObservation prevents work on a previously Running or | ||
| // Degraded Gateway from producing a new provision observation. The caller uses | ||
| // the stored phase for a normal event. The retry adapter supplies the phase that | ||
| // it cleared when it bypasses the phase gate. | ||
| func suppressGatewayProvisionObservation(gatewayID, previousPhase string) { | ||
| if previousPhase == gatewayPhaseRunning || previousPhase == gatewayPhaseDegraded { | ||
| claimGatewayProvisionObservation(gatewayID) | ||
| } | ||
| } | ||
|
|
||
| func gatewayProvisionDuration(gw *pb.Gateway) (time.Duration, bool) { | ||
| if gw == nil || gw.GetMetadata() == nil { | ||
| return 0, false | ||
| } | ||
| createdAt := gw.GetMetadata().GetCreatedAt() | ||
| runningAt := gw.GetMetadata().GetUpdatedAt() | ||
| if createdAt == nil || runningAt == nil { | ||
| return 0, false | ||
| } | ||
| if err := createdAt.CheckValid(); err != nil { | ||
| return 0, false | ||
| } | ||
| if err := runningAt.CheckValid(); err != nil { | ||
| return 0, false | ||
| } | ||
| duration := runningAt.AsTime().Sub(createdAt.AsTime()) | ||
| if duration < 0 { | ||
| return 0, false | ||
| } | ||
| return duration, true | ||
| } | ||
|
|
||
| // isGatewayProvisionCompletion excludes later recovery transitions. A new | ||
| // gateway can stay in Provisioning after the first reconcile while its route | ||
| // becomes ready. A Running gateway that fails moves through Degraded instead. | ||
| func isGatewayProvisionCompletion(currentPhase, desiredPhase string) bool { | ||
| return currentPhase == gatewayPhaseProvisioning && desiredPhase == gatewayPhaseRunning | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.