Skip to content

Conversation

@smarcet
Copy link
Collaborator

@smarcet smarcet commented Nov 11, 2025

chore: refactor code to get only once the current user
fix: add all entities to audit log
chore: refactoring

@smarcet smarcet force-pushed the feature/audit-log-revamp branch 2 times, most recently from 9cd211c to a5ef272 Compare November 11, 2025 17:57
@smarcet smarcet force-pushed the feature/audit-log-revamp branch from a5ef272 to e656ec5 Compare November 11, 2025 18:42
@smarcet smarcet requested review from Copilot and romanetar November 11, 2025 18:42
Copilot finished reviewing on behalf of smarcet November 11, 2025 18:44
Copy link
Contributor

Copilot AI left a 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 AuditContext class to encapsulate user, UI, and HTTP request information for audit logs
  • Extracted formatter creation logic into AuditLogFormatterFactory with dependency injection through a new service provider
  • Enhanced IAuditStrategy interface 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]);
Copy link

Copilot AI Nov 11, 2025

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.

Suggested change
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 uses AI. Check for mistakes.
Comment on lines +2 to +4

interface IAuditLogFormatterFactory
{
Copy link

Copilot AI Nov 11, 2025

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 make method
  • 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)
Suggested change
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.
*/

Copilot uses AI. Check for mistakes.

interface IAuditLogFormatterFactory
{
public function make($subject, $eventType): ?IAuditLogFormatter;
Copy link

Copilot AI Nov 11, 2025

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.

Suggested change
public function make($subject, $eventType): ?IAuditLogFormatter;
public function make($subject, string $eventType): ?IAuditLogFormatter;

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +2
<?php namespace App\Audit;

Copy link

Copilot AI Nov 11, 2025

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.

Copilot uses AI. Check for mistakes.

class AuditProvider extends ServiceProvider
{
protected $defer = true;
Copy link

Copilot AI Nov 11, 2025

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.

Suggested change
protected $defer = true;

Copilot uses AI. Check for mistakes.
* @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
Copy link

Copilot AI Nov 11, 2025

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.

Suggested change
* @param $subject
* @param mixed $subject

Copilot uses AI. Check for mistakes.
Comment on lines +30 to +33
if (count($subject) > 0) {
$child_entity = $subject[0];
}
if (is_null($child_entity) && count($subject->getSnapshot()) > 0) {
Copy link

Copilot AI Nov 11, 2025

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.

Suggested change
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) {

Copilot uses AI. Check for mistakes.
<?php namespace App\Audit;
/**
* Copyright 2022 OpenStack Foundation
* Copyright 2025 OpenStack Foundation
Copy link

Copilot AI Nov 11, 2025

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.

Suggested change
* Copyright 2025 OpenStack Foundation
* Copyright 2022-2025 OpenStack Foundation

Copilot uses AI. Check for mistakes.
Comment on lines +102 to +103
uiApp: $ui['app'] ?? null,
uiFlow: $ui['flow'] ?? null,
Copy link

Copilot AI Nov 11, 2025

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.

Copilot uses AI. Check for mistakes.
@smarcet smarcet force-pushed the feature/audit-log-revamp branch 3 times, most recently from 833f4fb to 4b894b5 Compare November 11, 2025 19:20
@smarcet smarcet force-pushed the feature/audit-log-revamp branch from 4b894b5 to 127d9a7 Compare November 11, 2025 20:27
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.

2 participants