定制 eerzho/opentelemetry-auto-class 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

eerzho/opentelemetry-auto-class

Composer 安装命令:

composer require eerzho/opentelemetry-auto-class

包简介

Automatic OpenTelemetry tracing for PHP methods via the #[Traceable] attribute

README 文档

README

Version Downloads PHP License

Automatic OpenTelemetry tracing for PHP methods via the #[Traceable] attribute. Framework-agnostic core — mark any class with the attribute, and spans are created automatically using the ext-opentelemetry hook API.

This is a read-only sub-split. Please open issues and pull requests in the monorepo.

Installation

composer require eerzho/opentelemetry-auto-class

Requirements:

Usage

Basic

Add #[Traceable] to a class — all public methods will be traced automatically:

use Eerzho\Instrumentation\Class\Attribute\Traceable;
use Eerzho\Instrumentation\Class\AttributeScanner;
use Eerzho\Instrumentation\Class\ClassInstrumentation;

#[Traceable]
class OrderService
{
    public function create(array $items): void
    {
        // span "OrderService::create" is created automatically
    }

    public function cancel(int $orderId): void
    {
        // span "OrderService::cancel" is created automatically
    }
}

// Register instrumentation
$map = AttributeScanner::scan([OrderService::class]);
ClassInstrumentation::register($map);

For Laravel and Symfony, use the framework integrations that handle class discovery automatically: opentelemetry-auto-class-laravel / opentelemetry-auto-class-symfony

Exclude methods

Use the exclude parameter to skip specific methods from tracing:

use Eerzho\Instrumentation\Class\Attribute\Traceable;

#[Traceable(exclude: ['healthCheck', 'getVersion'])]
class PaymentService
{
    public function charge(int $amount, string $currency): void
    {
        // traced
    }

    public function healthCheck(): bool
    {
        // NOT traced
        return true;
    }

    public function getVersion(): string
    {
        // NOT traced
        return '1.0.0';
    }
}

Exclude arguments

By default, all method arguments are captured as span attributes. Use #[Arguments(exclude: [...])] on a method to hide sensitive parameters:

use Eerzho\Instrumentation\Class\Attribute\Arguments;
use Eerzho\Instrumentation\Class\Attribute\Traceable;

#[Traceable]
class AuthService
{
    #[Arguments(exclude: ['password', 'token'])]
    public function login(string $email, string $password, string $token): void
    {
        // span captures "email" attribute only
        // "password" and "token" are excluded
    }

    public function logout(int $userId): void
    {
        // span captures "userId" attribute (no exclusions)
    }
}

Without attributes

You can register classes for tracing without using #[Traceable] — build the method map manually and pass it to ClassInstrumentation::register():

class OrderService
{
    public function create(array $items, int $priority, string $note): void {}

    public function cancel(int $orderId): void {}

    public function archive(int $orderId): void {}
}
use Eerzho\Instrumentation\Class\ClassInstrumentation;

ClassInstrumentation::register([
    OrderService::class => [
        // trace with arguments, skip 'priority' (position 1)
        'create' => ['items' => 0, 'note' => 2],
        // trace without capturing arguments
        'cancel' => [],
        // 'archive' is not listed — it will NOT be traced
    ],
]);

Each entry maps a class to its methods, and each method to its arguments (parameter name => position). Methods not listed are not traced. Arguments not listed are not captured.

How it works

Each traced method call produces a span:

  • Name: ClassName::methodName
  • Kind: INTERNAL

With attributes:

Attribute Value
code.function.name ClassName::methodName
code.file.path File where the method is defined
code.line.number Line number of the method
Method arguments Parameter name → serialized value

If a method throws an exception, the span records an exception event and sets status to ERROR:

Event attribute Value
exception.type Exception class name
exception.message Exception message
exception.stacktrace Full stack trace

Argument serialization

Arguments are serialized to span-compatible types:

Type Result Example
string, int, float, bool As-is "hello", 42, 3.14, true
null "null" null"null"
BackedEnum Backing value Status::Active"active"
Object with __toString() String cast $money"100 USD"
Object without __toString() Class name (FQCN) $dto"App\DTO\Order"
array JSON string [1, 2]"[1,2]"
Other (resource, ...) gettype() result "resource"

If JSON encoding of an array fails, the value is serialized as "array".

Disabling instrumentation

To disable tracing at runtime, use the standard OpenTelemetry environment variable:

OTEL_PHP_DISABLED_INSTRUMENTATIONS=class

Limitations

  • Only public methods are traced (protected and private are ignored)
  • Abstract classes, interfaces, traits, and enums are skipped
  • Requires ext-opentelemetry installed and loaded

License

MIT

eerzho/opentelemetry-auto-class 适用场景与选型建议

eerzho/opentelemetry-auto-class 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 43
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-20