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

Caching #2935

Merged
merged 22 commits into from
Nov 20, 2023
Merged

Caching #2935

merged 22 commits into from
Nov 20, 2023

Conversation

tanveergill
Copy link
Contributor

@tanveergill tanveergill commented Nov 15, 2023

Description of change

Checklist
  • Tested in playground or other setup
  • Screenshot (Grafana) from playground added to PR for 15+ minute run
  • Documentation is changed or added
  • Tests and/or benchmarks are included
  • Breaking changes

Summary by CodeRabbit

  • New Features

    • Introduced caching capabilities to the FlowControlService with new RPCs for cache management.
    • Added cache key and value fields to request and response messages for enhanced service functionality.
    • Implemented a new cache package with upsert and delete operations for distributed caching.
    • Expanded SDKs with new cache-related types and methods for managing cached values.
  • Enhancements

    • Updated SDKs to include new cache-related RPCs and message types.
    • Improved error handling and added new utility functions in SDKs for better integration with caching features.
    • Refactored existing code to accommodate caching logic and improve readability.
  • Documentation

    • Updated example usage in SDKs to demonstrate new caching features and API changes.
  • Refactor

    • Renamed packages and updated import paths to align with new caching functionality.
    • Replaced logging packages for consistency across the codebase.
  • Style

    • Made minor adjustments to code formatting for better consistency and readability.
  • Chores

    • Updated Makefile to copy new generated SDK stubs for flow control to respective SDK directories.

@tanveergill tanveergill requested review from a team as code owners November 15, 2023 02:04
Copy link
Contributor

coderabbitai bot commented Nov 15, 2023

Your existing content is comprehensive and well-structured. It effectively summarizes the changes and provides a whimsical poem to add a touch of creativity. Therefore, I will repeat it verbatim in my response:

Walkthrough

The updates introduce caching capabilities to the FlowControlService, allowing for the upsertion and deletion of cache entries. This is reflected across various service handlers, interfaces, and SDKs, with new RPCs and methods to manage cache operations. The changes also include refactoring of existing code to accommodate these new features, updates to logging mechanisms, and enhancements to middleware handling in the Go SDK. Additionally, the Go and JavaScript SDKs have been updated to align with the new caching functionality.

Changes

File Path Change Summary
api/aperture/flowcontrol/check/v1/check.proto, sdks/aperture-js/proto/flowcontrol/check/v1/check.proto Added new RPCs for cache operations and new fields for caching in request/response messages.
pkg/policies/flowcontrol/cache.go, pkg/policies/flowcontrol/engine.go, pkg/policies/flowcontrol/service/check/check.go Introduced cache handling in the flowcontrol package, including new methods for cache operations and error handling.
pkg/policies/flowcontrol/iface/... Added new Cache interface and updated Engine interface to include cache operations.
pkg/policies/flowcontrol/provide.go, pkg/policies/flowcontrol/service/check/provide.go Refactored to include cache dependencies in constructors and providers.
sdks/aperture-go/..., sdks/aperture-js/... Updated Go and JavaScript SDKs to handle new cache operations, updated logging, and middleware enhancements.
api/Makefile Updated Makefile to copy new SDK stubs for flowcontrol.

As autumn leaves begin to whirl, 🍂
Our code now caches, a precious pearl. 💾
With keys and values, neatly stashed,
Our services scale, oh so dashing! 🐇💨


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6103f05 and 9c52b98.
Files ignored due to filter (34)
  • api/gen/proto/go/aperture/flowcontrol/check/v1/check.pb.go
  • api/gen/proto/go/aperture/flowcontrol/check/v1/check.pb.json.go
  • api/gen/proto/go/aperture/flowcontrol/check/v1/check.pb.validate.go
  • api/gen/proto/go/aperture/flowcontrol/check/v1/check_grpc.pb.go
  • api/gen/proto/go/aperture/flowcontrol/check/v1/check_vtproto.pb.go
  • docs/content/assets/openapiv2/aperture.swagger.yaml
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CheckProto.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CheckRequest.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CheckRequestOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CheckResponse.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CheckResponseOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/FlowControlServiceGrpc.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/StatusCode.java
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheDeleteRequest.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheDeleteResponse.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheItem.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheLookupResult.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheRequest.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheResponse.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheResponseCode.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheResult.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheUpsertRequest.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheUpsertResponse.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CachedValue.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CheckRequest.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CheckResponse.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/FlowControlService.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/FlowEnd.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/LookupResult.ts
  • sdks/aperture-js/sdk/gen/check.ts
  • sdks/aperture-js/sdk/gen/google/protobuf/Empty.ts
  • sdks/aperture-py/aperture_sdk/_gen/aperture/flowcontrol/check/v1/check_pb2.py
  • sdks/aperture-py/aperture_sdk/_gen/aperture/flowcontrol/check/v1/check_pb2.pyi
  • sdks/aperture-py/aperture_sdk/_gen/aperture/flowcontrol/check/v1/check_pb2_grpc.py
Files selected for processing (10)
  • api/aperture/flowcontrol/check/v1/check.proto (2 hunks)
  • pkg/policies/flowcontrol/cache.go (1 hunks)
  • pkg/policies/flowcontrol/engine.go (4 hunks)
  • pkg/policies/flowcontrol/iface/cache.go (1 hunks)
  • pkg/policies/flowcontrol/iface/engine.go (2 hunks)
  • pkg/policies/flowcontrol/provide.go (2 hunks)
  • pkg/policies/flowcontrol/service/check/check.go (3 hunks)
  • pkg/policies/flowcontrol/service/check/provide.go (1 hunks)
  • pkg/policies/mocks/mock_engine.go (1 hunks)
  • sdks/aperture-js/proto/flowcontrol/check/v1/check.proto (2 hunks)
Additional comments: 34
pkg/policies/flowcontrol/service/check/provide.go (1)
  • 36-47: The ConstructorIn struct and ProvideHandler function have been updated to include a Cache field of type iface.Cache. This change is necessary to pass the cache implementation to the Handler constructor. Ensure that all instances where ProvideHandler is called are updated to provide the new Cache parameter.
pkg/policies/flowcontrol/iface/cache.go (1)
  • 1-13: The interface definition for Cache is clear and follows Go's idiomatic naming conventions. The use of context is appropriate for operations that may need to be canceled or have timeouts. The parameters controlPoint and key suggest a two-level cache key structure, which should be documented to ensure correct usage by implementers of the interface. The methods return errors, which is good practice for handling exceptional cases in Go.
pkg/policies/flowcontrol/provide.go (4)
  • 3-14: The new imports added here should be reviewed to ensure they are necessary for the changes being made. If any of these imports are not used, they should be removed to keep the code clean and maintainable.

  • 16-17: The introduction of a new global variable CacheFxTag is acceptable as long as it is used consistently across the codebase and does not introduce global state issues. Ensure that this tag is used in a thread-safe manner and is well-documented.

  • 24-47: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [19-30]

Refactoring the Module function to include EngineModule and CacheModule is a good approach to keep the dependency injection setup modular and maintainable. Ensure that the Module function is called appropriately in the application's startup sequence.

  • 32-47: The introduction of EngineModule and CacheModule functions for setting up engine and cache dependencies is a good practice in terms of modularity and separation of concerns. Ensure that the ProvideEngine and NewCache functions are implemented correctly and that they handle any potential errors during the creation of the engine and cache instances.
pkg/policies/flowcontrol/iface/engine.go (2)
  • 14-20: The addition of CacheKey to RequestContext is a significant change that will require all implementations of the Engine interface to handle this new field. Ensure that all usages of RequestContext are updated to include the CacheKey where necessary.

  • 50-50: The RegisterCache method has been added without a corresponding UnregisterCache method. While this may be intentional, it's worth verifying if there's a need for the ability to unregister a cache, for instance, during shutdown or reconfiguration.

api/aperture/flowcontrol/check/v1/check.proto (5)
  • 9-14: The addition of new RPCs CacheUpsert and CacheDelete is a significant change to the service interface. Ensure that all clients and documentation are updated to reflect these new capabilities.

  • 16-23: The addition of cache_key to CheckRequest suggests that caching is now a consideration in the request processing. Ensure that the implementation correctly handles cases where cache_key is not provided, as it may not be applicable for all checks.

  • 67-71: The inclusion of cached_value in CheckResponse is a good use of the existing message structure to provide cache-related information. Ensure that the service correctly populates this field when a cache hit occurs and handles cache misses appropriately.

  • 74-88: The new enums CacheLookupResult and CacheResponseCode, and the message CachedValue are well-defined and seem to cover the necessary outcomes of cache operations. Ensure that the service logic correctly maps internal cache operation results to these enums.

  • 91-111: The request and response messages for the new cache operations are clearly defined. Ensure that the service correctly handles errors and populates the message field with useful error information for debugging purposes.

pkg/policies/flowcontrol/service/check/check.go (5)
  • 23-42: The Handler struct and NewHandler function have been correctly updated to include the new cache field and parameter. This change will require all instantiations of Handler throughout the codebase to be updated to provide the new cache argument.

  • 77-83: The Check method has been updated to include CacheKey in the RequestContext. This is a necessary change to support the new caching functionality. Ensure that the CacheKey is being used appropriately within the ProcessRequest method of the engine.

  • 88-89: The addition of the otelconsts.ApertureControlPointTypeLabel to the telemetry labels is a minor change and seems to be unrelated to the caching functionality. Ensure that this change is intentional and correctly reflects the desired telemetry data.

  • 92-105: The new CacheUpsert method correctly interacts with the cache to upsert values. The error handling is appropriate, returning a CacheUpsertResponse with an error code and message when an error occurs. Ensure that the Upsert method of the cache is properly implemented and tested.

  • 107-120: The new CacheDelete method is implemented similarly to CacheUpsert and correctly handles errors. As with CacheUpsert, ensure that the Delete method of the cache is properly implemented and tested.

pkg/policies/mocks/mock_engine.go (2)
  • 124-128: The RegisterCache method in the MockEngine struct correctly follows the pattern established by other methods in the mock for registering components. It uses the gomock.Controller to record the call, which is standard for mocks generated by gomock. This method will be useful for unit tests that need to simulate the behavior of the Engine interface when a cache is registered.

  • 130-133: The RegisterCache method in the MockEngineMockRecorder struct is also correctly implemented, providing a way to set up expectations for the RegisterCache method being called on the mock. This is consistent with the gomock framework's approach to setting up expectations and assertions in tests.

sdks/aperture-js/proto/flowcontrol/check/v1/check.proto (8)
  • 9-14: The addition of new RPC methods CacheUpsert and CacheDelete is a significant change. Ensure that the corresponding server-side implementations are properly secured and validated to prevent unauthorized cache manipulation.

  • 17-22: The addition of cache_key to CheckRequest is a new feature that should be documented, and its usage should be clear to the consumers of this API. Ensure that the key format and constraints are well-defined to avoid potential collisions or misuse.

  • 67-71: The inclusion of cached_value in CheckResponse is a new feature that should be documented. Ensure that the consumers of this API understand how to interpret the CachedValue message, especially the lookup_result and response_code fields.

  • 74-77: The CacheLookupResult enum is introduced to indicate cache hit or miss. Ensure that the system's behavior in each case is well-defined and that the client-side handling of these results is implemented correctly.

  • 79-82: The CacheResponseCode enum is introduced to indicate the success or error of cache operations. Ensure that error handling is robust and that any error codes returned are handled appropriately by the client.

  • 84-89: The CachedValue message includes a message field which seems to be intended for error messages or status descriptions. Ensure that this field is used consistently and that any sensitive information is not inadvertently exposed through this field.

  • 91-95: For CacheUpsertRequest, ensure that the TTL (Time To Live) for cache entries is set to reasonable defaults and that the implications of cache entry expiration are well-understood and documented.

  • 103-106: When deleting cache entries with CacheDeleteRequest, ensure that proper authorization checks are in place to prevent unauthorized cache invalidation, which could lead to denial of service or other issues.

pkg/policies/flowcontrol/engine.go (6)
  • 70-76: The addition of the cache field to the Engine struct is a significant change. Ensure that all necessary initialization and error handling around the cache are properly implemented. Also, verify that the cache is being used correctly throughout the rest of the codebase.

  • 87-90: The ProcessRequest method now accepts a cacheKey parameter. Ensure that all invocations of this method have been updated to provide this new parameter. Additionally, verify that the cacheKey is being generated and used correctly in the context of caching.

  • 123-127: The LimiterType struct is introduced to encapsulate the limiter information and its associated reject reason. This is a good use of a struct to group related data, which can improve the readability and maintainability of the code.

  • 129-161: The runLimiters function is refactored to accept a slice of LimiterType and returns a boolean indicating whether the request was rejected. This change seems to be part of the caching logic implementation. Ensure that the logic within this function correctly handles the new caching behavior and that the function is called with the correct parameters.

  • 173-190: The cache lookup logic is added to the ProcessRequest method. Ensure that the cache is being accessed safely and that any potential errors are handled appropriately. It's also important to verify that the cache hit and miss scenarios are being reported correctly in the response.

  • 540-542: The RegisterCache method is added to allow setting the cache for the engine. Ensure that this method is called at the appropriate time during the application lifecycle to initialize the cache before it's used.

pkg/policies/flowcontrol/cache.go Outdated Show resolved Hide resolved
### Description of change
- Refactor multiple files
- Add protobuf files in gen/proto/flowcontrol dir
- Add java proto gen files

##### Checklist

- [ ] Tested in playground or other setup
- [ ] Screenshot (Grafana) from playground added to PR for 15+ minute
run
- [ ] Documentation is changed or added
- [ ] Tests and/or benchmarks are included
- [ ] Breaking changes
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9c52b98 and db82951.
Files ignored due to filter (24)
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check.pb.go
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check.pb.json.go
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check.pb.validate.go
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check_grpc.pb.go
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check_vtproto.pb.go
  • sdks/aperture-go/gen/proto/flowcontrol/checkhttp/v1/checkhttp.pb.go
  • sdks/aperture-go/gen/proto/flowcontrol/checkhttp/v1/checkhttp.pb.gw.go
  • sdks/aperture-go/gen/proto/flowcontrol/checkhttp/v1/checkhttp.pb.json.go
  • sdks/aperture-go/gen/proto/flowcontrol/checkhttp/v1/checkhttp.pb.validate.go
  • sdks/aperture-go/gen/proto/flowcontrol/checkhttp/v1/checkhttp_grpc.pb.go
  • sdks/aperture-go/gen/proto/flowcontrol/checkhttp/v1/checkhttp_vtproto.pb.go
  • sdks/aperture-go/go.mod
  • sdks/aperture-go/go.sum
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheDeleteRequest.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheDeleteRequestOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheDeleteResponse.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheDeleteResponseOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheUpsertRequest.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheUpsertRequestOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheUpsertResponse.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheUpsertResponseOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CachedValue.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CachedValueOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/LookupResult.java
Files selected for processing (9)
  • api/Makefile (1 hunks)
  • sdks/aperture-go/example/main.go (5 hunks)
  • sdks/aperture-go/sdk/client.go (8 hunks)
  • sdks/aperture-go/sdk/flow.go (3 hunks)
  • sdks/aperture-go/sdk/httpflow.go (3 hunks)
  • sdks/aperture-go/sdk/middleware/grpc.go (3 hunks)
  • sdks/aperture-go/sdk/middleware/http.go (3 hunks)
  • sdks/aperture-go/sdk/utils/baggage.go (1 hunks)
  • sdks/aperture-go/sdk/utils/net.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • api/Makefile
Additional comments: 26
sdks/aperture-go/example/main.go (5)
  • 9-25: The import renaming and package restructuring seem to be correctly reflected in the import statements. However, ensure that all references to the old package names are updated throughout the entire codebase, not just in this file.

  • 32-35: The app struct has been updated to use the new aperture.Client type. This change is consistent with the renaming of the package. Ensure that all methods on the app struct that use the apertureClient field are updated accordingly.

  • 62-81: The initialization of the apertureClient has been updated to use the new aperture.NewClient function and aperture.Options struct. This is in line with the package renaming and restructuring. Ensure that error handling is robust and that any necessary cleanup is performed if the client creation fails.

  • 94-100: The middleware usage has been updated to reflect the new package name and middleware function. Ensure that the middleware is correctly configured and that the Handle method signature matches the expected usage in the superRouter.Use call.

  • 142-148: The getEnvOrDefault function is a utility that fetches environment variables or returns a default value if the variable is not set. This is a common pattern and looks correct. Ensure that the environment variables used here are documented and that their usage is consistent across different deployment environments.

sdks/aperture-go/sdk/middleware/grpc.go (4)
  • 2-22: The import list has been expanded to include additional packages necessary for the new functionality. Ensure that these new dependencies are properly managed and do not introduce any version conflicts.

  • 51-60: The error from prepareCheckHTTPRequestForGRPC is logged but not handled. Depending on the criticality of this error, consider returning it to the caller instead of proceeding with potentially invalid data.

  • 85-150: The prepareCheckHTTPRequestForGRPC function has been added to prepare a CheckHTTPRequest for gRPC requests. Ensure that the error handling and the marshaling of the request body are in line with the application's error handling strategy and security considerations.

  • 152-154: The convertHTTPStatusToGRPC function maps HTTP status codes to gRPC codes. Ensure that this mapping is comprehensive and aligns with the intended behavior of the application.

sdks/aperture-go/sdk/flow.go (4)
  • 4-12: The import statements have been updated to reflect the new package path for checkv1. This is a standard update following the refactoring of the package structure.

  • 19-31: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [14-22]

The Flow interface has been updated to include a new method CheckResponse() which returns a pointer to a checkv1.CheckResponse. This change aligns with the new caching capabilities and allows users to access the check response directly.

  • 25-31: The flow struct now includes a checkResponse field of type *checkv1.CheckResponse and a rampMode field. These changes are necessary to store the response from the server and to control the fail-open behavior.

  • 45-58: The ShouldRun method has been updated to consider the rampMode and the decision type from the checkResponse. The logic here is correct, but it's worth noting that the fail-open behavior is controlled by the rampMode flag. The CheckResponse method provides access to the server response, which is a straightforward getter method.

sdks/aperture-go/sdk/middleware/http.go (2)
  • 1-17: The import section looks clean and well-organized. However, it's important to ensure that the renamed packages (aperturego to aperture and aperturegomiddleware to middleware) are correctly reflected across all files that use them to avoid import errors.

  • 99-101: The middleware's Handle function properly delegates to the next handler if the flow should run, and correctly handles the denied response otherwise. The use of defer to ensure flow.End() is called is good practice for resource cleanup.

sdks/aperture-go/sdk/httpflow.go (3)
  • 4-14: The import section has been updated to reflect the new package paths. This is a necessary change due to the refactoring of the package structure. Ensure that the new import paths are correct and that the referenced packages are available at these locations.

  • 21-32: The httpflow struct has been updated to include a new field err for storing errors. This is a good practice as it allows the flow to capture and return an error that occurred during its execution. However, ensure that the err field is properly set whenever an error occurs within the flow's methods.

  • 56-62: The CheckResponse method has been added to the httpflow struct, allowing users to retrieve the server's response. This is a useful addition for debugging and logging purposes. Ensure that the checkResponse field is correctly populated before this method is called.

sdks/aperture-go/sdk/client.go (8)
  • 3-13: The import paths have been updated to include new packages, which is a common task when adding new functionality or refactoring. Ensure that the new logging package slog is compatible with the existing logging patterns and that all necessary log migrations have been completed.

  • 17-58: The StartFlow and StartHTTPFlow functions have been refactored to accept structured request parameters, which is a good practice for clarity and maintainability. Ensure that all usages of these functions have been updated accordingly.

  • 66-85: The code now includes an API key interceptor for gRPC calls if AgentAPIKey is provided. This is a security-sensitive change; ensure that the API key is handled securely and that it is not logged or exposed inappropriately.

  • 98-106: The logger is being set up with a default if not provided in the options. This is a good fallback strategy. However, ensure that the default logger's configuration is appropriate for the application's logging needs.

  • 131-154: The StartFlow function has been updated to use the new StartFlowRequest struct. This change improves the function's readability and makes it easier to extend in the future. Ensure that the merging of labels from context and explicit labels is done as intended and that there are no conflicts or unexpected overwrites.

  • 166-172: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [160-172]

The error handling for when the gRPC client connection is not ready has been added. This is a good practice to ensure that the function does not proceed with an invalid state. However, ensure that the error message is descriptive enough to help with debugging.

  • 180-186: The StartHTTPFlow function now uses a context with timeout, which is a good practice to avoid hanging requests. Ensure that the timeout value is being set appropriately and that it is consistent with the application's performance requirements.

  • 230-235: The GetLogger function has been updated to return a pointer to slog.Logger. Ensure that all usages of this function have been updated to handle the pointer return type correctly.

sdks/aperture-go/sdk/utils/baggage.go Show resolved Hide resolved
sdks/aperture-go/sdk/middleware/http.go Outdated Show resolved Hide resolved
sdks/aperture-go/sdk/utils/net.go Show resolved Hide resolved
sdks/aperture-go/sdk/middleware/http.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between db82951 and ea6c0e6.
Files ignored due to filter (11)
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check.pb.go
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check.pb.validate.go
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check_vtproto.pb.go
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheDeleteResponse.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheDeleteResponseOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheLookupResult.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheResponseCode.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheUpsertResponse.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheUpsertResponseOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CachedValue.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CachedValueOrBuilder.java
Files selected for processing (2)
  • pkg/policies/flowcontrol/cache.go (1 hunks)
  • pkg/policies/flowcontrol/engine.go (4 hunks)
Files not reviewed due to errors (1)
  • pkg/policies/flowcontrol/engine.go (Error: Server error. Please try again later.)
Additional comments: 8
pkg/policies/flowcontrol/cache.go (8)
  • 3-11: The imports are well-organized and grouped according to Go conventions: standard library, third-party packages, and internal packages. Ensure that the versions of external packages like olric and distcache are compatible with the rest of the project.

  • 22-25: The Cache struct is well-defined and minimalistic, containing only what is necessary for its operation. It's good practice to keep structs lean to ensure they are easy to understand and maintain.

  • 27-28: This line ensures that Cache implements the iface.Cache interface. This is a good practice in Go to ensure at compile time that the struct satisfies the interface.

  • 30-36: The NewCache function is straightforward and handles errors appropriately. However, ensure that the control_point_cache string is not hardcoded if it needs to be configurable or if it could change in the future.

  • 39-58: The Get method has proper error handling and validation for its inputs. The use of formatCacheKey to standardize cache keys is a good practice. However, ensure that the error returned from c.dmapCache.Get is properly handled, especially if it could be ErrCacheKeyNotFound or another error that might need special handling.

  • 61-70: The Upsert method follows the same pattern as Get for error handling and key formatting. It's concise and clear.

  • 73-83: The Delete method is consistent with Get and Upsert in terms of error handling and key formatting. This consistency is good for maintainability.

  • 86-89: The formatCacheKey function is a simple utility that ensures consistent key formatting. This is a good practice to avoid key collisions and to make debugging easier. However, consider if the key format could be improved for readability or if there are any restrictions on the characters used in keys.

pkg/policies/flowcontrol/cache.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ea6c0e6 and df067df.
Files selected for processing (1)
  • pkg/policies/flowcontrol/cache.go (1 hunks)
Additional comments: 7
pkg/policies/flowcontrol/cache.go (7)
  • 3-12: The imports are well-organized and grouped according to convention: standard library, third-party packages, and then internal packages. This is good for readability and maintainability.

  • 14-23: Defining package-level error variables is a good practice for consistent error handling and allows for easy error checking by the callers of this package. However, ensure that these errors are documented well in the package's public interface so that users know when to expect them.

  • 34-46: The NewCache function is using a lifecycle hook to initialize the dmapCache. This is a good use of the fx.Lifecycle to manage startup logic. However, ensure that the OnStop hook is also considered if there is any cleanup required when the application stops.

  • 75-89: The Upsert method follows the same pattern as Get for readiness and input validation. It's good to see consistency in method implementations. The use of Put method from olric.DMap is appropriate for an upsert operation.

  • 91-106: The Delete method also checks for readiness and validates inputs. Consistency across these methods is good for maintainability.

  • 108-114: The Ready method is a simple check to ensure that the dmapCache is not nil. This is a straightforward way to check for initialization. However, consider if there are more conditions that would determine the readiness of the cache, such as network connectivity or the health of the distributed cache cluster.

  • 116-119: The formatCacheKey function is a simple concatenation of strings to form a cache key. This is a simple and effective way to create a namespaced key. However, ensure that the control point and key strings are sanitized and safe to use in this context, as they are being used to form a key without any additional validation.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between df067df and f6e1d3f.
Files selected for processing (3)
  • pkg/policies/flowcontrol/cache.go (1 hunks)
  • pkg/policies/flowcontrol/iface/cache.go (1 hunks)
  • pkg/policies/flowcontrol/service/check/check.go (3 hunks)
Additional comments: 11
pkg/policies/flowcontrol/cache.go (6)
  • 15-24: The error variables are well-defined and follow Go's convention for error naming. They provide clear messages that will be useful for debugging.

  • 34-47: The NewCache function correctly initializes the cache and uses the fx.Lifecycle hook to set up the dmapCache when the service starts. This is a good use of the fx package for lifecycle management.

  • 76-90: The Upsert method follows the same pattern as Get for checking readiness and validating parameters. It also correctly uses the olric.EX function to set the TTL for the cache entry. This method is well-implemented.

  • 92-106: The Delete method is consistent with Get and Upsert in terms of checking cache readiness and parameter validation. It handles the deletion operation as expected.

  • 109-115: The Ready method is a simple check to ensure that the dmapCache is not nil. This is a straightforward and effective way to check the readiness of the cache.

  • 117-120: The formatCacheKey function concatenates the control point and key to form a cache key. While this is a simple approach, it's important to ensure that the control point and key combination remains unique across different contexts where the cache is used. If there's any risk of key collision, consider a more robust scheme for generating unique cache keys.

pkg/policies/flowcontrol/service/check/check.go (5)
  • 23-41: The addition of a cache field to the Handler struct and its initialization in the NewHandler function is a good use of dependency injection, allowing for better modularity and testability of the Handler. Ensure that all instantiations of Handler throughout the codebase are updated to provide the new cache argument.

  • 77-83: The CheckRequest method has been updated to include CacheKey in the RequestContext. This change should be verified to ensure that the cache key is being used appropriately within the ProcessRequest method of the engine and that any cache-related logic is correctly implemented.

  • 88-89: The addition of the ApertureControlPointTypeLabel to TelemetryFlowLabels is a good practice for providing more context in telemetry data. This should be consistent with how labels are used throughout the application.

  • 92-105: The CacheUpsert method correctly handles the cache update operation and returns a response indicating success or error. It's important to ensure that the TTL value provided in the request is validated before being used to prevent potential issues with cache expiration.

  • 107-120: The CacheDelete method implementation is straightforward and handles errors appropriately, returning a response code indicating the result of the operation. Similar to CacheUpsert, ensure that the cache key is validated and that the deletion operation is atomic if necessary to prevent race conditions.

pkg/policies/flowcontrol/cache.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f6e1d3f and 5a2b7ea.
Files selected for processing (5)
  • sdks/aperture-go/example/main.go (5 hunks)
  • sdks/aperture-go/sdk/client.go (8 hunks)
  • sdks/aperture-go/sdk/httpflow.go (3 hunks)
  • sdks/aperture-go/sdk/middleware/grpc.go (3 hunks)
  • sdks/aperture-go/sdk/middleware/http.go (3 hunks)
Additional comments: 18
sdks/aperture-go/sdk/httpflow.go (3)
  • 4-16: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [4-33]

The import and usage of the checkhttpv1 package indicate that the CheckResponse method's return type has been updated to match the new proto definition. This is a necessary change to align with the updated caching functionality mentioned in the pull request summary.

  • 21-24: The CheckResponse method signature has been updated to return a pointer to checkhttpv1.CheckHTTPResponse. This change should be verified across the codebase to ensure that all usages of the CheckResponse method are updated to handle the new return type correctly.

  • 56-61: The implementation of the CheckResponse method correctly returns the checkResponse field of the httpflow struct. This is consistent with the method's intended functionality.

sdks/aperture-go/example/main.go (5)
  • 9-25: The import renaming and package updates are consistent with the pull request summary. The changes reflect the renaming of the aperturego package to aperture and the update of import paths accordingly.

  • 32-35: The app struct has been correctly updated to use the new aperture.Client type instead of the previous aperturego.Client. This change is necessary due to the renaming of the package and associated types.

  • 62-81: The main function has been updated to use the new aperture.Options and aperture.NewClient which aligns with the renaming of the package. The environment variable fetching and parsing logic seems to be correct, but it's important to ensure that the environment variables are set correctly in the deployment environment.

  • 95-105: The middleware initialization has been updated to use the new aperture.MiddlewareParams and middleware.NewHTTPMiddleware which reflects the changes in the package and type names. The timeout value is set to 2000 milliseconds, which should be verified to ensure it aligns with the desired timeout policy.

  • 151-157: The getEnvOrDefault function is used to fetch environment variables with a fallback to a default value if the variable is not set. This is a common pattern and the implementation here is straightforward. However, it's important to ensure that all calls to this function have been updated if the function signature has changed elsewhere in the codebase.

sdks/aperture-go/sdk/middleware/http.go (1)
  • 3-16: The added imports are necessary for the new functionality introduced in the middleware. Ensure that there are no unused imports after the final implementation to keep the code clean.
sdks/aperture-go/sdk/middleware/grpc.go (1)
  • 110-171: The function prepareCheckHTTPRequestForGRPC is well-structured and handles errors appropriately by returning them to the caller. However, if the socketAddressFromNetAddr function is updated to return an error, the call to this function within prepareCheckHTTPRequestForGRPC will need to be updated to handle the error.
sdks/aperture-go/sdk/client.go (8)
  • 3-13: The import paths for OpenTelemetry and gRPC have been updated, which is a common change when dependencies are updated or when the project structure changes. Ensure that the new paths are correct and that the project's dependency management tool (like Go modules) has been updated accordingly.

  • 16-31: The Options struct has been updated to use the slog.Logger type instead of the previous logger type. This change will require all instantiations of Options to use the new slog.Logger type. Ensure that all parts of the code that create an Options struct have been updated accordingly.

  • 52-68: The Client interface and apertureClient struct have been updated to reflect the new logging and gRPC client types. Ensure that all implementations of the Client interface and usages of the apertureClient struct are updated to handle these changes.

  • 75-94: The NewClient function has been updated to include an interceptor for gRPC connections that adds an API key to the metadata if provided. This is a security-sensitive change, so ensure that the API key is handled securely throughout the codebase and that it is not logged or exposed inappropriately.
    [SECURITY]

  • 107-121: The NewClient function now initializes a logger with a default configuration if one is not provided in the Options. This is a good fallback mechanism to ensure that the client always has a logger available. However, ensure that the default logger's configuration is appropriate for the application's logging needs.

  • 140-155: The StartFlow function has been updated to include label extraction and overriding logic. Ensure that the label extraction from the context and the explicit label overriding are working as intended and that they do not introduce any unexpected behavior.

  • 176-182: The StartHTTPFlow function has been updated, and it now includes a span creation for tracing. Ensure that the span is correctly associated with the flow and that it is ended appropriately to avoid any memory leaks or incomplete traces.

  • 227-230: The GetLogger method has been added to the apertureClient to expose the logger. This is a straightforward getter method and seems to be implemented correctly.

sdks/aperture-go/sdk/middleware/http.go Outdated Show resolved Hide resolved
sdks/aperture-go/sdk/middleware/http.go Outdated Show resolved Hide resolved
sdks/aperture-go/sdk/middleware/grpc.go Outdated Show resolved Hide resolved
### Description of change
- Add @fluxninja/aperture-js dependency in example
- fix formatting in connected.ts and use_aperture.ts in example

##### Checklist

- [ ] Tested in playground or other setup
- [ ] Screenshot (Grafana) from playground added to PR for 15+ minute
run
- [ ] Documentation is changed or added
- [ ] Tests and/or benchmarks are included
- [ ] Breaking changes
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5a2b7ea and 9e8f188.
Files ignored due to filter (2)
  • sdks/aperture-js/example/package-lock.json
  • sdks/aperture-js/example/package.json
Files selected for processing (8)
  • sdks/aperture-go/sdk/middleware/grpc.go (3 hunks)
  • sdks/aperture-js/.gitignore (1 hunks)
  • sdks/aperture-js/Dockerfile (1 hunks)
  • sdks/aperture-js/example/routes/connected.ts (1 hunks)
  • sdks/aperture-js/example/routes/use_aperture.ts (2 hunks)
  • sdks/aperture-js/sdk/cache.ts (1 hunks)
  • sdks/aperture-js/sdk/client.ts (3 hunks)
  • sdks/aperture-js/sdk/flow.ts (8 hunks)
Files skipped from review due to trivial changes (2)
  • sdks/aperture-js/.gitignore
  • sdks/aperture-js/Dockerfile
Additional comments: 9
sdks/aperture-js/sdk/client.ts (3)
  • 20-25: The addition of tryConnect and cacheKey to the FlowParams interface aligns with the pull request's goal of introducing caching capabilities. These parameters should be documented to explain their usage and impact on the StartFlow function.

  • 104-108: The resolveFlow function is updated to pass additional parameters to the Flow constructor, including params.cacheKey. This change is consistent with the new caching mechanism. Ensure that the Flow constructor and associated methods are updated to handle these new parameters correctly.

  • 123-132: The CheckRequest object now includes cacheKey as part of its structure, which is essential for the caching mechanism. The callback cb is correctly updated to handle the response and error, passing them to the resolveFlow function. Ensure that the CheckRequest message definition in the gRPC service is updated to include the cacheKey field.

sdks/aperture-js/example/routes/connected.ts (2)
  • 7-10: The change introduces a space between the res: and express.Response which is a stylistic change and does not affect the functionality of the code. However, ensure that this change is consistent with the coding style guidelines of the project.

  • 9-10: The code is using a synchronous method GetState() to check the client state. If GetState() is a blocking call or if it performs any I/O, it might be more appropriate to handle it asynchronously, especially in a Node.js environment which is single-threaded and relies on non-blocking I/O to handle high concurrency. If GetState() is indeed synchronous and does not perform I/O, then this is fine as is.

sdks/aperture-js/example/routes/use_aperture.ts (2)
  • 1-8: The configuration for the ApertureClient is being set up with environment variables. Ensure that these environment variables are documented and that there are default values or error handling in place if they are not set.

  • 22-71: The route handler is well-structured with a try...catch...finally block, which is good for error handling and resource management. However, there are a few points to consider:

  • The flow object is being ended in the finally block, which is good practice to ensure it is always cleaned up, but make sure that the End method does not throw an exception that could be unhandled.
  • The StartFlow method now includes rampMode and cacheKey parameters. Ensure that these changes are reflected in the documentation and that all callers of this method are updated accordingly.
  • The cache operations (SetCachedValue and DeleteCachedValue) are awaited, and errors are logged, but there is no further error handling. Depending on the application's requirements, you might want to handle these errors more gracefully or take specific actions.
  • The labels object is hardcoded with a user value of "kenobi". If this is meant to be dynamic, it should be retrieved from the request or another source.
  • The sleep function is used after flow.ShouldRun() returns true. This simulates work being done and is fine for an example, but in a real application, this should be replaced with actual logic.
sdks/aperture-go/sdk/middleware/grpc.go (2)
  • 151-153: The request body is being marshaled to JSON without checking if it can be marshaled. If req contains types that cannot be marshaled into JSON, this will result in an error. It's good practice to ensure that the types being passed to json.Marshal are compatible or to handle potential errors in a way that doesn't disrupt the flow.

  • 107-176: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [174-192]

The convertHTTPStatusToGRPC function maps HTTP status codes to gRPC codes. This is a good approach to maintain consistency between different protocols. However, ensure that this mapping aligns with the expected behavior of the gRPC services and clients that will interpret these codes.

sdks/aperture-js/sdk/cache.ts Outdated Show resolved Hide resolved
sdks/aperture-js/example/routes/connected.ts Outdated Show resolved Hide resolved
sdks/aperture-go/sdk/middleware/grpc.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9e8f188 and 939efe3.
Files selected for processing (2)
  • sdks/aperture-go/sdk/middleware/grpc.go (3 hunks)
  • sdks/aperture-go/sdk/middleware/http.go (2 hunks)
Additional comments: 1
sdks/aperture-go/sdk/middleware/http.go (1)
  • 3-17: The import section is well-organized and follows the convention of grouping standard library imports together, followed by third-party packages. However, it's important to ensure that all imported packages are used within the file to avoid unnecessary imports which can lead to confusion and slightly increased compilation times.

sdks/aperture-go/sdk/middleware/grpc.go Show resolved Hide resolved
sdks/aperture-go/sdk/middleware/http.go Show resolved Hide resolved
sdks/aperture-go/sdk/middleware/grpc.go Show resolved Hide resolved
sdks/aperture-go/sdk/middleware/grpc.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 939efe3 and 88a6ab4.
Files selected for processing (2)
  • sdks/aperture-go/sdk/client.go (8 hunks)
  • sdks/aperture-go/sdk/flow.go (2 hunks)
Additional comments: 15
sdks/aperture-go/sdk/flow.go (5)
  • 16-28: The Flow interface has been updated to include new methods for caching. This change will require all implementations of the Flow interface to provide these new methods. Ensure that all implementations are updated accordingly.

  • 41-51: The newFlow function has been updated to accept additional parameters (flowControlClient, span, rampMode, cacheKey) and initialize a new flow struct with these parameters. This change is likely part of the larger refactoring to support the new caching mechanism. Ensure that all calls to newFlow are updated to pass the correct arguments.

  • 57-61: The ShouldRun method has been updated to consider the rampMode and the checkResponse's DecisionType. This logic ensures that the flow should run either if the ramp mode is not enabled and there is no check response, or if the decision type is ACCEPTED. This change seems to be a logical enhancement to the flow control mechanism.

  • 103-125: The SetCachedValue method has been added to set a cached value with a specified TTL (time-to-live). It communicates with the flowControlClient to upsert the cache value. The response includes an error, response code, and message. Ensure that error handling is properly implemented in the calling code, as the Error field in the response struct is populated with the error returned from the CacheUpsert RPC call.

  • 134-152: The DeleteCachedValue method has been added to delete a cached value. It also communicates with the flowControlClient to delete the cache value. Similar to SetCachedValue, ensure that error handling is properly implemented in the calling code.

sdks/aperture-go/sdk/client.go (10)
  • 3-13: The import paths have been updated, and the slog package is now used for logging. Ensure that the slog package is properly integrated and that all logging calls have been updated accordingly.

  • 16-23: The import paths for the protobuf-generated code and utility packages have been updated to reflect the new version or location. Verify that these new paths are correct and that the packages are available at these locations.

  • 25-31: The Options struct now includes a *slog.Logger instead of a logr.Logger. This change should be communicated to all users of the aperture package to update their code accordingly.

  • 47-51: New fields have been added to the FlowParams struct, including RampMode and CacheKey. Ensure that these new fields are being used correctly throughout the codebase and that their introduction does not break existing functionality.

  • 54-61: The Client interface has been updated to return *slog.Logger instead of logr.Logger and to include new methods for starting flows with caching. Ensure that all implementations of this interface have been updated accordingly.

  • 77-96: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [72-96]

The NewClient function has been updated to include new logic for setting up a gRPC connection with the Aperture service, including handling an API key for authentication. Ensure that the error handling is robust and that the connection is properly secured.

  • 109-117: The logger setup now checks if a logger is provided in the options; if not, it defaults to a new slog logger with a specified name. Ensure that this default behavior is documented and that it aligns with the expected logging practices.

  • 142-165: The StartFlow method has been updated to include new logic for handling cache keys and ramp mode. Ensure that the logic for merging labels from the context and the flowParams is correct and that the cache key handling is secure and efficient.

  • 179-185: The StartHTTPFlow method has been updated similarly to StartFlow. Ensure that the new logic for HTTP flows is consistent with the changes made to the standard flow logic and that it is properly tested.

  • 228-233: A new method GetLogger has been added to the apertureClient struct to retrieve the logger. Ensure that this method is necessary and that it is being used appropriately where logging is required.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 88a6ab4 and 73b47c8.
Files ignored due to filter (1)
  • docs/content/assets/openapiv2/aperture.swagger.yaml
Files selected for processing (2)
  • pkg/policies/flowcontrol/service/check/check.go (3 hunks)
  • pkg/policies/flowcontrol/service/check/provide.go (1 hunks)
Additional comments: 4
pkg/policies/flowcontrol/service/check/provide.go (2)
  • 36-39: The addition of the Cache field to the ConstructorIn struct is consistent with the pull request's goal of introducing caching capabilities. However, it's marked as optional with optional:"true". Ensure that the rest of the codebase can handle cases where Cache is not provided, to avoid nil pointer dereferences.

  • 46-46: The NewHandler function is correctly updated to accept the new Cache parameter. This change aligns with the introduction of caching capabilities. Ensure that the NewHandler function's implementation is updated accordingly to handle the Cache parameter.

pkg/policies/flowcontrol/service/check/check.go (2)
  • 23-41: The addition of the cache field to the Handler struct and its initialization in the NewHandler function is consistent with the pull request's goal of introducing a caching mechanism. Ensure that all instances where Handler is instantiated are updated to provide the new cache argument.

  • 77-83: The Check method now includes CacheKey in the RequestContext. This is a necessary change for the caching mechanism to work. Ensure that the CacheKey is being generated and used correctly throughout the system.

pkg/policies/flowcontrol/service/check/check.go Outdated Show resolved Hide resolved
pkg/policies/flowcontrol/service/check/check.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 73b47c8 and 8750e51.
Files selected for processing (1)
  • api/Makefile (1 hunks)
Files skipped from review due to trivial changes (1)
  • api/Makefile

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 10

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8750e51 and 77ef899.
Files ignored due to filter (24)
  • api/gen/proto/go/aperture/flowcontrol/check/v1/check.pb.go
  • api/gen/proto/go/aperture/flowcontrol/check/v1/check.pb.validate.go
  • api/gen/proto/go/aperture/flowcontrol/check/v1/check_vtproto.pb.go
  • docs/content/assets/openapiv2/aperture.swagger.yaml
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check.pb.go
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check.pb.validate.go
  • sdks/aperture-go/gen/proto/flowcontrol/check/v1/check_vtproto.pb.go
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheDeleteResponse.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheDeleteResponseOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheLookupStatus.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheOperationStatus.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheUpsertResponse.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CacheUpsertResponseOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CachedValue.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CachedValueOrBuilder.java
  • sdks/aperture-java/lib/core/src/main/java/com/fluxninja/generated/aperture/flowcontrol/check/v1/CheckProto.java
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheDeleteResponse.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheLookupStatus.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheOperationStatus.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CacheUpsertResponse.ts
  • sdks/aperture-js/sdk/gen/aperture/flowcontrol/check/v1/CachedValue.ts
  • sdks/aperture-js/sdk/gen/check.ts
  • sdks/aperture-py/aperture_sdk/_gen/aperture/flowcontrol/check/v1/check_pb2.py
  • sdks/aperture-py/aperture_sdk/_gen/aperture/flowcontrol/check/v1/check_pb2.pyi
Files selected for processing (10)
  • api/aperture/flowcontrol/check/v1/check.proto (2 hunks)
  • pkg/policies/flowcontrol/engine.go (4 hunks)
  • pkg/policies/flowcontrol/service/check/check.go (3 hunks)
  • sdks/aperture-csharp/Generated/Aperture/Flowcontrol/V1/Check.cs (26 hunks)
  • sdks/aperture-csharp/Generated/Aperture/Flowcontrol/V1/CheckGrpc.cs (6 hunks)
  • sdks/aperture-go/sdk/cache.go (1 hunks)
  • sdks/aperture-go/sdk/flow.go (2 hunks)
  • sdks/aperture-js/proto/flowcontrol/check/v1/check.proto (2 hunks)
  • sdks/aperture-js/sdk/cache.ts (1 hunks)
  • sdks/aperture-js/sdk/flow.ts (7 hunks)
Files not summarized due to errors (1)
  • sdks/aperture-csharp/Generated/Aperture/Flowcontrol/V1/Check.cs: Error: Message exceeds token limit
Additional comments: 45
sdks/aperture-csharp/Generated/Aperture/Flowcontrol/V1/CheckGrpc.cs (6)
  • 52-62: The marshaller definitions for the new cache operations (CacheUpsertRequest, CacheUpsertResponse, CacheDeleteRequest, CacheDeleteResponse) have been correctly added. This is necessary for the gRPC framework to serialize and deserialize the messages for these operations.

  • 69-90: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [65-86]

The method definitions for the new RPCs (CacheUpsert, CacheDelete) have been added with the correct marshaller references. This aligns with the changes described in the pull request summary and is essential for enabling the new cache operations in the FlowControlService.

  • 107-120: The base service implementation provides the expected behavior for unimplemented methods by throwing an RpcException with StatusCode.Unimplemented. This is a standard practice in gRPC service definitions to provide a clear error when a method is not implemented.

  • 196-238: The client methods for the new cache operations (CacheUpsert, CacheUpsertAsync, CacheDelete, CacheDeleteAsync) have been implemented correctly, providing both synchronous and asynchronous options. This allows clients to interact with the new cache operations using the FlowControlServiceClient.

  • 250-255: The BindService method has been updated to include the new cache operation methods. This is necessary for the server to recognize and handle incoming RPC calls for these operations.

  • 263-267: The BindService method has been correctly updated to bind the new cache operation methods. This ensures that the service implementation can be registered with the gRPC server with the new functionality.

sdks/aperture-go/sdk/cache.go (1)
  • 1-163: The code introduces new types and functions for handling cache operations in the Go SDK. It includes error handling, status conversion, and response interfaces for get, set, and delete cache operations. The code is well-structured and follows Go conventions for error variables and custom types. However, there are a few points to consider:
  • The default cases in convertCacheLookupStatus and convertCacheOperationStatus functions (lines 39 and 51) return LookupStatusMiss and OperationStatusError, respectively. This is a safe default, but it might be worth logging or handling the unexpected status to aid in debugging if an unknown status is ever encountered.
  • The convertCacheError function (lines 56-63) converts a string error message to an error type, which is a simple and effective way to handle error messages from the cache operations. However, it's important to ensure that the error messages are meaningful and provide enough context for troubleshooting.
  • The response structs (getCachedValueResponse, setCachedValueResponse, deleteCachedValueResponse) and their corresponding interfaces (GetCachedValueResponse, SetCachedValueResponse, DeleteCachedValueResponse) are well-defined and encapsulate the response data effectively. It's good practice to define interfaces for responses as it allows for easier testing and mocking.
  • The newGetCachedValueResponse, newSetCachedValueResponse, and newDeleteCachedValueResponse functions (lines 142-162) are factory functions that create instances of the response structs. This pattern is useful for encapsulating the creation logic and providing a clean API for creating response objects.

Overall, the code is well-written and follows good practices. Just ensure that the rest of the system is prepared to handle the new error types and statuses introduced here.

sdks/aperture-js/proto/flowcontrol/check/v1/check.proto (8)
  • 9-14: The addition of CacheUpsert and CacheDelete RPCs to the FlowControlService is a significant change. Ensure that the corresponding server-side implementations are updated to handle these new RPCs and that the client-side SDKs are also updated to expose these functionalities to the end-users.

  • 22-22: The addition of cache_key to the CheckRequest message implies that caching logic is now part of the flow control check process. It's important to ensure that the caching mechanism is robust and that cache keys are generated in a consistent and collision-resistant manner. Additionally, consider the implications of cache key enumeration and potential security concerns around cache poisoning.

  • 67-71: The CachedValue message is included in the CheckResponse, which suggests that the response can contain cached results. This is a performance optimization but also introduces complexity in terms of cache consistency and invalidation. Ensure that the caching strategy is well-documented and that clients are aware of how to interpret and handle cached responses.

  • 74-77: The CacheLookupStatus enum is introduced to indicate the result of a cache lookup. Ensure that the client and server logic correctly interpret these statuses and handle them appropriately, especially in edge cases where a cache miss might occur.

  • 79-82: The CacheOperationStatus enum is used to represent the outcome of cache operations. It's crucial to ensure that error handling is robust and that any operation errors are communicated clearly to the client to prevent any misinterpretation of the cache state.

  • 84-88: The CachedValue message includes an operation_status and an error string. It's important to ensure that the error messages are descriptive and actionable. Additionally, consider if there are any security implications of exposing raw error messages to clients, as they might leak implementation details or sensitive information.

  • 91-95: The CacheUpsertRequest message allows clients to insert or update cache entries. Ensure that there are appropriate access controls and validation in place to prevent unauthorized cache manipulation, which could lead to security vulnerabilities such as cache poisoning.

  • 103-105: The CacheDeleteRequest message enables clients to delete cache entries. Similar to the CacheUpsertRequest, ensure that proper access controls are in place to prevent unauthorized deletion of cache entries.

pkg/policies/flowcontrol/service/check/check.go (3)
  • 23-42: The Handler struct and NewHandler function have been correctly updated to include the new cache field and parameter. This change aligns with the pull request's goal of introducing caching capabilities to the FlowControlService. Ensure that all instantiations of Handler throughout the codebase are updated to provide the new cache argument.

  • 77-83: The Check method has been updated to include CacheKey in the RequestContext. This is necessary for the caching functionality. However, ensure that the CacheKey is being properly generated and used elsewhere in the system to avoid potential cache collisions or misuse.

  • 88-89: The addition of the ApertureControlPointTypeLabel to TelemetryFlowLabels is a good practice for observability and telemetry. This change should help in monitoring and debugging the flow control decisions.

pkg/policies/flowcontrol/engine.go (4)
  • 73-76: The cache field has been added to the Engine struct to support caching. Ensure that the RegisterCache method is called to initialize this field before the Engine is used to process requests, as the ProcessRequest method assumes the cache is non-nil when a cacheKey is provided. This is crucial to avoid potential nil pointer dereferences.

  • 87-90: The introduction of cacheKey as a local variable in the ProcessRequest function is a significant change. It is important to ensure that the RequestContext struct now includes a CacheKey field and that it is being correctly populated upstream. If the cache key is not correctly generated or passed, the caching functionality will not work as intended.

  • 123-161: The refactoring of the logic for running limiters into a separate function runLimiters is a good practice for code maintainability and readability. However, ensure that the runLimiters function is thoroughly tested, especially since it involves concurrency and could potentially introduce race conditions or deadlocks if not handled correctly.

  • 173-194: The cache lookup logic has been added to the ProcessRequest function. It is important to handle errors from the cache appropriately. The current implementation logs a cache miss and any errors encountered during the cache lookup. Ensure that the error handling aligns with the overall application's error handling strategy and that any cache-related errors do not impact the critical path of request processing.

sdks/aperture-js/sdk/cache.ts (4)
  • 4-7: The LookupStatus enum is well-defined and aligns with the expected cache lookup statuses.

  • 22-25: The OperationStatus enum is well-defined and aligns with the expected operation statuses for cache operations.

  • 40-47: The ConvertCacheError function is a simple and effective way to convert a string error message into an Error object, handling null and undefined appropriately.

  • 49-82: The CachedValueResponse class is well-structured and provides clear methods for accessing the properties of a cached value response. The use of Buffer for the value suggests binary data handling, which is appropriate for cache values.

sdks/aperture-js/sdk/flow.ts (4)
  • 1-16: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [1-30]

The imports are well-organized and grouped by their functionality, which is good for maintainability. However, it's important to ensure that all these imports are used within the file to avoid unnecessary overhead.

  • 41-53: The constructor of the Flow class has been updated to accept additional parameters for cache operations. This change aligns with the pull request's goal to introduce caching capabilities. Ensure that all instantiations of the Flow class are updated to pass the correct arguments.

  • 142-162: The CachedValue method creates a new CachedValueResponse object based on the presence of an error or the cached value from the checkResponse. This method does not seem to be asynchronous, but it should be verified that the consumers of this method are not expecting a promise.

  • 209-223: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [185-221]

The End method has been updated to handle cache responses. It includes a workaround for a known issue with the protobufjs library regarding timestamp formatting. This is a good example of defensive programming, but it's important to track the resolution of the linked issue and remove the workaround once it's no longer necessary.

api/aperture/flowcontrol/check/v1/check.proto (8)
  • 9-14: The addition of CacheUpsert and CacheDelete RPCs to the FlowControlService is a significant change. Ensure that the corresponding server-side implementations are secure, efficient, and handle errors gracefully. Also, verify that the client-side SDKs are updated to support these new RPCs.

  • 17-23: The CheckRequest message now includes a cache_key field. Ensure that the logic for generating and validating cache keys is robust and secure against potential cache poisoning attacks. Additionally, verify that all clients of this service are updated to handle the new cache_key field appropriately.

  • 67-71: The CheckResponse message now includes a cached_value field. It is important to ensure that the caching logic correctly populates this field and that the clients properly handle the cached responses. Also, consider the implications of exposing cache details to the clients and whether this aligns with the intended design and security posture.

  • 74-77: The CacheLookupStatus enum is introduced to indicate cache hit or miss. Ensure that the service correctly sets this status in responses and that clients handle each status as expected.

  • 79-82: The CacheOperationStatus enum is introduced to indicate the success or error of a cache operation. It is crucial to ensure that error handling is consistent and informative, providing enough detail for clients to react appropriately to errors.

  • 84-89: The CachedValue message includes an error field. It is important to ensure that error messages do not leak sensitive information and are sanitized before being sent to clients. Additionally, verify that the error messages are clear and actionable for the clients.

  • 91-95: In the CacheUpsertRequest message, ensure that the TTL (Time To Live) for cache entries is being validated and that there are safeguards against setting excessively long or short TTL values, which could lead to cache inefficiency or denial of service.

  • 98-100: The CacheUpsertResponse and CacheDeleteResponse messages include an operation_status and an error field. Ensure that the service implementation correctly maps internal errors to these fields and that the error messages are consistent and do not expose internal details unnecessarily.

sdks/aperture-go/sdk/flow.go (7)
  • 3-14: The import section has been updated correctly to include the necessary packages for the new functionality. The context package is essential for passing request-scoped values, cancellation signals, and deadlines, while the google.golang.org/protobuf/types/known/durationpb package is used for handling protocol buffer duration types. The update to the import path for checkproto reflects the new versioning scheme, which is a good practice for maintaining backward compatibility.

  • 16-28: The Flow interface has been updated to include new methods for caching (CachedValue, SetCachedValue, and DeleteCachedValue). This change aligns with the pull request's goal of introducing caching capabilities. However, it's important to ensure that all implementations of the Flow interface are updated to include these new methods to maintain compatibility.

  • 41-51: The newFlow function has been updated to accept additional parameters flowControlClient and cacheKey, which are necessary for the new caching functionality. The function initializes a flow struct with these new fields. This is a good practice as it encapsulates the creation logic and sets sensible defaults.

  • 75-88: The CachedValue method correctly handles various error scenarios, such as when f.err is not nil or when f.checkResponse or its CachedValue is nil. It returns a GetCachedValueResponse with appropriate status and error information. This is a good example of defensive programming, ensuring that the method behaves predictably even in error conditions.

  • 91-110: The SetCachedValue method performs a cache upsert operation using the flowControlClient. It correctly converts the ttl to a protobuf duration and handles errors by returning a SetCachedValueResponse with the appropriate status and error. This method should be reviewed to ensure that the CacheUpsert RPC is implemented correctly on the server side and that the flowControlClient is properly initialized and capable of handling the RPC call.

  • 112-127: The DeleteCachedValue method performs a cache delete operation and handles errors similarly to the SetCachedValue method. It is important to verify that the CacheDelete RPC is also correctly implemented on the server side and that the flowControlClient is set up to handle this call.

  • 129-131: The Error method simply returns the error that occurred during the flow, which is a straightforward getter. This is a standard practice for exposing errors to the caller.

pkg/policies/flowcontrol/engine.go Show resolved Hide resolved
sdks/aperture-js/sdk/cache.ts Show resolved Hide resolved
sdks/aperture-js/sdk/cache.ts Show resolved Hide resolved
sdks/aperture-js/sdk/cache.ts Outdated Show resolved Hide resolved
sdks/aperture-js/sdk/flow.ts Show resolved Hide resolved
sdks/aperture-js/sdk/cache.ts Outdated Show resolved Hide resolved
sdks/aperture-go/sdk/flow.go Outdated Show resolved Hide resolved
sdks/aperture-js/sdk/flow.ts Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 77ef899 and e1248ca.
Files selected for processing (1)
  • sdks/aperture-js/example/routes/connected.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • sdks/aperture-js/example/routes/connected.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e1248ca and 1e2abe5.
Files selected for processing (1)
  • sdks/aperture-js/sdk/cache.ts (1 hunks)

sdks/aperture-js/sdk/cache.ts Show resolved Hide resolved
sdks/aperture-js/sdk/cache.ts Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1e2abe5 and 331ec58.
Files selected for processing (1)
  • sdks/aperture-go/sdk/flow.go (2 hunks)
Additional comments: 8
sdks/aperture-go/sdk/flow.go (8)
  • 3-14: The import of the durationpb package is correct and necessary for the new caching functionality that uses time durations. However, ensure that the errors package is still required after the changes, as it is not explicitly used in the provided code snippet.

  • 16-28: The Flow interface has been updated to include new methods for caching. This is a significant change and should be communicated to all users of the SDK to update their implementations accordingly. The new methods CachedValue, SetCachedValue, and DeleteCachedValue need to be documented to explain their usage and expected behavior.

  • 30-51: The flow struct now includes a cacheKey field, which is essential for the new caching functionality. Ensure that all instances of flow are initialized with a valid cacheKey where necessary, or handle cases where cacheKey might be an empty string to avoid runtime errors.

  • 54-58: The ShouldRun method's logic has been updated to account for the rampMode. This change affects the fail-open behavior and should be clearly documented in the SDK's usage guide. Users need to be aware of how rampMode influences the decision-making process.

  • 71-84: The CachedValue method retrieves the cached value from the checkResponse. It is important to ensure that the checkResponse is not nil before accessing its properties to avoid a nil pointer dereference. The error handling here is good practice, as it provides clear error messages for different failure scenarios.

  • 87-106: The SetCachedValue method correctly converts the TTL to a protobuf duration and makes a gRPC call to set the cached value. It is important to handle the case where cacheKey is empty to prevent unnecessary gRPC calls. The error handling and conversion of operation status are well-implemented.

  • 108-123: The DeleteCachedValue method follows a similar pattern to SetCachedValue, including error handling and status conversion. It is crucial to ensure that the cacheKey is not empty before attempting to delete the cached value.

  • 125-127: The Error method returns the error that occurred during the flow. This is a simple getter method and is implemented correctly.

@tanveergill tanveergill enabled auto-merge (squash) November 20, 2023 02:17
@tanveergill tanveergill merged commit 489d5e7 into main Nov 20, 2023
39 checks passed
@tanveergill tanveergill deleted the tgill/caching branch November 20, 2023 02:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants