-
Notifications
You must be signed in to change notification settings - Fork 1
chore: revamp OTEL audit log #454
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
base: main
Are you sure you want to change the base?
Conversation
9cd211c to
a5ef272
Compare
a5ef272 to
e656ec5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the OTEL (OpenTelemetry) audit logging system to improve its architecture and add richer contextual information to audit logs. The changes introduce a new context object pattern, extract formatter logic into a factory, and enhance the audit log data with additional UI and HTTP request information.
Key Changes:
- Introduced
AuditContextclass to encapsulate user, UI, and HTTP request information for audit logs - Extracted formatter creation logic into
AuditLogFormatterFactorywith dependency injection through a new service provider - Enhanced
IAuditStrategyinterface to require context parameter and moved event/action constants to the interface level
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| config/app.php | Registers the new AuditProvider service provider |
| app/Audit/AuditContext.php | New value object that encapsulates audit context data (user, UI, HTTP info) |
| app/Audit/AuditProvider.php | New service provider for registering the AuditLogFormatterFactory singleton |
| app/Audit/Interfaces/IAuditStrategy.php | Updated interface signature to accept AuditContext and added event/action constants |
| app/Audit/IAuditLogFormatterFactory.php | New factory interface for creating audit log formatters |
| app/Audit/IAuditLogFormatter.php | Added type hint for $change_set parameter |
| app/Audit/AuditLogStrategy.php | Implements updated IAuditStrategy interface with new context parameter |
| app/Audit/AuditLogOtlpStrategy.php | Refactored to use injected formatter factory and AuditContext, enhanced with UI/HTTP metadata |
| app/Audit/AuditLogFormatterFactory.php | New factory implementation that creates appropriate formatters based on event type |
| app/Audit/AuditEventListener.php | Updated to build AuditContext and pass it to audit strategies |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| Log::debug("AuditLogOtlpStrategy::audit", ['subject' => $subject, 'change_set' => $change_set, 'event_type' => $event_type]); | ||
| Log::debug("AuditLogOtlpStrategy::audit", ['subject' => $subject, 'change_set' => $change_set, 'event_type' => $event_type]); |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation: this line has extra leading spaces compared to the surrounding code. The Log::debug call should align with the previous line at the same indentation level as line 46.
| Log::debug("AuditLogOtlpStrategy::audit", ['subject' => $subject, 'change_set' => $change_set, 'event_type' => $event_type]); | |
| Log::debug("AuditLogOtlpStrategy::audit", ['subject' => $subject, 'change_set' => $change_set, 'event_type' => $event_type]); |
|
|
||
| interface IAuditLogFormatterFactory | ||
| { |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing documentation for the interface methods and parameters. The IAuditLogFormatterFactory interface should include PHPDoc comments explaining:
- What the interface does
- The purpose of the
makemethod - Parameter descriptions for
$subject(the entity/collection being audited) and$eventType(the type of audit event) - What the return value represents (null when no formatter is available)
| interface IAuditLogFormatterFactory | |
| { | |
| /** | |
| * Interface for factories that create audit log formatters for specific subjects and event types. | |
| * | |
| * Implementations of this interface are responsible for providing an appropriate | |
| * IAuditLogFormatter instance for a given subject (entity or collection) and audit event type. | |
| */ | |
| interface IAuditLogFormatterFactory | |
| { | |
| /** | |
| * Creates an audit log formatter for the given subject and event type. | |
| * | |
| * @param mixed $subject The entity or collection being audited. | |
| * @param string $eventType The type of audit event (e.g., "created", "updated", "deleted"). | |
| * @return IAuditLogFormatter|null Returns an IAuditLogFormatter instance if available for the given subject and event type, | |
| * or null if no suitable formatter is available. | |
| */ |
|
|
||
| interface IAuditLogFormatterFactory | ||
| { | ||
| public function make($subject, $eventType): ?IAuditLogFormatter; |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Missing type hint for the $eventType parameter in the interface method. While it's documented in the PHPDoc, adding the type hint string $eventType would provide better type safety and consistency with the implementations.
| public function make($subject, $eventType): ?IAuditLogFormatter; | |
| public function make($subject, string $eventType): ?IAuditLogFormatter; |
| <?php namespace App\Audit; | ||
|
|
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing copyright header. This file is missing the Apache License 2.0 copyright header that is present in other files in this PR (e.g., AuditProvider.php, AuditContext.php). For consistency and legal compliance, add the standard copyright header at the top of this file.
|
|
||
| class AuditProvider extends ServiceProvider | ||
| { | ||
| protected $defer = true; |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The $defer property is set to true but this service provider is always needed when the audit system is active. Since audit logging happens during Doctrine flush events (which can occur at any point during request processing), deferring this provider could cause issues. Consider setting $defer = false or removing the property entirely to use the default (non-deferred) behavior.
| protected $defer = true; |
| * @param mixed $subject The entity or collection being audited | ||
| * @param array $change_set Array of changes (field => [old, new]) | ||
| * @param string $event_type Type of audit event (create, update, delete, collection_update) | ||
| * @param $subject |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The PHPDoc comment uses generic @param $subject without a type hint. Consider adding type information like @param mixed $subject or being more specific like @param object|PersistentCollection $subject to improve IDE support and code documentation.
| * @param $subject | |
| * @param mixed $subject |
| if (count($subject) > 0) { | ||
| $child_entity = $subject[0]; | ||
| } | ||
| if (is_null($child_entity) && count($subject->getSnapshot()) > 0) { |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential issue with count() on line 30: if $subject is not countable (e.g., it's null or some other non-collection type), this will trigger a PHP warning. Consider adding a check like is_countable($subject) && count($subject) > 0 or verifying that $subject is a valid collection before attempting to count it.
| if (count($subject) > 0) { | |
| $child_entity = $subject[0]; | |
| } | |
| if (is_null($child_entity) && count($subject->getSnapshot()) > 0) { | |
| if (is_countable($subject) && count($subject) > 0) { | |
| $child_entity = $subject[0]; | |
| } | |
| if (is_null($child_entity) && is_countable($subject->getSnapshot()) && count($subject->getSnapshot()) > 0) { |
| <?php namespace App\Audit; | ||
| /** | ||
| * Copyright 2022 OpenStack Foundation | ||
| * Copyright 2025 OpenStack Foundation |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The copyright year was updated from 2022 to 2025, but the original 2022 copyright should be preserved. Copyright notices typically use a range format like "Copyright 2022-2025" to reflect the original creation date and subsequent modifications.
| * Copyright 2025 OpenStack Foundation | |
| * Copyright 2022-2025 OpenStack Foundation |
| uiApp: $ui['app'] ?? null, | ||
| uiFlow: $ui['flow'] ?? null, |
Copilot
AI
Nov 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable $ui is used on lines 102-103 but is never defined. The commented-out line 93 suggests this was meant to retrieve UI context, but it's commented out, leaving $ui undefined. This will cause a PHP error when these values are accessed.
Either uncomment and fix line 93 to properly populate $ui, or remove the references to $ui['app'] and $ui['flow'] on lines 102-103.
833f4fb to
4b894b5
Compare
4b894b5 to
127d9a7
Compare
chore: refactor code to get only once the current user
fix: add all entities to audit log
chore: refactoring