feat: add support for OTEL logs

feat: add support for OTEL logs
This commit is contained in:
Ondrej Vlach 2024-01-18 18:37:41 +01:00
parent 1a3cf1c2e0
commit 192cfb9b73
Signed by: ovlach
GPG Key ID: 4FF1A23B4914DE70
7 changed files with 131 additions and 12 deletions

View File

@ -3,3 +3,4 @@ TODO:
- [ ] Chybí speciální slovník nebo vypnutí slovníku pro testy - [ ] Chybí speciální slovník nebo vypnutí slovníku pro testy
- [ ] V reálný aplikaci bych použil Mockery, nicméně tady mě to přijde zbytečný - [ ] V reálný aplikaci bych použil Mockery, nicméně tady mě to přijde zbytečný
- [ ] Nastavení cache ideálně v memcached/redis etc. - [ ] Nastavení cache ideálně v memcached/redis etc.
- [ ] Vylepsit OTEL logs

View File

@ -13,6 +13,7 @@
"nyholm/psr7": "*", "nyholm/psr7": "*",
"open-telemetry/exporter-otlp": "^1.0", "open-telemetry/exporter-otlp": "^1.0",
"open-telemetry/opentelemetry-auto-symfony": "^1.0@beta", "open-telemetry/opentelemetry-auto-symfony": "^1.0@beta",
"open-telemetry/opentelemetry-logger-monolog": "^1.0",
"open-telemetry/opentelemetry-propagation-server-timing": "^0.0.1", "open-telemetry/opentelemetry-propagation-server-timing": "^0.0.1",
"open-telemetry/opentelemetry-propagation-traceresponse": "^0.0.2", "open-telemetry/opentelemetry-propagation-traceresponse": "^0.0.2",
"open-telemetry/sdk": "^1.0", "open-telemetry/sdk": "^1.0",

60
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "15e05e23293f0f945b50f450fe8213c3", "content-hash": "a62a11e50f44a0e917cf2ab753b0a5d8",
"packages": [ "packages": [
{ {
"name": "composer/semver", "name": "composer/semver",
@ -761,6 +761,64 @@
}, },
"time": "2023-12-12T11:41:45+00:00" "time": "2023-12-12T11:41:45+00:00"
}, },
{
"name": "open-telemetry/opentelemetry-logger-monolog",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/opentelemetry-php/contrib-logger-monolog.git",
"reference": "da70f3678aba1e8187f889e78f3a2a55f43f6395"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/opentelemetry-php/contrib-logger-monolog/zipball/da70f3678aba1e8187f889e78f3a2a55f43f6395",
"reference": "da70f3678aba1e8187f889e78f3a2a55f43f6395",
"shasum": ""
},
"require": {
"monolog/monolog": "^1.1|^2|^3",
"open-telemetry/api": "^1.0.0beta16"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"google/protobuf": ">=3.5.0",
"guzzlehttp/guzzle": "^7.4",
"nyholm/psr7": "^1.6",
"open-telemetry/exporter-otlp": ">=1.0.0beta6",
"open-telemetry/sdk": ">=1.0.0beta7",
"phan/phan": "^5.0",
"phpbench/phpbench": "^1.2",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16",
"vimeo/psalm": "^4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"OpenTelemetry\\Contrib\\Logs\\Monolog\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"description": "OpenTelemetry Monolog handler.",
"homepage": "https://opentelemetry.io",
"keywords": [
"handler",
"logs",
"monolog",
"open-telemetry",
"opentelemetry",
"otel"
],
"support": {
"source": "https://github.com/opentelemetry-php/contrib-logger-monolog/tree/1.0.0"
},
"time": "2023-10-17T21:44:43+00:00"
},
{ {
"name": "open-telemetry/opentelemetry-propagation-server-timing", "name": "open-telemetry/opentelemetry-propagation-server-timing",
"version": "0.0.1", "version": "0.0.1",

View File

@ -5,6 +5,10 @@ monolog:
when@dev: when@dev:
monolog: monolog:
handlers: handlers:
otel: # Tohle bych v realny aplikaci nepouzil na devu
type: service
id: App\Bridge\Monolog\Handler\SymfonyOtelHandler
main: main:
type: stream type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log" path: "%kernel.logs_dir%/%kernel.environment%.log"
@ -40,6 +44,9 @@ when@test:
when@prod: when@prod:
monolog: monolog:
handlers: handlers:
otel:
type: service
id: App\Bridge\Monolog\Handler\SymfonyOtelHandler
main: main:
type: fingers_crossed type: fingers_crossed
action_level: error action_level: error

View File

@ -45,6 +45,7 @@ services:
$innerGenerator: '@App\Service\Remote\UsetrenoQRCodeProvider' $innerGenerator: '@App\Service\Remote\UsetrenoQRCodeProvider'
$cacheDuration: 'PT60S' $cacheDuration: 'PT60S'
App\Service\CurrencyListerInterface: '@App\Service\StaticCurrencyLister' App\Service\CurrencyListerInterface: '@App\Service\StaticCurrencyLister'
App\Service\QRCodeQROptionsProviderInterface: '@App\Service\QRCodeQROptionsDefaultProvider' App\Service\QRCodeQROptionsProviderInterface: '@App\Service\QRCodeQROptionsDefaultProvider'
App\Service\QRCodeGeneratorInterface: '@App\Service\CachedQRCodeGenerator' App\Service\QRCodeGeneratorInterface: '@App\Service\CachedQRCodeGenerator'

View File

@ -11,22 +11,40 @@ processors:
actions: actions:
- action: insert - action: insert
key: loki.attribute.labels key: loki.attribute.labels
value: log.file, log.line, log.module_path, http.request_id, http.uri value: context, code.filepath, code.namespace, code.function, code.lineno, http.request.method, http.request.body.size, url.full, url.scheme, url.path, http.route, http.response.status_code
- action: insert - action: insert
from_attribute: log.file from_attribute: context
key: log.file key: context
- action: insert - action: insert
from_attribute: log.line from_attribute: code.namespace
key: log.line key: code.namespace
- action: insert - action: insert
from_attribute: log.module_path from_attribute: code.function
key: log.module_path key: code.function
- action: insert - action: insert
from_attribute: http.request_id from_attribute: code.lineno
key: http.request_id key: code.lineno
- action: insert - action: insert
from_attribute: http.uri from_attribute: http.request.method
key: http.uri key: http.request.method
- action: insert
from_attribute: http.request.body.size
key: http.request.body.size
- action: insert
from_attribute: http.response.status_code
key: http.response.status_code
- action: insert
from_attribute: url.full
key: url.full
- action: insert
from_attribute: url.scheme
key: url.scheme
- action: insert
from_attribute: url.path
key: url.path
- action: insert
from_attribute: http.route
key: http.route
- action: insert - action: insert
key: loki.format key: loki.format
value: raw value: raw

View File

@ -0,0 +1,33 @@
<?php
namespace App\Bridge\Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\FormattableHandlerTrait;
use Monolog\LogRecord;
use OpenTelemetry\API\Globals;
use OpenTelemetry\Contrib\Logs\Monolog\Handler;
use Psr\Log\LogLevel;
use Symfony\Bridge\Monolog\Formatter\VarDumperFormatter;
final class SymfonyOtelHandler extends AbstractHandler
{
protected readonly Handler $innerHandler;
public function __construct() {
parent::__construct();
$this->innerHandler = new Handler(
Globals::loggerProvider(),
LogLevel::INFO, //or `Logger::INFO`, or `Level::Info` depending on monolog version
true,
);
}
public function handle(LogRecord $record): bool
{
return $this->innerHandler->handle($record);
}
}