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

fix(contrib-auto-laravel): fix log level type issue #306

Closed
wants to merge 4 commits into from

Conversation

flc1125
Copy link

@flc1125 flc1125 commented Oct 16, 2024

@flc1125 flc1125 requested a review from a team as a code owner October 16, 2024 04:44
Copy link

welcome bot commented Oct 16, 2024

Thanks for opening your first pull request! If you haven't yet signed our Contributor License Agreement (CLA), then please do so that we can accept your contribution. A link should appear shortly in this PR if you have not already signed one.

Copy link

linux-foundation-easycla bot commented Oct 16, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@@ -37,7 +37,9 @@ public function recordLog(MessageLogged $log): void
$underlyingLogger = $this->logger->getLogger();

/** @phan-suppress-next-line PhanUndeclaredMethod */
if (method_exists($underlyingLogger, 'isHandling') && !$underlyingLogger->isHandling($log->level)) {
if (method_exists($underlyingLogger, 'isHandling') && !$underlyingLogger->isHandling(
$underlyingLogger->toMonologLevel($log->level))
Copy link
Collaborator

Choose a reason for hiding this comment

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

this seems like it would now only work with monolog as the underlying logger, but there could be others, right? is toMonologLevel a part of any common interface between loggers?

Copy link
Author

Choose a reason for hiding this comment

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

@brettmc
I'm not quite sure how many of the third-party libraries in the industry that comply with \Psr\Log\LoggerInterface support the method method_exists($underlyingLogger, 'isHandling').

  • Suppose there are many. Then I wonder if adding method_exists($underlyingLogger, 'toMonologLevel') is too specific.
  • Suppose there are few, and it is more for supporting Monolog. Then I think it is acceptable to add method_exists($underlyingLogger, 'toMonologLevel').

Of course, there is another way, which is to add an adapter to solve this problem, but the extension might be a bit cumbersome.

So, I'd like to know your thoughts.

Copy link
Author

Choose a reason for hiding this comment

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

Also, there is another method, that is, I define an interface of ShouldHandle and bring it in when doing new LogWatcher($this->instrumentation). It's probably like this:

<?php

declare(strict_types=1);

use Illuminate\Log\Events\MessageLogged;
use Illuminate\Log\LogManager;

interface ShouldHandle
{
    public function shouldHandle(LogManager $logger, MessageLogged $log): bool;
}

Copy link
Author

Choose a reason for hiding this comment

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

@brettmc
Another processing method. Because method_exists($underlyingLogger, 'isHandling') it is too special. This method is not supported in \Psr\Log\LoggerInterface.

As an official extension package, for this special case, I suggest that users can consider overriding the recordLog method of LogWatcher.

And for this package, we only need to add a shoudRecordLog method.


For this, what we may have to do:

Of course, there are some other additions. That is, it can be retrieved more conveniently through some configurations. Currently, our register entry has some limitations. But this may take some time to continuously improve.

@bobstrecansky
Copy link
Collaborator

@flc1125 - will you add some relevant tests to this PR as well please?

Copy link

codecov bot commented Oct 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.33%. Comparing base (67ce817) to head (aed62ae).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##               main     #306   +/-   ##
=========================================
  Coverage     80.33%   80.33%           
  Complexity     1026     1026           
=========================================
  Files            98       98           
  Lines          4114     4114           
=========================================
  Hits           3305     3305           
  Misses          809      809           
Flag Coverage Δ
Aws 85.51% <ø> (ø)
Context/Swoole 0.00% <ø> (ø)
Instrumentation/CakePHP 20.00% <ø> (ø)
Instrumentation/CodeIgniter 73.94% <ø> (ø)
Instrumentation/ExtAmqp 89.58% <ø> (ø)
Instrumentation/Guzzle 69.73% <ø> (ø)
Instrumentation/HttpAsyncClient 81.33% <ø> (ø)
Instrumentation/IO 70.90% <ø> (ø)
Instrumentation/MongoDB 77.33% <ø> (ø)
Instrumentation/OpenAIPHP 86.82% <ø> (ø)
Instrumentation/PDO 89.56% <ø> (ø)
Instrumentation/Psr14 78.12% <ø> (ø)
Instrumentation/Psr15 93.50% <ø> (ø)
Instrumentation/Psr16 97.50% <ø> (ø)
Instrumentation/Psr18 82.08% <ø> (ø)
Instrumentation/Psr3 60.25% <ø> (ø)
Instrumentation/Psr6 97.61% <ø> (ø)
Instrumentation/Slim 86.95% <ø> (ø)
Instrumentation/Symfony 89.07% <ø> (ø)
Instrumentation/Yii 77.77% <ø> (ø)
Logs/Monolog 100.00% <ø> (ø)
Propagation/ServerTiming 100.00% <ø> (ø)
Propagation/TraceResponse 100.00% <ø> (ø)
ResourceDetectors/Container 93.02% <ø> (ø)
Sampler/RuleBased 32.14% <ø> (ø)
Shims/OpenTracing 92.99% <ø> (ø)
Symfony 88.20% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 67ce817...aed62ae. Read the comment docs.

@flc1125
Copy link
Author

flc1125 commented Oct 16, 2024

@flc1125 - will you add some relevant tests to this PR as well please?

No problem. Give me some time.

@ChrisLightfootWild
Copy link
Contributor

fix: #264 (comment)

@flc1125 this link appears to be broken for me. Though if I hover over it, I see the following:
image

I can't actually see that review though.


Have you seen an example of this failure? (Do you have a stacktrace to hand?)

@flc1125
Copy link
Author

flc1125 commented Oct 17, 2024

fix: #264 (comment)

@flc1125 this link appears to be broken for me. Though if I hover over it, I see the following: image

I can't actually see that review though.

Have you seen an example of this failure? (Do you have a stacktrace to hand?)

@ChrisLightfootWild Ch

image

It should be collapsed.


This is an example of an error I encountered:

  • PHP: 8.1.1
  • Laravel Framework 9.52.16
Uncaught TypeError: Monolog\Logger::isHandling(): Argument #1 ($level) must be of type int, string given, called in /webroot/project/vendor/open-telemetry/opentelemetry-auto-laravel/src/Watchers/LogWatcher.php on line 40 and defined in /webroot/project/vendor/monolog/monolog/src/Monolog/Logger.php:530
Stack trace:
#0 /webroot/project/vendor/open-telemetry/opentelemetry-auto-laravel/src/Watchers/LogWatcher.php(40): Monolog\Logger->isHandling()
#1 /webroot/project/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(421): OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\LogWatcher->recordLog()
#2 /webroot/project/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(249): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}()
#3 /webroot/project/vendor/laravel/framework/src/Illuminate/Log/Logger.php(245): Illuminate\Events\Dispatcher->dispatch()
#4 /webroot/project/vendor/laravel/framework/src/Illuminate/Log/Logger.php(186): Illuminate\Log\Logger->fireLogEvent()
#5 /webroot/project/vendor/laravel/framework/src/Illuminate/Log/Logger.php(94): Illuminate\Log\Logger->writeLog()
#6 /webroot/project/vendor/laravel/framework/src/Illuminate/Log/LogManager.php(645

Besides, I checked the latest version of Laravel. Its type hasn't changed and the same problem is estimated to occur.

@flc1125
Copy link
Author

flc1125 commented Oct 17, 2024

@bobstrecansky @brettmc I think I already know where the problem is.

The occurrence of this problem mainly lies in the version of Monolog.

  • Under version 2.x, the data type problem I mentioned will occur.
  • Under version 3.x, it works normally. Among them: Laravel started to upgrade to this version since v10.x (related PR).

https://github.com/Seldaek/monolog/pull/1656/files#diff-1e7fd545cec457de96f5ed6bd7249ba091cd9e699b4057db15ff1e2e0364025bR429-R434


I want to know if our maintenance policy has any requirements for the version of Laravel? This will facilitate me in dealing with this problem.

Of course, for my problem, upgrading my framework is a good choice.

@brettmc
Copy link
Collaborator

brettmc commented Oct 17, 2024

I want to know if our maintenance policy has any requirements for the version of Laravel?

Our policy is to try to support the versions that are actively supported

@flc1125
Copy link
Author

flc1125 commented Oct 17, 2024

I want to know if our maintenance policy has any requirements for the version of Laravel?

Our policy is to try to support the versions that are actively supported

@brettmc OK, just checked official maintenance info this pr should be closed. thanks everyone for the help.
https://laravel.com/docs/11.x/releases#support-policy

image

@flc1125 flc1125 closed this Oct 17, 2024
@ChrisLightfootWild
Copy link
Contributor

@flc1125 thanks for the additional info. I'm still going to try and find some time to look into this a bit further and see if we need to tackle this now.

I have the basis of v1.1 for Laravel instrumentation in #269 and that targets the supported versions of Laravel which may encounter this.

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.

4 participants