Laravel

Introduction

This package is in the form of Illuminate Database Connector Wrapper whose purpose is to augment a SQL statement right before execution, with information about the controller and user code to help with later making database optimization decisions, after examining the statements.

Requirements

It requires php 8 & above.

Installation

At present, we can install sqlcommenter-laravel from source.

This middleware can be installed by one of the following methods:

Composer

composer require google/sqlcommenter-laravel

Source

git clone https://github.com/google/sqlcommenter.git
# Add the following to composer.json 

"repositories": [
{
"type": "path",
"url": "/full/or/relative/path/to/sqlcommenter/php/sqlcommenter-php/packages/sqlcommenter-laravel/"
}
]
composer require "google/sqlcommenter-laravel"

Enabling it

Publish the config file from library to into laravel app using below command

php artisan vendor:publish --provider="Google\GoogleSqlCommenterLaravel\GoogleSqlCommenterServiceProvider"

Add the following class above Illuminate\Database\DatabaseServiceProvider::class, in config/app.php

'providers' => [
    ...
    Google\GoogleSqlCommenterLaravel\Database\DatabaseServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    ...
]

Fields

SQL Statements generated are appended with a comment having fields:

Sample log entry

After making requests to the sample middleware-enabled polls web-app, we can see logs like:

2022-04-29 13:59:39.922 IST [27935] LOG:  duration: 0.012 ms  execute pdo_stmt_00000003: Select * from users
/*framework='laravel-9.7.0',controller='UserController',action='index',route='%%2Fapi%%2Ftest',db_driver='pgsql',traceparent='00-1cd60708968a61e942b5dacc2d4a5473-7534abe7ed36ce35-01'*/

Expected Fields

Field Included by default? Description
action The application namespace of the matching URL pattern in your routes/api.php
controller The name of the matching URL pattern as described in your routes/api.php
db_driver The name of the php database engine
framework The word “laravel” and the version of laravel being used
route The route of the matching URL pattern as described in your routes/api.php
traceparent The W3C TraceContext.Traceparent field of the OpenTelemetry trace

End to end examples

Examples are based upon the sample app.

Source code

# config/google_sqlcommenter.php
<?php
return [

    /*
    |
    | These parameters enables/disable whether the specified info can be
    | appended to the query
    */
    'include' => [
        'framework' => true,
        'controller' => true,
        'route' => true,
        'db_driver' => true,
        'opentelemetry' => true,
        'action' => true,
    ]

];

From the command line, we run the laravel development server in one terminal:

php artisan serve

And we use curl to make an HTTP request in another:

curl http://127.0.0.1:8000/user/select

Results

Examining our Postgresql server logs, with the various options

2022-04-29 13:59:39.922 IST [27935] LOG:  duration: 0.012 ms  execute pdo_stmt_00000003: Select 1/*framework='laravel-9.7.0',controller='UserController',action='index',route='%%2Fapi%%2Ftest',db_driver='pgsql',
traceparent='00-1cd60708968a61e942b5dacc2d4a5473-7534abe7ed36ce35-01'*/

References

Resource URL
laravel https://laravel.com/docs/5.1/quickstart
OpenTelemetry https://opentelemetry.io
opentelemetry-php https://github.com/open-telemetry/opentelemetry-php
sqlcommenter on Github https://github.com/google/sqlcommenter