myschoolmanagement / elastic-apm-symfony-bundle
将 Elastic APM 集成到 Symfony
Requires
- php: ^7.1|^8.0
- symfony/config: ^3.4|^4.0|^5.0|^6.0
- symfony/console: ^3.4|^4.0|^5.0|^6.0
- symfony/dependency-injection: ^3.4|^4.0|^5.0|^6.0
- symfony/event-dispatcher: ^3.4|^4.0|^5.0|^6.0
- symfony/http-kernel: ^3.4|^4.0|^5.0|^6.0
Requires (Dev)
README
此捆绑包将 Elastic APM PHP API 集成到 Symfony。有关 Elastic APM 的更多信息,请访问 https://elastic.ac.cn/apm。此捆绑包添加了更多基本功能。以下是一个快速列表
-
更好的事务命名策略:您的交易跟踪可以准确地通过路由名称、控制器名称、请求 URI 命名,或者您可以通过一个无缝的界面选择一个自定义命名策略,该界面使用您认为合适的任何命名约定。在运行控制台命令时,它也将事务名称设置为命令名称。
-
控制台命令增强:在运行控制台命令时,它将 CLI 传递的选项和参数设置为事务跟踪的自定义参数,以便更容易调试。
-
异常监听:它还捕获 Web 请求和控制台命令中的所有 Symfony 异常,并将它们发送到 Elastic APM。
-
交互器服务:它通过 Service 类
ElasticApmInteractorInterface::class
提供对 Elastic APM API 的访问。可以将它注入到任何类、控制器、服务中,以与 APM 进行通信。如果使用adaptive
交互,则在扩展未加载时(例如在开发环境中)将忽略任何 APM 调用。$this->apm->addLabel('name', 'john');
-
日志支持:在开发中,您可能不会设置 Elastic APM。有配置可以启用日志记录,将所有操作输出到您的 Symfony 日志中,从而模拟实际生产中的操作。
安装
步骤 0 : 安装 Elastic APM
遵循 https://elastic.ac.cn/guide/en/apm/agent/php/current/intro.html。
步骤 1: 添加依赖
$ composer require myschoolmanagement/elastic-apm-symfony-bundle
步骤 2: 注册捆绑包
然后通过您的内核注册捆绑包
<?php // in AppKernel::registerBundles() $bundles = array( // ... new ElasticApmBundle\ElasticApmBundle(), // ... );
步骤 3: 配置 Elastic APM
您应该在此处查看所有配置项,https://elastic.ac.cn/guide/en/apm/agent/php/current/configuration.html。这些必须通过环境变量或 php.ini
设置。它们不能在请求期间设置,因此捆绑包不支持设置它们。
步骤 4: 配置捆绑包
以下是可以配置捆绑包的所有选项。
# app/config/config.yml elastic_apm: enabled: true # Defaults to true logging: false # If true, logs all interactions to the Symfony log (default: false) interactor: ~ # The interactor service that is used. Setting enabled=false will override this value deprecations: true # If true, reports deprecations to Elastic APM (default: true) track_memory_usage: false # If true, records peak memory usage memory_usage_label: memory_usage # The name of the custom label to write memory usage to exceptions: enabled: true # If true, sends exceptions (default: true) should_unwrap_exceptions: false # If true, will also sends the previous/nested exception (default: false) ignored_exceptions: # List of exception classes to ignore - An\Ignored\Exception http: enabled: true transaction_naming: route # route, controller or service (see below) transaction_naming_service: ~ # Transaction naming service (see below) commands: enabled: true # If true, enhances CLI commands with options and arguments (default: true) explicitly_collect_exceptions: true # Turn this off if you are experiencing multiple reports of exceptions.
增强 RUM 仪器
由于有多种安装和配置仪器的方法,此捆绑包不集成 RUM(请参阅 https://elastic.ac.cn/guide/en/apm/server/current/overview.html)。
事务命名策略
捆绑包自带三种内置事务命名策略
route
controller
uri
分别根据路由、控制器或请求 URI 命名事务。然而,捆绑包支持通过 service
配置选项自定义事务命名策略。如果您已选择 service
配置选项,则必须通过 transaction_naming_service
配置选项传递自己的事务命名服务名称。
事务命名服务类必须实现 ElasticApmBundle\TransactionNamingStrategy\TransactionNamingStrategyInterface
接口。有关创建自己的服务的更多信息,请参阅 Symfony 文档中的 Creating/Configuring Services in the Container。
交互器服务
配置键elastic_apm.interactor
将接受一个实现ElasticApmInteractorInterface
的服务ID。此包附带了一些可能适合您的服务。
请注意,如果您设置elastic_apm.enabled: false
,则无论为elastic_apm.interactor
使用什么值,您都将始终使用BlackholeInteractor
。
Monolog
Elastic APM PHP扩展不支持将日志条目发送为错误之外的内容。我们建议在您的应用程序配置中添加新的日志处理程序并配置elasticsearch(或Elastica)客户端。
示例
# app/config/config.yml monolog: handlers: errors_to_elasticsearch: type: buffer level: error handler: elasticsearch elasticsearch: type: service id: 'Monolog\Handler\ElasticsearchHandler'
故障排除
命令异常被记录多次
PHP APM将自动收集未处理的异常。此包还会安装命令异常的监听器。我们的监听器和默认行为可能会冲突,从而导致此行为。
要修复此问题,您可以在command
配置节点下关闭explicitly_collect_exceptions
。
鸣谢
此包主要基于https://github.com/ekino/EkinoNewRelicBundle的工作成果。