Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
*.phar
.idea
*.sh
*~
ci
composer.lock
Expand All @@ -23,3 +22,6 @@ test.php

# phpenv
.php-version

# Sonarqube
.scannerwork
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
language: php

git:
depth: false

env:
global:
- COMPOSER_DISABLE_XDEBUG_WARN=1

matrix:
include:
- dist: precise
php: '5.3'
php: '5.4'
- dist: xenial
php: '7.2'
after_success:
- bash sonar-scanner.sh

services:
- redis-server
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
6.2.3 (Nov 1, 2019)
- Added flag `IPAddressesEnabled` into options to enable/disable sending MachineName and MachineIP when data is posted in headers.

6.2.2 (Sep 18, 2019)
- Fetch multiple splits at once on getTreatments/getTreatmentsWithConfig
- Removed MatcherClient (DependencyMatcher now uses Evaluator directly)
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
"require-dev": {
"phpunit/phpunit": "~4.0",
"phpdocumentor/phpdocumentor": "2.*",
"squizlabs/php_codesniffer": "2.*"
"squizlabs/php_codesniffer": "2.*",
"rogervila/php-sonarqube-scanner": "0.4.0"
},

"autoload": {
"psr-4": {
"SplitIO\\": "src/SplitIO/",
Expand Down
41 changes: 41 additions & 0 deletions sonar-scanner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#/bin/bash -e

sonar_scanner() {
local params="$@"

vendor/bin/sonar-scanner \
-Dsonar.host.url='https://sonarqube.split-internal.com' \
-Dsonar.login="$SONAR_TOKEN" \
-Dsonar.ws.timeout='300' \
-Dsonar.sources='./src' \
-Dsonar.projectName='php-client' \
-Dsonar.exclusions='**/tests/**/*.*' \
-Dsonar.links.ci='https://travis-ci.com/splitio/php-client' \
-Dsonar.links.scm='https://github.com/splitio/php-client' \
"${params}"

return $?
}

if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
sonar_scanner \
-Dsonar.pullrequest.provider='GitHub' \
-Dsonar.pullrequest.github.repository='splitio/php-client' \
-Dsonar.pullrequest.key=$TRAVIS_PULL_REQUEST \
-Dsonar.pullrequest.branch=$TRAVIS_PULL_REQUEST_BRANCH \
-Dsonar.pullrequest.base=$TRAVIS_BRANCH
else
if [ "$TRAVIS_BRANCH" == 'master' ]; then
sonar_scanner \
-Dsonar.branch.name=$TRAVIS_BRANCH
else
if [ "$TRAVIS_BRANCH" == 'develop' ]; then
TARGET_BRANCH='master'
else
TARGET_BRANCH='develop'
fi
sonar_scanner \
-Dsonar.branch.name=$TRAVIS_BRANCH \
-Dsonar.branch.target=$TARGET_BRANCH
fi
fi
9 changes: 3 additions & 6 deletions src/SplitIO/Component/Cache/ImpressionCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@

use SplitIO\Component\Common\Di;
use SplitIO\Component\Cache\KeyFactory;
use SplitIO\Sdk\QueueMetadataMessage;

class ImpressionCache
{
const IMPRESSIONS_QUEUE_KEY = "SPLITIO.impressions";
const IMPRESSION_KEY_DEFAULT_TTL = 3600;

public function logImpressions($impressions, $metadata)
public function logImpressions($impressions, QueueMetadataMessage $metadata)
{
$toStore = array_map(
function ($imp) use ($metadata) {
return json_encode(array(
"m" => array(
"s" => $metadata['sdkVersion'],
"i" => $metadata['machineIp'],
"n" => $metadata['machineName'],
),
'm' => $metadata->toArray(),
"i" => array(
"k" => $imp->getId(),
"b" => $imp->getBucketingKey(),
Expand Down
10 changes: 6 additions & 4 deletions src/SplitIO/Sdk/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use SplitIO\Component\Cache\MetricsCache;
use SplitIO\Sdk\Events\EventDTO;
use SplitIO\Sdk\Events\EventQueueMessage;
use SplitIO\Sdk\Events\EventQueueMetadataMessage;
use SplitIO\Sdk\QueueMetadataMessage;
use SplitIO\Sdk\Impressions\Impression;
use SplitIO\TreatmentImpression;
use SplitIO\Sdk\Impressions\ImpressionLabel;
Expand Down Expand Up @@ -38,6 +38,9 @@ public function __construct($options = array())
if (isset($options['impressionListener'])) {
$this->impressionListener = new \SplitIO\Sdk\ImpressionListenerWrapper($options['impressionListener']);
}
$this->queueMetadata = new QueueMetadataMessage(
isset($options['IPAddressesEnabled']) ? $options['IPAddressesEnabled'] : true
);
}

/**
Expand Down Expand Up @@ -305,7 +308,7 @@ private function doInputValidationForTreatments($key, $featureNames, array $attr
private function registerData($impressions, $attributes, $metricName, $latency = null)
{
try {
TreatmentImpression::log($impressions);
TreatmentImpression::log($impressions, $this->queueMetadata);
if (isset($this->impressionListener)) {
$this->impressionListener->sendDataToClient($impressions, $attributes);
}
Expand Down Expand Up @@ -541,8 +544,7 @@ public function track($key, $trafficType, $eventType, $value = null, $properties

try {
$eventDTO = new EventDTO($key, $trafficType, $eventType, $value, $properties);
$eventMessageMetadata = new EventQueueMetadataMessage();
$eventQueueMessage = new EventQueueMessage($eventMessageMetadata, $eventDTO);
$eventQueueMessage = new EventQueueMessage($this->queueMetadata, $eventDTO);
return EventsCache::addEvent($eventQueueMessage);
} catch (\Exception $exception) {
// @codeCoverageIgnoreStart
Expand Down
8 changes: 5 additions & 3 deletions src/SplitIO/Sdk/Events/EventQueueMessage.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace SplitIO\Sdk\Events;

use SplitIO\Sdk\QueueMetadataMessage;

class EventQueueMessage
{
/**
Expand All @@ -18,15 +20,15 @@ class EventQueueMessage
* @param $metadata
* @param $event
*/
public function __construct(EventQueueMetadataMessage $metadata, EventDTO $event)
public function __construct(QueueMetadataMessage $metadata, EventDTO $event)
{
$this->metadata = $metadata;
$this->event = $event;
}


/**
* @return EventQueueMetadataMessage
* @return QueueMetadataMessage
*/
public function getMetadata()
{
Expand All @@ -36,7 +38,7 @@ public function getMetadata()
/**
* @param mixed $metadata
*/
public function setMetadata(EventQueueMetadataMessage $metadata)
public function setMetadata(QueueMetadataMessage $metadata)
{
$this->metadata = $metadata;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace SplitIO\Sdk\Events;
namespace SplitIO\Sdk;

class EventQueueMetadataMessage
class QueueMetadataMessage
{
private $sdkVersion;

Expand All @@ -10,16 +10,23 @@ class EventQueueMetadataMessage
private $machineName;

/**
* EventQueueMetadataMessage constructor.
* QueueMetadataMessage constructor.
*/
public function __construct()
public function __construct($IPAddressesEnabled = true)
{
$this->sdkVersion = 'php-' . \SplitIO\version();
$this->machineIP = \SplitIO\getHostIpAddress();
$this->machineName = 'unknown';
$this->machineIP = 'NA';
$this->machineName = 'NA';
if ($IPAddressesEnabled) {
$this->machineIP = \SplitIO\getHostIpAddress();
if ($this->machineIP != 'unknown') {
$this->machineName = 'ip-' . str_replace('.', '-', $this->machineIP);
} else {
$this->machineName = 'unknown';
}
}
}


/**
* @return mixed
*/
Expand Down
11 changes: 4 additions & 7 deletions src/SplitIO/TreatmentImpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,26 @@
use SplitIO\Component\Cache\ImpressionCache;
use SplitIO\Sdk\Impressions\Impression;
use SplitIO\Component\Common\Di;
use SplitIO\Sdk\QueueMetadataMessage;

class TreatmentImpression
{
/**
* @param \SplitIO\Sdk\Impressions\Impression $impressions
* @return bool
*/
public static function log($impressions)
public static function log($impressions, QueueMetadataMessage $metadata)
{
try {
Di::getLogger()->debug($impressions);
if (is_null($impressions) || (is_array($impressions) && 0 == count($impressions))) {
return;
return null;
}
$impressionCache = new ImpressionCache();
$toStore = (is_array($impressions)) ? $impressions : array($impressions);
return $impressionCache->logImpressions(
$toStore,
array(
'sdkVersion' => 'php-' . \SplitIO\version(),
'machineIp' => \SplitIO\getHostIpAddress(),
'machineName' => null, // TODO
)
$metadata
);
} catch (\Exception $e) {
Di::getLogger()->warning('Unable to write impression back to redis.');
Expand Down
2 changes: 1 addition & 1 deletion src/SplitIO/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Version
{
const CURRENT = '6.2.2';
const CURRENT = '6.2.3';
}
27 changes: 23 additions & 4 deletions tests/Suite/Sdk/ImpressionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SplitIO\Sdk\Impressions\Impression;
use SplitIO\Test\Suite\Redis\ReflectiveTools;
use SplitIO\Component\Cache\ImpressionCache;
use SplitIO\Sdk\QueueMetadataMessage;

class ImpressionsTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -26,6 +27,7 @@ public function testImpressionsAreAdded()
$redisClient = ReflectiveTools::clientFromCachePool(Di::getCache());

$redisClient->del(ImpressionCache::IMPRESSIONS_QUEUE_KEY);
$queueMetadata = new QueueMetadataMessage();

TreatmentImpression::log(new Impression(
'someMatchingKey',
Expand All @@ -35,7 +37,7 @@ public function testImpressionsAreAdded()
123456,
321654,
'someBucketingKey'
));
), $queueMetadata);

// Assert that the TTL is within a 10-second range (between it was set and retrieved).
$ttl = $redisClient->ttl(ImpressionCache::IMPRESSIONS_QUEUE_KEY);
Expand All @@ -47,6 +49,7 @@ public function testImpressionsAreAdded()

$this->assertEquals($decoded['m']['s'], 'php-'.\Splitio\version());
$this->assertEquals($decoded['m']['i'], 'unknown');
$this->assertEquals($decoded['m']['n'], 'unknown');
$this->assertEquals($decoded['i']['k'], 'someMatchingKey');
$this->assertEquals($decoded['i']['b'], 'someBucketingKey');
$this->assertEquals($decoded['i']['f'], 'someFeature');
Expand All @@ -64,14 +67,16 @@ public function testExpirationOnlyOccursOnce()

$sdkConfig = array(
'log' => array('adapter' => 'stdout'),
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options)
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options),
'IPAddressEnabled' => false
);

//Initializing the SDK instance.
\SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);

$redisClient = ReflectiveTools::clientFromCachePool(Di::getCache());
$redisClient->del(ImpressionCache::IMPRESSIONS_QUEUE_KEY);
$queueMetadata = new QueueMetadataMessage(false);

TreatmentImpression::log(new Impression(
'someMatchingKey',
Expand All @@ -81,7 +86,7 @@ public function testExpirationOnlyOccursOnce()
123456,
321654,
'someBucketingKey'
));
), $queueMetadata);

sleep(3);

Expand All @@ -93,11 +98,25 @@ public function testExpirationOnlyOccursOnce()
123456,
321654,
'someBucketingKey'
));
), $queueMetadata);

$ttl = $redisClient->ttl(ImpressionCache::IMPRESSIONS_QUEUE_KEY);
// $ttl should be lower than or equalt the default impressions TTL minus 3 seconds,
// since it should have not been resetted with the last imrpession logged.
$this->assertLessThanOrEqual(ImpressionCache::IMPRESSION_KEY_DEFAULT_TTL - 3, $ttl);

$imp = $redisClient->rpop(ImpressionCache::IMPRESSIONS_QUEUE_KEY);
$decoded = json_decode($imp, true);

$this->assertEquals($decoded['m']['s'], 'php-'.\Splitio\version());
$this->assertEquals($decoded['m']['i'], 'NA');
$this->assertEquals($decoded['m']['n'], 'NA');
$this->assertEquals($decoded['i']['k'], 'someMatchingKey');
$this->assertEquals($decoded['i']['b'], 'someBucketingKey');
$this->assertEquals($decoded['i']['f'], 'someFeature');
$this->assertEquals($decoded['i']['t'], 'on');
$this->assertEquals($decoded['i']['r'], 'label1');
$this->assertEquals($decoded['i']['m'], 123456);
$this->assertEquals($decoded['i']['c'], 321654);
}
}
Loading