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

YII2 : cant get traceId in logMessage #1487

Open
runtu666 opened this issue Jan 24, 2025 · 6 comments
Open

YII2 : cant get traceId in logMessage #1487

runtu666 opened this issue Jan 24, 2025 · 6 comments
Labels
bug Something isn't working

Comments

@runtu666
Copy link

runtu666 commented Jan 24, 2025

code :

<?php

namespace app\components;

use yii\helpers\VarDumper;
use yii\log\FileTarget;

class JsonLogTarget extends FileTarget
{
    public function export(): void
    {
        foreach ($this->messages as $message) {
            list($text, $level, $category, $timestamp) = $message;
            if (!is_string($text)) {
                if ($text instanceof \Throwable) {
                    $text = (string)$text;
                } else {
                    $text = VarDumper::export($text);
                }
            }
            $log = [
                'timestamp' => $timestamp,
                'level' => $level,
                'category' => $category,
                'message' => $text,
                'trace' => $message[4] ?? null,  // 如果有堆栈信息
            ];
            $span = \OpenTelemetry\API\Trace\Span::getCurrent();
            $log['traceId'] = $span->getContext()->getTraceId();
            $log['spanId'] = $span->getContext()->getSpanId();
            $text = json_encode($log) . PHP_EOL;
            file_put_contents('php://stdout', $text);
        }
    }
}

output:
{"timestamp":1737686232.099166,"level":1,"category":"application","message":"test","trace":[{"file":"\/var\/www\/html\/controllers\/SiteController.php","line":68,"function":"error","class":"yii\\BaseYii","type":"::"}],"traceId":"00000000000000000000000000000000","spanId":"0000000000000000"}

why the traceId and spanId is 000000...

@runtu666 runtu666 added the bug Something isn't working label Jan 24, 2025
@brettmc
Copy link
Collaborator

brettmc commented Jan 24, 2025

That looks like it should work. Can you confirm that you have an active span at the time that the log was generated? It's important that it's active, since Span::getCurrent() fetches the active span from context, eg:

$span = $tracer->spanBuilder('test');
var_dump(Span::getCurrent()->getContext()->getTraceId()); //all 0's
$scope = $span->activate();
var_dump(Span::getCurrent()->getContext()->getTraceId()); //a trace id
$logger->log('test');
$scope->detach();
$span->end();

@runtu666
Copy link
Author

runtu666 commented Jan 24, 2025

i can get a trace id in controller.

But I want to write each traceId to the log, so I implemented
The export method of the yii\log\Target class cannot get the traceId.

Image

@brettmc

@runtu666
Copy link
Author

Show you my code:
controller:
Image
logger:
Image

output:

Image

why it can get traceId in controller, cant get in logger?

@brettmc
Copy link
Collaborator

brettmc commented Jan 28, 2025

Looking at Yii's documentation, I think your issue is that logs are collected in memory, then only flushed when a request finishes or the buffer is full.

When the logger object flushes log messages to log targets, they do not get exported immediately. Instead, the message exporting only occurs when a log target accumulates certain number of the filtered messages.

That means that your code in export (eg $span->getContext()->getTraceId()) is running at shutdown, not when the log was created, and so there is no longer an active span.

@runtu666
Copy link
Author

runtu666 commented Feb 6, 2025

So, How can I write traceId into log with yii2?

@brettmc
Copy link
Collaborator

brettmc commented Feb 6, 2025

So, How can I write traceId into log with yii2?

If you're willing to try out auto-instrumentation, our psr-3 auto-instrumentation does what you want: https://github.com/open-telemetry/opentelemetry-php-contrib/blob/main/src/Instrumentation/Psr3/tests/phpt/inject_yii.phpt

If you want to know how to do it with your current approach, you might need to ask this question in the yii2 support/help place. The question you need answered is how you can modify a log record immediately, rather than when they are flushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants