Skip to content
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

feat: add metrics for konnect entity operations #953

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
and preview deployments are now customized with Konnect-related settings.
[#910](https://github.com/Kong/gateway-operator/pull/910)

### Added

- Added prometheus metrics for Konnect entity operations in the metrics server:
- `gateway_operator_konnect_entity_operation_count` for number of operations.
- `gateway_operator_konnect_entity_operation_duration_milliseconds` for duration of operations.
[#953](https://github.com/Kong/gateway-operator/pull/953)

## [v1.4.1]

> Release date: 2024-11-28
Expand Down
75 changes: 72 additions & 3 deletions controller/konnect/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/kong/gateway-operator/controller/konnect/constraints"
sdkops "github.com/kong/gateway-operator/controller/konnect/ops/sdk"
"github.com/kong/gateway-operator/controller/pkg/log"
"github.com/kong/gateway-operator/internal/metrics"
"github.com/kong/gateway-operator/pkg/consts"
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"

Expand Down Expand Up @@ -45,11 +46,15 @@ func Create[
ctx context.Context,
sdk sdkops.SDKWrapper,
cl client.Client,
metricRecorder metrics.Recorder,
e TEnt,
) (*T, error) {
var (
err error
start = time.Now()

entityType = e.GetTypeName()
statusCode int
)
switch ent := any(e).(type) {
case *konnectv1alpha1.KonnectGatewayControlPlane:
Expand Down Expand Up @@ -162,6 +167,7 @@ func Create[
}

case errors.As(err, &errSDK):
statusCode = errSDK.StatusCode
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToCreateReason, errSDK.Error())
case errors.As(err, &errRelationsFailed):
e.SetKonnectID(errRelationsFailed.KonnectID)
Expand All @@ -172,6 +178,22 @@ func Create[
SetKonnectEntityProgrammedCondition(e)
}

if err != nil {
metricRecorder.RecordKonnectEntityOperationFailure(
sdk.GetServerURL(),
metrics.KonnectEntityOperationCreate,
entityType,
time.Since(start),
statusCode,
)
} else {
metricRecorder.RecordKonnectEntityOperationSuccess(
sdk.GetServerURL(),
metrics.KonnectEntityOperationCreate,
entityType,
time.Since(start),
)
}
logOpComplete(ctx, start, CreateOp, e, err)

return e, IgnoreUnrecoverableAPIErr(err, loggerForEntity(ctx, e, CreateOp))
Expand All @@ -182,7 +204,7 @@ func Create[
func Delete[
T constraints.SupportedKonnectEntityType,
TEnt constraints.EntityType[T],
](ctx context.Context, sdk sdkops.SDKWrapper, cl client.Client, ent TEnt) error {
](ctx context.Context, sdk sdkops.SDKWrapper, cl client.Client, metricRecorder metrics.Recorder, ent TEnt) error {
if ent.GetKonnectStatus().GetKonnectID() == "" {
cond, ok := k8sutils.GetCondition(konnectv1alpha1.KonnectEntityProgrammedConditionType, ent)
if ok && cond.Status == metav1.ConditionTrue {
Expand All @@ -197,6 +219,9 @@ func Delete[
var (
err error
start = time.Now()

entityType = ent.GetTypeName()
statusCode int
)
switch ent := any(ent).(type) {
case *konnectv1alpha1.KonnectGatewayControlPlane:
Expand Down Expand Up @@ -245,6 +270,26 @@ func Delete[
return fmt.Errorf("unsupported entity type %T", ent)
}

if err != nil {
var errSDK *sdkkonnecterrs.SDKError
if errors.As(err, &errSDK) {
statusCode = errSDK.StatusCode
}
metricRecorder.RecordKonnectEntityOperationFailure(
sdk.GetServerURL(),
metrics.KonnectEntityOperationDelete,
entityType,
time.Since(start),
statusCode,
)
} else {
metricRecorder.RecordKonnectEntityOperationSuccess(
sdk.GetServerURL(),
metrics.KonnectEntityOperationDelete,
entityType,
time.Since(start),
)
}
logOpComplete(ctx, start, DeleteOp, ent, err)

return err
Expand Down Expand Up @@ -297,6 +342,7 @@ func Update[
sdk sdkops.SDKWrapper,
syncPeriod time.Duration,
cl client.Client,
metricRecorder metrics.Recorder,
e TEnt,
) (ctrl.Result, error) {
now := time.Now()
Expand All @@ -312,7 +358,13 @@ func Update[
)
}

var err error
var (
err error

entityType = e.GetTypeName()
statusCode int
start = time.Now()
)
switch ent := any(e).(type) {
case *konnectv1alpha1.KonnectGatewayControlPlane:
err = updateControlPlane(ctx, sdk.GetControlPlaneSDK(), sdk.GetControlPlaneGroupSDK(), cl, ent)
Expand Down Expand Up @@ -367,6 +419,7 @@ func Update[
)
switch {
case errors.As(err, &errSDK):
statusCode = errSDK.StatusCode
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToUpdateReason, errSDK.Body)
case errors.As(err, &errRelationsFailed):
e.SetKonnectID(errRelationsFailed.KonnectID)
Expand All @@ -377,7 +430,23 @@ func Update[
SetKonnectEntityProgrammedCondition(e)
}

logOpComplete(ctx, now, UpdateOp, e, err)
if err != nil {
metricRecorder.RecordKonnectEntityOperationFailure(
sdk.GetServerURL(),
metrics.KonnectEntityOperationUpdate,
entityType,
time.Since(start),
statusCode,
)
} else {
metricRecorder.RecordKonnectEntityOperationSuccess(
sdk.GetServerURL(),
metrics.KonnectEntityOperationUpdate,
entityType,
time.Since(start),
)
}
logOpComplete(ctx, start, UpdateOp, e, err)

return ctrl.Result{}, IgnoreUnrecoverableAPIErr(err, loggerForEntity(ctx, e, UpdateOp))
}
Expand Down
5 changes: 3 additions & 2 deletions controller/konnect/ops/ops_controlplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

sdkmocks "github.com/kong/gateway-operator/controller/konnect/ops/sdk/mocks"
"github.com/kong/gateway-operator/internal/metrics"
"github.com/kong/gateway-operator/modules/manager/scheme"
"github.com/kong/gateway-operator/pkg/consts"

Expand Down Expand Up @@ -619,7 +620,7 @@ func TestCreateAndUpdateControlPlane_KubernetesMetadataConsistency(t *testing.T)
ID: "12345",
},
}, nil)
_, err := Create(ctx, sdk.SDK, fakeClient, cp)
_, err := Create(ctx, sdk.SDK, fakeClient, &metrics.MockRecorder{}, cp)
require.NoError(t, err)

t.Log("Triggering UpdateControlPlane with expected labels")
Expand All @@ -633,7 +634,7 @@ func TestCreateAndUpdateControlPlane_KubernetesMetadataConsistency(t *testing.T)
ID: "12345",
},
}, nil)
_, err = Update(ctx, sdk.SDK, 0, fakeClient, cp)
_, err = Update(ctx, sdk.SDK, 0, fakeClient, &metrics.MockRecorder{}, cp)
require.NoError(t, err)
}

Expand Down
5 changes: 3 additions & 2 deletions controller/konnect/ops/ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/kong/gateway-operator/controller/konnect/constraints"
sdkmocks "github.com/kong/gateway-operator/controller/konnect/ops/sdk/mocks"
"github.com/kong/gateway-operator/internal/metrics"
"github.com/kong/gateway-operator/modules/manager/scheme"
"github.com/kong/gateway-operator/pkg/consts"

Expand Down Expand Up @@ -237,7 +238,7 @@ func testCreate[
sdk = tc.sdkFunc(t, sdk)
}

_, err := Create(context.Background(), sdk, fakeClient, tc.entity)
_, err := Create(context.Background(), sdk, fakeClient, &metrics.MockRecorder{}, tc.entity)
if tc.expectedErrorContains != "" {
require.ErrorContains(t, err, tc.expectedErrorContains)
} else {
Expand Down Expand Up @@ -345,7 +346,7 @@ func testDelete[
sdk = tc.sdkFunc(t, sdk)
}

err := Delete(context.Background(), sdk, fakeClient, tc.entity)
err := Delete(context.Background(), sdk, fakeClient, &metrics.MockRecorder{}, tc.entity)
if tc.expectedError != "" {
require.ErrorContains(t, err, tc.expectedError)
return
Expand Down
8 changes: 8 additions & 0 deletions controller/konnect/ops/sdk/mocks/sdkfactory_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func NewMockSDKWrapperWithT(t *testing.T) *MockSDKWrapper {
}
}

const (
mockSDKServerURL = "http://mock-api.konnect.test"
)

func (m MockSDKWrapper) GetServerURL() string {
return mockSDKServerURL
}

func (m MockSDKWrapper) GetControlPlaneSDK() sdkops.ControlPlaneSDK {
return m.ControlPlaneSDK
}
Expand Down
12 changes: 11 additions & 1 deletion controller/konnect/ops/sdk/sdkfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ type SDKWrapper interface {
GetKeySetsSDK() KeySetsSDK
GetSNIsSDK() SNIsSDK
GetDataPlaneCertificatesSDK() DataPlaneClientCertificatesSDK

// GetServerURL returns the server URL for recording metrics.
GetServerURL() string
}

type sdkWrapper struct {
sdk *sdkkonnectgo.SDK
serverURL string
sdk *sdkkonnectgo.SDK
}

var _ SDKWrapper = sdkWrapper{}

// GetServerURL returns the Konnect server URL for recording metrics.
func (w sdkWrapper) GetServerURL() string {
return w.serverURL
}

// GetControlPlaneSDK returns the SDK to operate Konnect control planes.
func (w sdkWrapper) GetControlPlaneSDK() ControlPlaneSDK {
return w.sdk.ControlPlanes
Expand Down Expand Up @@ -165,6 +174,7 @@ func NewSDKFactory() SDKFactory {
// NewKonnectSDK creates a new Konnect SDK.
func (f sdkFactory) NewKonnectSDK(serverURL string, token SDKToken) SDKWrapper {
return sdkWrapper{
serverURL: serverURL,
sdk: sdkkonnectgo.New(
sdkkonnectgo.WithSecurity(
sdkkonnectcomp.Security{
Expand Down
19 changes: 16 additions & 3 deletions controller/konnect/reconciler_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
sdkops "github.com/kong/gateway-operator/controller/konnect/ops/sdk"
"github.com/kong/gateway-operator/controller/pkg/log"
"github.com/kong/gateway-operator/controller/pkg/patch"
"github.com/kong/gateway-operator/internal/metrics"
"github.com/kong/gateway-operator/pkg/consts"
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"

Expand All @@ -45,6 +46,8 @@ type KonnectEntityReconciler[T constraints.SupportedKonnectEntityType, TEnt cons
Client client.Client
SyncPeriod time.Duration
MaxConcurrentReconciles uint

MetricRecoder metrics.Recorder
}

// KonnectEntityReconcilerOption is a functional option for the KonnectEntityReconciler.
Expand All @@ -71,6 +74,15 @@ func WithKonnectMaxConcurrentReconciles[T constraints.SupportedKonnectEntityType
}
}

// WithMetricRecoder sets the metric recorder to record metrics of Konnect entity operations of the reconciler.
func WithMetricRecorder[T constraints.SupportedKonnectEntityType, TEnt constraints.EntityType[T]](
metricRecorder metrics.Recorder,
) KonnectEntityReconcilerOption[T, TEnt] {
return func(r *KonnectEntityReconciler[T, TEnt]) {
r.MetricRecoder = metricRecorder
}
}

// NewKonnectEntityReconciler returns a new KonnectEntityReconciler for the given
// Konnect entity type.
func NewKonnectEntityReconciler[
Expand All @@ -88,6 +100,7 @@ func NewKonnectEntityReconciler[
Client: client,
SyncPeriod: consts.DefaultKonnectSyncPeriod,
MaxConcurrentReconciles: consts.DefaultKonnectMaxConcurrentReconciles,
MetricRecoder: &metrics.MockRecorder{},
}
for _, opt := range opts {
opt(r)
Expand Down Expand Up @@ -469,7 +482,7 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
}

if controllerutil.RemoveFinalizer(ent, KonnectCleanupFinalizer) {
if err := ops.Delete[T, TEnt](ctx, sdk, r.Client, ent); err != nil {
if err := ops.Delete[T, TEnt](ctx, sdk, r.Client, r.MetricRecoder, ent); err != nil {
if res, errStatus := patch.StatusWithCondition(
ctx, r.Client, ent,
konnectv1alpha1.KonnectEntityProgrammedConditionType,
Expand Down Expand Up @@ -500,7 +513,7 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
// https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/controller_utils.go
if status := ent.GetKonnectStatus(); status == nil || status.GetKonnectID() == "" {
obj := ent.DeepCopyObject().(client.Object)
_, err := ops.Create[T, TEnt](ctx, sdk, r.Client, ent)
_, err := ops.Create[T, TEnt](ctx, sdk, r.Client, r.MetricRecoder, ent)

// TODO: this is actually not 100% error prone because when status
// update fails we don't store the Konnect ID and hence the reconciler
Expand Down Expand Up @@ -557,7 +570,7 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
return ctrl.Result{}, nil
}

if res, err := ops.Update[T, TEnt](ctx, sdk, r.SyncPeriod, r.Client, ent); err != nil {
if res, err := ops.Update[T, TEnt](ctx, sdk, r.SyncPeriod, r.Client, r.MetricRecoder, ent); err != nil {
setServerURLAndOrgID(ent, serverURL, apiAuth.Status.OrganizationID)
if errUpd := r.Client.Status().Update(ctx, ent); errUpd != nil {
if k8serrors.IsConflict(errUpd) {
Expand Down
Loading
Loading