opentelemetry之php-Automatic Instrumentation环境安装及接入.md

阅读php的opemtelemetry安装文档,尝试了挺久的,目前环境还是部分成功,记录一下便于查阅;

环境设置

(1)传统centos系统安装

yum  install -y gcc make autoconf
php pickle.phar install --source https://github.com/open-telemetry/opentelemetry-php-instrumentation.git#1.0.0beta2   //此步骤出错,如下

 

 - Installing opentelemetry-php-instrumentation (1.0.0beta2): Cloning 1.0.0beta2
Cloning failed using an ssh key for authentication, enter your GitHub credentials to access private repos
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+on+6e8acdc4a9ed+2023-04-13+0353
PHP Fatal error:  Uncaught Error: Call to a member function getName() on null in phar:///pickle.phar/vendor/composer/composer/src/Composer/Util/GitHub.php:91
Stack trace:
#0 phar:///pickle.phar/vendor/composer/composer/src/Composer/Util/Git.php(110): Composer\Util\GitHub->authorizeOAuthInteractively()
#1 phar:///pickle.phar/vendor/composer/composer/src/Composer/Downloader/GitDownloader.php(90): Composer\Util\Git->runCommand()
#2 phar:///pickle.phar/vendor/composer/composer/src/Composer/Downloader/VcsDownloader.php(90): Composer\Downloader\GitDownloader->doDownload()
#3 phar:///pickle.phar/src/Package/Convey/Command/Git.php(105): Composer\Downloader\VcsDownloader->download()
#4 phar:///pickle.phar/src/Package/Convey/Command/Git.php(50): Pickle\Package\Convey\Command\Git->fetch()
#5 phar:///pickle.phar/src/Package/Convey.php(63): Pickle\Package\Convey\Command\Git->execute()
#6 phar:///pickle.phar/src/Console/Helper/PackageHelper.php(119): Pickle\Package\Convey->deliver()
#7 phar:///pickle.phar/src/Console/Command/InstallerCommand.php(217): Pickle\Console\Helper\PackageHelper->convey()
#8 phar:///pickle.phar/vendor/symfony/console/Command/Command.php(298): Pickle\Console\Command\InstallerCommand->execute()
#9 phar:///pickle.phar/vendor/symfony/console/Application.php(1005): Symfony\Component\Console\Command\Command->run()
#10 phar:///pickle.phar/vendor/symfony/console/Application.php(299): Symfony\Component\Console\Application->doRunCommand()
#11 phar:///pickle.phar/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun()
#12 phar:///pickle.phar/bin/pickle(38): Symfony\Component\Console\Application->run()
#13 /pickle.phar(10): require('...')
#14 {main}
  thrown in phar:///pickle.phar/vendor/composer/composer/src/Composer/Util/GitHub.php on line 91

Fatal error: Uncaught Error: Call to a member function getName() on null in phar:///pickle.phar/vendor/composer/composer/src/Composer/Util/GitHub.php:91
Stack trace:
#0 phar:///pickle.phar/vendor/composer/composer/src/Composer/Util/Git.php(110): Composer\Util\GitHub->authorizeOAuthInteractively()
#1 phar:///pickle.phar/vendor/composer/composer/src/Composer/Downloader/GitDownloader.php(90): Composer\Util\Git->runCommand()
#2 phar:///pickle.phar/vendor/composer/composer/src/Composer/Downloader/VcsDownloader.php(90): Composer\Downloader\GitDownloader->doDownload()
#3 phar:///pickle.phar/src/Package/Convey/Command/Git.php(105): Composer\Downloader\VcsDownloader->download()
#4 phar:///pickle.phar/src/Package/Convey/Command/Git.php(50): Pickle\Package\Convey\Command\Git->fetch()
#5 phar:///pickle.phar/src/Package/Convey.php(63): Pickle\Package\Convey\Command\Git->execute()
#6 phar:///pickle.phar/src/Console/Helper/PackageHelper.php(119): Pickle\Package\Convey->deliver()
#7 phar:///pickle.phar/src/Console/Command/InstallerCommand.php(217): Pickle\Console\Helper\PackageHelper->convey()
#8 phar:///pickle.phar/vendor/symfony/console/Command/Command.php(298): Pickle\Console\Command\InstallerCommand->execute()
#9 phar:///pickle.phar/vendor/symfony/console/Application.php(1005): Symfony\Component\Console\Command\Command->run()
#10 phar:///pickle.phar/vendor/symfony/console/Application.php(299): Symfony\Component\Console\Application->doRunCommand()
#11 phar:///pickle.phar/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun()
#12 phar:///pickle.phar/bin/pickle(38): Symfony\Component\Console\Application->run()
#13 /pickle.phar(10): require('...')
#14 {main}
  thrown in phar:///pickle.phar/vendor/composer/composer/src/Composer/Util/GitHub.php on line 91

设置了认证,还是出错,目前按官方文档操作的错误还是没有解决;

  vi  /root/.config/composer/auth.json
{
    "bitbucket-oauth": {},
    "github-oauth": {"github.com":"ghp_6fomvltWFs6KAZxxx"},
    "gitlab-oauth": {},
    "gitlab-token": {},
    "http-basic": {},
    "bearer": {}
}

(2)镜像式安装环境  

制作相应php环境镜像Dockerfile:

FROM php:8.2-fpm-alpine as build

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions open-telemetry/opentelemetry-php-instrumentation@1.0.0beta2

运行镜像,验证环境:

docker run -it -d --name centos3 -p 5023:22 -p 8088:80 --privileged=true oh_myphp:1.0.0 /sbin/init
docker exec -it centos3 /bin/sh
php -m | grep  otel_instrumentation

接入设置环境

OTEL_PHP_AUTOLOAD_ENABLED=true \
OTEL_SERVICE_NAME=your-service-name \
OTEL_TRACES_EXPORTER=otlp \
OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4317 \
OTEL_PROPAGATORS=baggage,tracecontext \

写入代码接入Example:

<?php
OpenTelemetry\Instrumentation\hook(
    'class': DemoClass::class,
    'function': 'run',
    'pre': static function (DemoClass $demo, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) {
        static $instrumentation;
        $instrumentation ??= new CachedInstrumentation('example');
        $instrumentation->tracer()->spanBuilder($class)
            ->startSpan()
            ->activate();
    },
    'post': static function (DemoClass $demo, array $params, $returnValue, ?Throwable $exception) use ($tracer) {
        $scope = Context::storage()->scope();
        $scope->detach();
        $span = Span::fromContext($scope->context());
        if ($exception) {
            $span->recordException($exception);
            $span->setStatus(StatusCode::STATUS_ERROR);
        }
        $span->end();
    }
);

$demo = new DemoClass();
$demo->run();

参考:

Automatic Instrumentation | OpenTelemetry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值