定制 open-telemetry/opentelemetry-logger-monolog 二次开发

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

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

open-telemetry/opentelemetry-logger-monolog

Composer 安装命令:

composer require open-telemetry/opentelemetry-logger-monolog

包简介

OpenTelemetry Monolog handler.

README 文档

README

Releases Issues Source Mirror Latest Version Stable

This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.

OpenTelemetry Monolog handler

A monolog handler for OpenTelemetry. See https://opentelemetry.io/docs/instrumentation/php/manual/#logs for further documentation.

Requirements

API + SDK

This package depends on the OpenTelemetry API, but a configured OpenTelemetry SDK should also be provided.

Exporter

Usually logs are exported to a receiver via the otlp protocol in the protobuf format, via http or gRPC.

This requires:

  • a protobuf implementation; either the protobuf extension or the google/protobuf package
  • the open-telemetry/exporter-otlp package
  • the open-telemetry/transport-grpc package, if using gRPC transport
  • a PSR-7 and PSR-18 implementation, if using HTTP transport

Receiver

Logs must be emitted to a receiver/system that understands the OpenTelemetry protocol, such as the OpenTelemetry collector.

Installation

composer require open-telemetry/opentelemetry-logger-monolog

Usage

The OpenTelemetry handler, configured with an OpenTelemetry LoggerProvider, is used to send Monolog LogRecords to OpenTelemetry.

The LoggerProvider can be configured in a number of ways: manually, via an SDK Builder, or automatically (using environment/php.ini variables).

Manual configuration

Set up an SDK LoggerProvider and pass it to the handler:

$loggerProvider = new \OpenTelemetry\SDK\Logs\LoggerProvider(/* params */);
$handler = new \OpenTelemetry\Contrib\Logs\Monolog\Handler(
    $loggerProvider,
    'info',
    true,
);

Automatic configuration

If you use OpenTelemetry SDK autoloading, you can retrieve the global logger provider. That may be a no-op implementation if there was any misconfiguration.

See autoload-sdk example for how to use autoloading with the OpenTelemetry SDK.

Create a Logger

Finally, add the handler to a Monolog logger:

$logger = new \Monolog\Logger(
    'name',
    [$handler],
);
$logger->info('hello world');

Attributes Mode

This OpenTelemetry handler will convert any context array or extra array in the Monolog\LogRecord to OpenTelemetry\API\Logs\LogRecord attributes. There are two options for handling conflicts between the classes.

Note 1: Exceptions have special handling in both the PSR-3 spec and the OpenTelemetry spec. If a PHP Throwable is included in the context array with a key of exception, it will be added as exception. attributes to the OpenTelemetry Log Record.

Note 2: Both Monolog and the OpenTelemetry Protocol employ serialization algorithms when encoding attributes. This combination can lead to obtuse JSON blobs in the OTLP log records; this can be avoided by using only scalar values for attributes.

By default, the attribute keys will be context and extra, along with context. and extra. prefixed keys with the individual array entries. Example:

$host = new stdClass();
$host->name = 'example.com';
$host->tcp = 80;
new Monolog\LogRecord(
    ...,
    context: [
        'foo' => 'bar',
        'baz' => 'bat',
    ],
    extra: [
        'host' => $host,
    ]
);

/**
 * becomes:
 *
 * OpenTelemetry\API\Logs\LogRecord (
 *     ...,
 *     attributes => array (
 *         context => array (
 *             foo => 'bar',
 *             baz => 'bat',
 *         )
 *         context.foo => 'bar'
 *         context.baz => 'bat'
 *         extra => array (
 *             host => array (
 *                 stdClass => array (
 *                     name => 'example.com'
 *                     tcp => 80
 *                 )
 *             )
 *         )
 *         extra.host => stdClass (
 *             name => 'example.com'
 *             tcp => 80
 *         )
 *     )
 * )
 */

Alternatively, if your context and extra keys do not conflict with OpenTelemetry Semantic Conventions for Attribute keys, you can set OTEL_PHP_MONOLOG_ATTRIB_MODE=otel and they will be sent directly as Attributes. Example:

new Monolog\LogRecord(
    ...,
    context: [
        'myapp.data.foo' => 'bar',
        'myapp.data.baz' => 'bat',
    ],
    extra: [
        'server.address' => 'example.com',
        'server.port' => 80,
    ]
);

/**
 * becomes:
 *
 * OpenTelemetry\API\Logs\LogRecord (
 *     ...,
 *     attributes => array (
 *         myapp.data.foo => 'bar'
 *         myapp.data.baz => 'bat'
 *         server.address => 'example.com'
 *         server.port => 80
 *      )
 * )
 */

open-telemetry/opentelemetry-logger-monolog 适用场景与选型建议

open-telemetry/opentelemetry-logger-monolog 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.28M 次下载、GitHub Stars 达 9, 最近一次更新时间为 2023 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 open-telemetry/opentelemetry-logger-monolog 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.28M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 29
  • 依赖项目数: 14
  • 推荐数: 3

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2023-04-27