-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[metrics] Dependency resolution metrics
This PR introduces two histogram metrics to help in analysing the time taken by dependency resolution requests.
- Loading branch information
Showing
6 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,34 @@ | ||
package resolver | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
) | ||
|
||
type InstrumentedResolver struct { | ||
operatorsV1alpha1Resolver Resolver | ||
successMetricsEmitter func(time.Duration) | ||
failureMetricsEmitter func(time.Duration) | ||
} | ||
|
||
var _ Resolver = &OperatorsV1alpha1Resolver{} | ||
|
||
func NewInstrumentedResolver(resolver Resolver, successMetricsEmitter, failureMetricsEmitter func(time.Duration)) *InstrumentedResolver { | ||
return &InstrumentedResolver{ | ||
operatorsV1alpha1Resolver: resolver, | ||
successMetricsEmitter: successMetricsEmitter, | ||
failureMetricsEmitter: failureMetricsEmitter, | ||
} | ||
} | ||
|
||
func (ir *InstrumentedResolver) ResolveSteps(namespace string, sourceQuerier SourceQuerier) ([]*v1alpha1.Step, []v1alpha1.BundleLookup, []*v1alpha1.Subscription, error) { | ||
start := time.Now() | ||
steps, lookups, subs, err := ir.operatorsV1alpha1Resolver.ResolveSteps(namespace, sourceQuerier) | ||
if err != nil { | ||
ir.failureMetricsEmitter(time.Now().Sub(start)) | ||
} else { | ||
ir.successMetricsEmitter(time.Now().Sub(start)) | ||
} | ||
return steps, lookups, subs, err | ||
} |
69 changes: 69 additions & 0 deletions
69
pkg/controller/registry/resolver/instrumented_resolver_test.go
This file contains 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,69 @@ | ||
package resolver | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
failure = time.Duration(0) | ||
success = time.Duration(1) | ||
reset = time.Duration(99999) | ||
) | ||
|
||
type fakeResolverWithError struct{} | ||
type fakeResolverWithoutError struct{} | ||
|
||
func (r *fakeResolverWithError) ResolveSteps(namespace string, sourceQuerier SourceQuerier) ([]*v1alpha1.Step, []v1alpha1.BundleLookup, []*v1alpha1.Subscription, error) { | ||
return nil, nil, nil, errors.New("Fake error") | ||
} | ||
|
||
func (r *fakeResolverWithoutError) ResolveSteps(namespace string, sourceQuerier SourceQuerier) ([]*v1alpha1.Step, []v1alpha1.BundleLookup, []*v1alpha1.Subscription, error) { | ||
return nil, nil, nil, nil | ||
} | ||
|
||
func newFakeResolverWithError() *fakeResolverWithError { | ||
return &fakeResolverWithError{} | ||
} | ||
|
||
func newFakeResolverWithoutError() *fakeResolverWithoutError { | ||
return &fakeResolverWithoutError{} | ||
} | ||
|
||
func TestInstrumentedResolverFailure(t *testing.T) { | ||
result := []time.Duration{} | ||
|
||
changeToFailure := func(num time.Duration) { | ||
result = append(result, failure) | ||
} | ||
|
||
changeToSuccess := func(num time.Duration) { | ||
result = append(result, success) | ||
} | ||
|
||
instrumentedResolver := NewInstrumentedResolver(newFakeResolverWithError(), changeToSuccess, changeToFailure) | ||
instrumentedResolver.ResolveSteps("", nil) | ||
require.Equal(t, len(result), 1) // check that only one call was made to a change function | ||
require.Equal(t, result[0], failure) // check that the call was made to changeToFailure function | ||
} | ||
|
||
func TestInstrumentedResolverSuccess(t *testing.T) { | ||
result := []time.Duration{} | ||
|
||
changeToFailure := func(num time.Duration) { | ||
result = append(result, failure) | ||
} | ||
|
||
changeToSuccess := func(num time.Duration) { | ||
result = append(result, success) | ||
} | ||
|
||
instrumentedResolver := NewInstrumentedResolver(newFakeResolverWithoutError(), changeToSuccess, changeToFailure) | ||
instrumentedResolver.ResolveSteps("", nil) | ||
require.Equal(t, len(result), 1) // check that only one call was made to a change function | ||
require.Equal(t, result[0], success) // check that the call was made to changeToSuccess function | ||
} |
This file contains 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 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 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