-
Notifications
You must be signed in to change notification settings - Fork 87
feat(telemetry): extend TelemetryPolicy with logging stanza #2179
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
andresllh
wants to merge
2
commits into
Kuadrant:main
Choose a base branch
from
andresllh:feat/telemetry-logging-fields
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
49 changes: 48 additions & 1 deletion
49
cmd/extensions/telemetry-policy/api/v1alpha1/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
199 changes: 199 additions & 0 deletions
199
cmd/extensions/telemetry-policy/internal/controller/telemetrypolicy_reconciler_test.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,199 @@ | ||
| //go:build unit | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| celref "github.com/google/cel-go/common/types/ref" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
|
|
||
| "github.com/kuadrant/kuadrant-operator/cmd/extensions/telemetry-policy/api/v1alpha1" | ||
| "github.com/kuadrant/kuadrant-operator/pkg/extension/types" | ||
| ) | ||
|
|
||
| type addDataCall struct { | ||
| domain types.Domain | ||
| binding string | ||
| expression string | ||
| } | ||
|
|
||
| type mockKuadrantCtx struct { | ||
| calls []addDataCall | ||
| failOn string // return error when binding matches this value | ||
| failErr error | ||
| } | ||
|
|
||
| func (m *mockKuadrantCtx) AddDataTo(_ context.Context, _ types.Policy, domain types.Domain, binding, expression string) error { | ||
| m.calls = append(m.calls, addDataCall{domain: domain, binding: binding, expression: expression}) | ||
| if m.failOn != "" && binding == m.failOn { | ||
| return m.failErr | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockKuadrantCtx) Resolve(context.Context, types.Policy, string, bool) (celref.Val, error) { | ||
| return nil, nil | ||
| } | ||
| func (m *mockKuadrantCtx) ResolvePolicy(context.Context, types.Policy, string, bool) (types.Policy, error) { | ||
| return nil, nil | ||
| } | ||
| func (m *mockKuadrantCtx) ReconcileObject(context.Context, client.Object, client.Object, types.MutateFn) (client.Object, error) { | ||
| return nil, nil | ||
| } | ||
| func (m *mockKuadrantCtx) RegisterActionMethod(_ context.Context, _ types.Policy, _ types.ActionMethodConfig) error { | ||
| return nil | ||
| } | ||
| func (m *mockKuadrantCtx) NewPipeline(types.Policy) types.Pipeline { return nil } | ||
|
|
||
| func newTestPolicy(metrics map[string]string, loggingFields map[string]string) *v1alpha1.TelemetryPolicy { | ||
| pol := &v1alpha1.TelemetryPolicy{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-policy", | ||
| Namespace: "default", | ||
| Generation: 1, | ||
| }, | ||
| Spec: v1alpha1.TelemetryPolicySpec{ | ||
| TargetRef: gatewayapiv1alpha2.LocalPolicyTargetReferenceWithSectionName{ | ||
| LocalPolicyTargetReference: gatewayapiv1alpha2.LocalPolicyTargetReference{ | ||
| Group: "gateway.networking.k8s.io", | ||
| Kind: "Gateway", | ||
| Name: "my-gw", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| if metrics != nil { | ||
| pol.Spec.Metrics = &v1alpha1.MetricsSpec{Default: v1alpha1.MetricsConfig{Labels: metrics}} | ||
| } | ||
| if loggingFields != nil { | ||
| pol.Spec.Logging = &v1alpha1.LoggingSpec{Default: v1alpha1.LoggingConfig{Fields: loggingFields}} | ||
| } | ||
| return pol | ||
| } | ||
|
|
||
| func TestReconcileSpec_LoggingFieldsOnly(t *testing.T) { | ||
| mock := &mockKuadrantCtx{} | ||
| r := &TelemetryPolicyReconciler{ | ||
| ExtensionBase: types.ExtensionBase{Logger: logr.Discard()}, | ||
| } | ||
| pol := newTestPolicy(nil, map[string]string{ | ||
| "client_identity": "auth.identity.sub", | ||
| "request_path": "request.path", | ||
| }) | ||
|
|
||
| status, err := r.reconcileSpec(context.Background(), pol, mock) | ||
| if err != nil { | ||
| t.Fatalf("reconcileSpec returned error: %v", err) | ||
| } | ||
| if status == nil { | ||
| t.Fatal("reconcileSpec returned nil status") | ||
| } | ||
|
|
||
| if len(mock.calls) != 2 { | ||
| t.Fatalf("expected 2 AddDataTo calls, got %d", len(mock.calls)) | ||
| } | ||
|
|
||
| callMap := make(map[string]addDataCall, len(mock.calls)) | ||
| for _, c := range mock.calls { | ||
| callMap[c.binding] = c | ||
| } | ||
|
|
||
| if c, ok := callMap["logging.fields.client_identity"]; !ok { | ||
| t.Error("missing AddDataTo call for logging.fields.client_identity") | ||
| } else { | ||
| if c.expression != "auth.identity.sub" { | ||
| t.Errorf("client_identity expression = %q, want %q", c.expression, "auth.identity.sub") | ||
| } | ||
| if c.domain != types.DomainRequest { | ||
| t.Errorf("client_identity domain = %v, want DomainRequest", c.domain) | ||
| } | ||
| } | ||
|
|
||
| if c, ok := callMap["logging.fields.request_path"]; !ok { | ||
| t.Error("missing AddDataTo call for logging.fields.request_path") | ||
| } else { | ||
| if c.expression != "request.path" { | ||
| t.Errorf("request_path expression = %q, want %q", c.expression, "request.path") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestReconcileSpec_MetricsAndLogging(t *testing.T) { | ||
| mock := &mockKuadrantCtx{} | ||
| r := &TelemetryPolicyReconciler{ | ||
| ExtensionBase: types.ExtensionBase{Logger: logr.Discard()}, | ||
| } | ||
| pol := newTestPolicy( | ||
| map[string]string{"model": "responseBodyJSON('/model')"}, | ||
| map[string]string{"client_identity": "auth.identity.sub"}, | ||
| ) | ||
|
|
||
| _, err := r.reconcileSpec(context.Background(), pol, mock) | ||
| if err != nil { | ||
| t.Fatalf("reconcileSpec returned error: %v", err) | ||
| } | ||
|
|
||
| if len(mock.calls) != 2 { | ||
| t.Fatalf("expected 2 AddDataTo calls, got %d", len(mock.calls)) | ||
| } | ||
|
|
||
| callMap := make(map[string]addDataCall, len(mock.calls)) | ||
| for _, c := range mock.calls { | ||
| callMap[c.binding] = c | ||
| } | ||
|
|
||
| if _, ok := callMap["metrics.labels.model"]; !ok { | ||
| t.Error("missing AddDataTo call for metrics.labels.model") | ||
| } | ||
| if _, ok := callMap["logging.fields.client_identity"]; !ok { | ||
| t.Error("missing AddDataTo call for logging.fields.client_identity") | ||
| } | ||
| } | ||
|
|
||
| func TestReconcileSpec_LoggingFieldError(t *testing.T) { | ||
| expectedErr := fmt.Errorf("binding failed") | ||
| mock := &mockKuadrantCtx{ | ||
| failOn: "logging.fields.bad_field", | ||
| failErr: expectedErr, | ||
| } | ||
| r := &TelemetryPolicyReconciler{ | ||
| ExtensionBase: types.ExtensionBase{Logger: logr.Discard()}, | ||
| } | ||
| pol := newTestPolicy(nil, map[string]string{ | ||
| "bad_field": "invalid.expression", | ||
| }) | ||
|
|
||
| status, err := r.reconcileSpec(context.Background(), pol, mock) | ||
| if err != expectedErr { | ||
| t.Fatalf("expected error %v, got %v", expectedErr, err) | ||
| } | ||
| if status == nil { | ||
| t.Fatal("expected error status, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestReconcileSpec_EmptySpec(t *testing.T) { | ||
| mock := &mockKuadrantCtx{} | ||
| r := &TelemetryPolicyReconciler{ | ||
| ExtensionBase: types.ExtensionBase{Logger: logr.Discard()}, | ||
| } | ||
| pol := newTestPolicy(nil, nil) | ||
|
|
||
| status, err := r.reconcileSpec(context.Background(), pol, mock) | ||
| if err != nil { | ||
| t.Fatalf("reconcileSpec returned error: %v", err) | ||
| } | ||
| if status == nil { | ||
| t.Fatal("reconcileSpec returned nil status") | ||
| } | ||
| if len(mock.calls) != 0 { | ||
| t.Errorf("expected 0 AddDataTo calls for empty spec, got %d", len(mock.calls)) | ||
| } | ||
| } |
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.