laraotel/opentelemetry-laravel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laraotel/opentelemetry-laravel

Composer 安装命令:

composer require laraotel/opentelemetry-laravel

包简介

This package provides a simple way to use Telemetry From OpenTelemetry Otel to your Laravel application to Measure performance across jobs and services.

README 文档

README

Total Downloads License

This package provides a simple way to use Telemetry From OpenTelemetry OTel into your Laravel application to Measure performance across jobs and services, database queries, events etc..

Introduction

OpenTelemetry, or OTel for short, is an Observability tools designed to create and manage telemetry data such as traces, metrics and logs, to collect information on how your entire system is behaving.

You can easily measure performance of a Laravel powered system. It can transmit the results to a tracing tool like Jaeger, Zipkin Or you can export data into Console, Json, text etc..

Bundle Zipkin and Jaeger into your Application

To visualize traces exported from our application, we need to integrate open source tracing tools Zipkin and Jaeger into our setup using docker.

First, we create a docker-compose.yaml file in the root of our project, with content as follows:

version: '3.7'
services:
    zipkin:
        image: openzipkin/zipkin-slim
        ports:
            - "9411:9411"
    jaeger:
        image: jaegertracing/all-in-one
        environment:
            COLLECTOR_ZIPKIN_HOST_PORT: 9412

        ports:
            - "9412:9412"
            - "16686:16686"

Next, we pull in Zipkin and Jaeger by running docker-compose up -d.

We can confirm that Zipkin is up by navigating to http://localhost:9411/ on our browser. For Jaeger, navigating to http://localhost:16686/ on our browser should display the Jaeger home page.

laraotel laraotel

Installation

You can install the package via composer:

composer require laraotel/opentelemetry-laravel:2.0.8

Important Note: The opentelemetry extension must be enabled on your machine.

Usage

Configuration

Publish the configuration file:

php artisan vendor:publish --provider="LaraOTel\OpenTelemetryLaravel\OpenTelemetryServiceProvider" --tag="config"

Update the environment variables

You can find them in configuration file: config/otel.php.

Register the middleware

you can register the middleware in the app/Http/Kernel.php:

protected $middleware = [
    \LaraOTel\OpenTelemetryLaravel\Middlewares\MeasureRequest::class,
    // ...
];

In laravel 11 you can not register in the kernel.php the middlewares anymore. You can register your global middleware in bootstrap/app.php

->withMiddleware(function (Middleware $middleware) {
    $middleware->append(\LaraOTel\OpenTelemetryLaravel\Middlewares\MeasureRequest::class);
})

or you can set the env variable OTEL_AUTO_TRACE_REQUESTS to true to enable it automatically.

Watchers

This package provides some watchers to help you trace your application:

  • LaraOTel\OpenTelemetryLaravel\Watchers\AuthenticateWatcher to trace authentications.
  • LaraOTel\OpenTelemetryLaravel\Watchers\CacheWatcher to trace cache operations.
  • LaraOTel\OpenTelemetryLaravel\Watchers\DatabaseQueryWatcher to trace database queries.
  • LaraOTel\OpenTelemetryLaravel\Watchers\QueueWatcher to trace job execution.
  • LaraOTel\OpenTelemetryLaravel\Watchers\RedisWatcher to trace redis operations.
  • LaraOTel\OpenTelemetryLaravel\Watchers\EventWatcher to trace event opearations.
  • LaraOTel\OpenTelemetryLaravel\Watchers\HttpClientWatcher to trace http client requests.
  • LaraOTel\OpenTelemetryLaravel\Watchers\LogWatcher to trace log operations.

You can enable or disable them in the configuration file: config/otel.php.

Custom span

You can create a custom span by using the LaraOTel\OpenTelemetryLaravel\Facades\Measure facade:

use LaraOTel\OpenTelemetryLaravel\Facades\Measure;

Measure::span('my-web-request')->measure(function() {
    // ...
});

or manually start and end a span:

Measure::start('my-web-request');

// ...

Measure::end();

and you can modify the span attributes by using a closure:

Measure::start('my-web-request', function($span) {
    $span->setAttribute('key', 'value');
    // ...
});

// ...
Measure::end();

of course, you can get the span instance by using the Measure::span() method:

$span = Measure::span('my-web-request');
$span->setAttribute('key', 'value');
$scope = $span->activate();

// ...

$span->end();
$scope->detach();

laraotel laraotel laraotel

Available Drivers

'default' => env('OTEL_DEFAULT_TRACER', 'log'),

# available drivers: `console`, `log`, `text`, `zipkin`, `http-json`, `http-binary`, `grpc`.
'tracers' => [
    'console' => [
        'driver' => 'console',
        'transport' => 'stream',
        'span_exporter' => 'console',
    ],

    'log' => [
        'driver' => 'log',
        'transport' => 'stream',
        'span_exporter' => 'console',
        'endpoint' => storage_path('logs/otel.log'),
    ],

    'text' => [
        'driver' => 'text',
        'transport' => 'stream',
        'endpoint' => storage_path('logs/otel.text'),
    ],

    'zipkin' => [
        'driver' => 'zipkin',
        'transport' => 'http',
        'span_exporter' => 'otlp',
        'endpoint' => env('OTEL_EXPORTER_ZIPKIN_ENDPOINT', 'http://zipkin:9411/api/v2/spans'),
        'content_type' => 'application/json',
    ],

    'http-json' => [
        'driver' => 'http-json',
        'transport' => 'http',
        'span_exporter' => 'otlp',
        'endpoint' => env('OTEL_HTTP_JSON_ENDPOINT', 'http://localhost:9411/v1/traces'),
        'content_type' => 'application/json',
    ],

    'http-binary' => [
        'driver' => 'http-binary',
        'transport' => 'http',
        'span_exporter' => 'otlp',
        'endpoint' => env('OTEL_HTTP_BINARY_ENDPOINT', 'http://localhost:4318/v1/traces'),
        'content_type' => 'application/x-protobuf',
    ],
],

Available Logs Level

use LaraOTel\OpenTelemetryLaravel\Facades\Logger;

Logger::debug('my log message');
Logger::info('my log message');
Logger::warning('my log message');
Logger::error('my log message');

UML Diagram of the package design

laraotel

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

laraotel/opentelemetry-laravel 适用场景与选型建议

laraotel/opentelemetry-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.49k 次下载、GitHub Stars 达 84, 最近一次更新时间为 2024 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laraotel-opentelemetry-laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laraotel/opentelemetry-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laraotel/opentelemetry-laravel 我们能提供哪些服务?
定制开发 / 二次开发

基于 laraotel/opentelemetry-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 84
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 84
  • Watchers: 3
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-12