codekandis/sentry-client 问题修复 & 功能扩展

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

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

codekandis/sentry-client

Composer 安装命令:

composer require codekandis/sentry-client

包简介

This library represents a wrapper for the `Sentry SDK` with enhanced development features.

README 文档

README

Version License Minimum PHP Version Code Coverage

codekandis/sentry-client is a wrapper library for the Sentry SDK package getsentry/sentry-php, currently supporting the Sentry SDK version 2.x. It provides the functionality of the wrapped package by an object-oriented API.

Index

Installation

Install the latest version with

$ composer require codekandis/sentry-client

How to use

Create a configuration

There's two possibilites.

Use the default configuration

For convenience the default configuration SentryClientConfiguration implements the fluent interface.

<?php declare( strict_types = 1 );
namespace Vendor\Project;

use CodeKandis\SentryClient\Configurations\SentryClientConfiguration;

$sentryClientConfiguration = ( new SentryClientConfiguration() )
	->setDsn( 'dsn' )
	->setErrorTypes( E_ALL )
	->setDisplayErrors( true );

Implement the configuration interface on your own

The SentryClient comes with the configuration interface SentryClientConfigurationInterface. So you are enabled to implement a configuration on your own.

<?php declare( strict_types = 1 );
namespace Vendor\Project;

use CodeKandis\SentryClient\Configurations\SentryClientConfigurationInterface;

class SentryClientConfiguration implements SentryClientConfigurationInterface
{
	public function getDsn(): string
	{
		return 'dsn';
	}
	
	/**
	 * ...
	 */
}

$sentryClientConfiguration = new SentryClientConfiguration();

Instantiate the Sentry Client

<?php declare( strict_types = 1 );
namespace Vendor\Project;

use CodeKandis\SentryClient\Configurations\SentryClientConfiguration;
use CodeKandis\SentryClient\SentryClient;
use const E_ALL;

$sentryClient = new SentryClient(
	( new SentryClientConfiguration() )
	    ->setDsn( 'dsn' )
		->setErrorTypes( E_ALL )
		->setDisplayErrors( true )
);

As soon as the SentryClient is instantiated the PHP directives error_reporting and display_errors will be set immediately. Be aware that display_errors, once set to true, will take effect in displaying all captured events. So besides errors and exceptions manually captured messages are displayed as well.

Capturing events

There are two methods of capturing messages, errors and exceptions - so-called events in terms of Sentry.

Manual capturing

Messages
<?php declare( strict_types = 1 );
namespace Vendor\Project;

use CodeKandis\SentryClient\Configurations\SentryClientConfiguration;
use CodeKandis\SentryClient\SentryClient;
use CodeKandis\SentryClient\Severities;

( new SentryClient( new SentryClientConfiguration() ) )
	->captureMessage(
		'This is a message.',
		Severities::INFO,
		[
			'some' => 'context'
		],
		[
			'some tag',
			'another tag'
		],
		[
			'id'         => 'some username',
			'ip_address' => '42.42.42.42'
		]
	);
Errors

Only the last occured error can be captured.

<?php declare( strict_types = 1 );
namespace Vendor\Project;

use CodeKandis\SentryClient\Configurations\SentryClientConfiguration;
use CodeKandis\SentryClient\SentryClient;
use CodeKandis\SentryClient\Severities;

( new SentryClient( new SentryClientConfiguration() ) )
	->captureLastError(
		Severities::ERROR,
		[
			'some' => 'context'
		],
		[
			'some tag',
			'another tag'
		],
		[
			'id'         => 'some username',
			'ip_address' => '42.42.42.42'
		]
	);
Exceptions
<?php declare( strict_types = 1 );
namespace Vendor\Project;

use CodeKandis\SentryClient\Configurations\SentryClientConfiguration;
use CodeKandis\SentryClient\SentryClient;
use CodeKandis\SentryClient\Severities;
use Exception;

( new SentryClient( new SentryClientConfiguration() ) )
	->captureThrowable(
		new Exception( 'This is an exception' ),
		Severities::FATAL,
		[
			'some' => 'context'
		],
		[
			'some tag',
			'another tag'
		],
		[
			'id'         => 'some username',
			'ip_address' => '42.42.42.42'
		]
	);

Automatic Capturing

The SentryClient comes with built-in error and exception handlers. Once the automatic capturing of events is enabled the occured events will be captured by that handlers and sent to your configured Sentry instance.

To enable the automatic capturing you just have to call SentryClient::register().

<?php declare( strict_types = 1 );
namespace Vendor\Project;

use CodeKandis\SentryClient\Configurations\SentryClientConfiguration;
use CodeKandis\SentryClient\SentryClient;

( new SentryClient( new SentryClientConfiguration() ) )
	->register();

Using custom error and exception handlers

In case you have already set your own error or exception handlers, registering the SentryClient for automatic capturing won't affect them being executed.

<?php declare( strict_types = 1 );
namespace Vendor\Project;

use CodeKandis\SentryClient\Configurations\SentryClientConfiguration;
use CodeKandis\SentryClient\SentryClient;
use Exception;
use Throwable;
use function set_error_handler;
use function set_exception_handler;
use function trigger_error;

set_error_handler(
	function ( int $level, string $message )
	{
		echo 'Error handler: ' . $message . "\n";
	}
);

set_exception_handler(
	function ( Throwable $exception )
	{
		echo 'Exception handler: ' . $exception->getMessage() . "\n";
	}
);

( new SentryClient( new SentryClientConfiguration() ) )
	->register();

trigger_error( 'An error occured.' );            // outputs `Error handler: An error occured.`
throw new Exception( 'An exception occured.' );  // outputs `Exception handler: An exception occured.`

The Sentry Client pushes its own handlers on top of its internal managed stack of handlers. So the error and exception handlers are executed in the following order:

  1. the SentryClient handler
  2. the wrapped Sentry SDK handler
  3. your custom handler

So it's guaranteed all events will be sent to your Sentry instance and your handlers are executed as well.

Testing

To get the integration tests running some settings must be made in the TestConstants. All necessary information can be found in your Sentry instance.

Depending on the workload of your Sentry instance your events may not be fetchable immediately by the API. So a proper TestConstants::EVENT_PROCESSING_THRESHOLD in seconds must be set.

Due to the nature of the wrapped Sentry SDK executing all test cases at once causes some integration tests to fail. Some necessary test outputs mess up with the following tests. It's recommended to run all single tests in the test case SentryClientInterfaceTest manually one by one.

codekandis/sentry-client 适用场景与选型建议

codekandis/sentry-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 622 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 codekandis/sentry-client 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 622
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-25