Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto on session tracking #517

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f9d9b38
AOST: Configuration changes
Cawllec Feb 5, 2019
4ec3855
AOST: Client reflection changes and tests
Cawllec Feb 5, 2019
7168505
AOST: Update Client guzzle creation and HTTP calls
Cawllec Feb 5, 2019
488c4fe
AOST: Update Client & HttpClient tests to new interface
Cawllec Feb 5, 2019
3c71e1e
Merge pull request #516 from bugsnag/master
Cawllec Feb 6, 2019
b8b2020
AOST: Update session tracker to check SessionsEnabled, added tests
Cawllec Feb 6, 2019
93b2d4c
Merge branch 'next' into aost/base-changes
Cawllec Feb 6, 2019
b3aa1b0
AOST: Improve tests
Cawllec Feb 6, 2019
45b401a
AOST: Add correct responses to storage calls
Cawllec Feb 6, 2019
fa0bcc8
AOST: Remove deprecated deploy function
Cawllec Feb 8, 2019
1a4f433
AOST: Update UPGRADING guide
Cawllec Feb 8, 2019
bcd2058
AOST: Remove deploy tests
Cawllec Feb 8, 2019
e2b6269
AOST: Add updating examples
Cawllec Feb 8, 2019
8b8b3cc
AOST: Add deploy->build example
Cawllec Feb 8, 2019
bde1c08
AOST: Make upgrading examples language more explicit
Cawllec Feb 11, 2019
ab3cc78
AOST: Close code block in upgrading
Cawllec Feb 11, 2019
7acfea6
AOST: Remove release changes
Cawllec Feb 12, 2019
4e8b739
AOST: Line formatting
Cawllec Feb 12, 2019
6fa5b16
AOST: Replace deploy in HTTPClient
Cawllec Feb 12, 2019
660abaa
AOST: Use correct exception class
Cawllec Feb 12, 2019
bccd728
AOST: Break SessionTracker changes into new PR
Cawllec Feb 12, 2019
9d466c0
AOST: Retain previous API
Cawllec Feb 12, 2019
27143e8
AOST: Return to original shouldCaptureSessions name
Cawllec Feb 12, 2019
00a6a88
AOST: Re-add original session methods
Cawllec Feb 12, 2019
e2898c1
AOST: Don't check sessionsEnabled if autoCaptureSessions is disabled
Cawllec Feb 12, 2019
8f5ab58
AOST: Make uri acquisition compatible with different guzzle versions
Cawllec Feb 13, 2019
c91f17d
AOST: Minor fixes
Cawllec Feb 13, 2019
9a7ca29
AOST: Add tests to cover original interface expectations
Cawllec Feb 13, 2019
d959d3e
AOST: Ensure method calls and docs are correct
Cawllec Feb 13, 2019
19ab537
AOST: Formatting
Cawllec Feb 13, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 89 additions & 27 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
use Bugsnag\Middleware\SessionData;
use Bugsnag\Request\BasicResolver;
use Bugsnag\Request\ResolverInterface;
use Composer\CaBundle\CaBundle;
use GuzzleHttp\Client as Guzzle;
use Bugsnag\Utils;
use GuzzleHttp\ClientInterface;
use ReflectionClass;
use ReflectionException;
Expand All @@ -30,7 +29,7 @@ class Client
*
* @var string
*/
const ENDPOINT = 'https://notify.bugsnag.com';
const ENDPOINT = Configuration::DEFAULT_NOTIFY_ENDPOINT;

/**
* The config instance.
Expand Down Expand Up @@ -88,10 +87,13 @@ class Client
public static function make($apiKey = null, $endpoint = null, $defaults = true)
{
$config = new Configuration($apiKey ?: getenv('BUGSNAG_API_KEY'));
$guzzle = static::makeGuzzle($endpoint ?: getenv('BUGSNAG_ENDPOINT'));

$client = new static($config, null, $guzzle);
if ($endpoint = $endpoint ?: getenv('BUGSNAG_ENDPOINT')) {
$sessionEndpoint = getenv('BUGSNAG_SESSION_ENDPOINT');
$config->setEndpoints($endpoint, $sessionEndpoint);
}

$client = new static($config, null, null);
if ($defaults) {
$client->registerDefaultCallbacks();
}
Expand All @@ -111,10 +113,20 @@ public static function make($apiKey = null, $endpoint = null, $defaults = true)
public function __construct(Configuration $config, ResolverInterface $resolver = null, ClientInterface $guzzle = null)
{
$this->config = $config;
if ($guzzle) {
$this->config->setGuzzleClient($guzzle);
if (version_compare(ClientInterface::VERSION, '6') === 1) {
$endpoint = $guzzle->getConfig('base_uri');
} else {
$endpoint = $guzzle->getBaseUrl();
}
$this->config->setEndpoints((string) $endpoint, null);
}

$this->resolver = $resolver ?: new BasicResolver();
$this->recorder = new Recorder();
$this->pipeline = new Pipeline();
$this->http = new HttpClient($config, $guzzle ?: static::makeGuzzle());
$this->http = new HttpClient($config);
$this->sessionTracker = new SessionTracker($config);

$this->registerMiddleware(new NotificationSkipper($config));
Expand All @@ -135,28 +147,8 @@ public function __construct(Configuration $config, ResolverInterface $resolver =
public static function makeGuzzle($base = null, array $options = [])
{
$key = version_compare(ClientInterface::VERSION, '6') === 1 ? 'base_uri' : 'base_url';

$options[$key] = $base ?: static::ENDPOINT;

if ($path = static::getCaBundlePath()) {
$options['verify'] = $path;
}

return new Guzzle($options);
}

/**
* Get the ca bundle path if one exists.
*
* @return string|false
*/
protected static function getCaBundlePath()
{
if (!class_exists(CaBundle::class)) {
return false;
}

return realpath(CaBundle::getSystemCaRootBundlePath());
return Utils::makeGuzzle($options);
}

/**
Expand Down Expand Up @@ -784,6 +776,41 @@ public function setAutoCaptureSessions($track)
return $this;
}

/**
* Sets the notify and session endpoints
*
* @param string $notifyEndpoint the notify endpoint
* @param string $sessionEndpoint the session endpoint
*
* @return $this
*/
public function setEndpoints($notifyEndpoint, $sessionEndpoint)
{
$this->config->setEndpoints($notifyEndpoint, $sessionEndpoint);

return $this;
}

/**
* Gets the notify endpoint
*
* @return string
*/
public function getNotifyEndpoint()
{
return $this->config->getNotifyEndpoint();
}

/**
* Gets the session endpoint
*
* @return string
*/
public function getSessionEndpoint()
{
return $this->config->getSessionEndpoint();
}

/**
* Set session delivery endpoint.
*
Expand All @@ -808,6 +835,41 @@ public function getSessionClient()
return $this->config->getSessionClient();
}

/**
* Gets the current guzzle client.
*
* @return GuzzleHttp\ClientInterface
*/
public function getGuzzleClient()
{
return $this->config->getGuzzleClient();
}

/**
* Sets the guzzle client
*
* Delivery URLs should be set using the "setEndpoints" method
*
* @param GuzzleHttp\ClientInterface $guzzleClient the new guzzle client
*
* @return $this
*/
public function setGuzzleClient($guzzleClient)
{
$this->config->setGuzzleClient($guzzleClient);
return $this;
}

/**
* Whether any sessions are enabled.
*
* @return bool
*/
public function sessionsEnabled()
{
return $this->config->sessionsEnabled();
}

/**
* Whether should be auto-capturing sessions.
*
Expand Down
145 changes: 124 additions & 21 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@
namespace Bugsnag;

use InvalidArgumentException;
use Bugsnag\Utils;

class Configuration
{
/**
* The default endpoint.
* The default notification endpoint.
*
* @var string
*/
const SESSION_ENDPOINT = 'https://sessions.bugsnag.com';
const DEFAULT_NOTIFY_ENDPOINT = 'https://notify.bugsnag.com';

/**
* The default session endpoint.
*
* @var string
*/
const DEFAULT_SESSION_ENDPOINT = 'https://sessions.bugsnag.com';

/**
* The default build endpoint.
*
* @var string
*/
const BUILD_ENDPOINT = 'https://build.bugsnag.com';
const DEFAULT_BUILD_ENDPOINT = 'https://build.bugsnag.com';

/**
* The Bugsnag API Key.
Expand Down Expand Up @@ -116,25 +124,39 @@ class Configuration
protected $errorReportingLevel;

/**
* Whether to track sessions.
* Whether to track sessions automatically.
*
* @var bool
*/
protected $autoCaptureSessions = true;

/**
* Whether to allow any sessions to be tracked.
*
* @var bool
*/
protected $autoCaptureSessions = false;
protected $enableSessions = true;

/**
* A client to use to send sessions.
* The client for making http requests
*
* @var \Guzzle\ClientInterface
* @var GuzzleHttp\ClientInterface|null
*/
protected $sessionClient;
protected $guzzleClient = null;

/**
* The endpoint to deliver notifications to.
*
* @var string
*/
protected $notifyEndpoint = self::DEFAULT_NOTIFY_ENDPOINT;

/**
* The endpoint to deliver sessions to.
*
* @var string
*/
protected $sessionEndpoint = self::SESSION_ENDPOINT;
protected $sessionEndpoint = self::DEFAULT_SESSION_ENDPOINT;

/**
* The endpoint to deliver build notifications to.
Expand Down Expand Up @@ -578,6 +600,75 @@ public function setAutoCaptureSessions($track)
return $this;
}

/**
* Whether any sessions are enabled.
*
* @return bool
*/
public function sessionsEnabled()
{
if ($this->enableSessions) {
$notifyEndpointSet = $this->notifyEndpoint !== static::DEFAULT_NOTIFY_ENDPOINT;
$sessionEndpointSet = $this->sessionEndpoint !== static::DEFAULT_SESSION_ENDPOINT;
if ($notifyEndpointSet && !$sessionEndpointSet) {
syslog(LOG_WARNING, 'The session endpoint has not been set, all further session capturing will be disabled');
$this->enableSessions = false;
} elseif (!$notifyEndpointSet && $sessionEndpointSet) {
throw new InvalidArgumentException('The session endpoint cannot be modified without the notify endpoint');
}
}
return $this->enableSessions;
}

/**
* Whether should be auto-capturing sessions.
*
* @return bool
*/
public function shouldCaptureSessions()
{
return $this->autoCaptureSessions && $this->sessionsEnabled();
}

/**
* Sets the notify and session endpoints
*
* @param string $notifyEndpoint the notify endpoint
* @param string $sessionEndpoint the session endpoint
*
* @return $this
*/
public function setEndpoints($notifyEndpoint, $sessionEndpoint)
{
if ($notifyEndpoint) {
$this->notifyEndpoint = $notifyEndpoint;
}
if ($sessionEndpoint) {
$this->sessionEndpoint = $sessionEndpoint;
}
return $this;
}

/**
* Gets the notify endpoint
*
* @return string
*/
public function getNotifyEndpoint()
{
return $this->notifyEndpoint;
}

/**
* Gets the session endpoint
*
* @return string
*/
public function getSessionEndpoint()
{
return $this->sessionEndpoint;
}

/**
* Set session delivery endpoint.
*
Expand All @@ -587,9 +678,7 @@ public function setAutoCaptureSessions($track)
*/
public function setSessionEndpoint($endpoint)
{
$this->sessionEndpoint = $endpoint;

$this->sessionClient = Client::makeGuzzle($this->sessionEndpoint);
$this->setEndpoints(null, $endpoint);

return $this;
}
Expand All @@ -601,21 +690,35 @@ public function setSessionEndpoint($endpoint)
*/
public function getSessionClient()
{
if (is_null($this->sessionClient)) {
$this->sessionClient = Client::makeGuzzle($this->sessionEndpoint);
}
return $this->getGuzzleClient();
}

return $this->sessionClient;
/**
* Sets the guzzle client
*
* Delivery URLs should be set using the "setEndpoints" method
*
* @param GuzzleHttp\ClientInterface $guzzleClient the new guzzle client
*
* @return $this
*/
public function setGuzzleClient($guzzleClient)
{
$this->guzzleClient = $guzzleClient;
return $this;
}

/**
* Whether should be auto-capturing sessions.
* Gets the current guzzle client, making one if necessary
*
* @return bool
* @return GuzzleHttp\ClientInterface
*/
public function shouldCaptureSessions()
public function getGuzzleClient()
{
return $this->autoCaptureSessions;
if (!$this->guzzleClient) {
$this->guzzleClient = Utils::makeGuzzle();
}
return $this->guzzleClient;
}

/**
Expand Down Expand Up @@ -643,6 +746,6 @@ public function getBuildEndpoint()
return $this->buildEndpoint;
}

return self::BUILD_ENDPOINT;
return self::DEFAULT_BUILD_ENDPOINT;
}
}
Loading