Skip to content

Commit

Permalink
added sending profile to api
Browse files Browse the repository at this point in the history
  • Loading branch information
shagtv committed Mar 20, 2019
1 parent d15026b commit df7abad
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

There are next changes:

## 1.3.0

There are next changes:

- added sending profile to api

## 1.2.0

There are next changes:
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ What functions regressed most in the last day/week/month?
What is the historical trend for execution time of a page/function? and so on.

[![Build Status](https://travis-ci.org/badoo/liveprof.svg?branch=master)](https://travis-ci.org/badoo/liveprof)
[![GitHub release](https://img.shields.io/github/release/badoo/liveprof.svg)](/~https://github.com/badoo/liveprof/releases/latest)
[![codecov](https://codecov.io/gh/badoo/liveprof/branch/master/graph/badge.svg)](https://codecov.io/gh/badoo/liveprof)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/badoo/liveprof/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/badoo/liveprof/?branch=master)
[![GitHub license](https://img.shields.io/github/license/badoo/liveprof.svg)](/~https://github.com/badoo/liveprof/blob/master/LICENSE)
Expand Down Expand Up @@ -71,9 +70,10 @@ There is a full list of methods you can use to change options:

// Start profiling
\Badoo\LiveProfiler\LiveProfiler::getInstance()
->setMode(\Badoo\LiveProfiler\LiveProfiler::MODE_DB) // optional, MODE_DB - save profiles to db, MODE_FILES - save profiles to files
->setMode(\Badoo\LiveProfiler\LiveProfiler::MODE_DB) // optional, MODE_DB - save profiles to db, MODE_FILES - save profiles to files, MODE_API - send profiles to http://liveprof.org/
->setConnectionString('mysql://db_user:db_password@db_mysql:3306/Profiler?charset=utf8') // optional, you can also set the connection url in the environment variable LIVE_PROFILER_CONNECTION_URL
->setPath('/app/data/') // optional, path to save profiles, you can also set the file path in the environment variable LIVE_PROFILER_PATH
->setApiKey('api_key') // optional, api key to send profiles and see demo, you can get it on http://liveprof.org/
->setApp('Site1') // optional, current app name to use one profiler in several apps, "Default" by default
->setLabel('users') // optional, the request name, by default the url path or script name in cli
->setDivider(700) // optional, profiling starts for 1 of 700 requests with the same app and label, 1000 by default
Expand Down Expand Up @@ -109,7 +109,10 @@ Environment Variables
=====================

`LIVE_PROFILER_CONNECTION_URL`: [url](https://www.doctrine-project.org/projects/doctrine-dbal/en/2.8/reference/configuration.html#configuration) for the database connection
`LIVE_PROFILER_PATH`: path to save profiles is \Badoo\LiveProfiler\LiveProfiler::MODE_FILES mode

`LIVE_PROFILER_PATH`: path to save profiles in \Badoo\LiveProfiler\LiveProfiler::MODE_FILES mode

`LIVE_PROFILER_API_URL`: api url to send profiles in \Badoo\LiveProfiler\LiveProfiler::MODE_API mode and see demo on [liveprof.org](http://liveprof.org/)

Work flow
=========
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"doctrine/dbal": "<2.9",
"psr/log": "~1.0",
"ext-json": "*",
"ext-zlib": "*"
"ext-zlib": "*",
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.0|~5.0"
Expand Down
56 changes: 56 additions & 0 deletions src/Badoo/LiveProfiler/LiveProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ class LiveProfiler
{
CONST MODE_DB = 'db';
CONST MODE_FILES = 'files';
CONST MODE_API = 'api';

/** @var LiveProfiler */
protected static $instance;
/** @var string */
protected $mode = self::MODE_DB;
/** @var string */
protected $path = '';
/** @var string */
protected $api_key = '';
/** @var string */
protected $url = 'http://liveprof.org/api';
/** @var Connection */
protected $Conn;
/** @var LoggerInterface */
Expand Down Expand Up @@ -67,6 +72,12 @@ public function __construct($connection_string_or_path = '', $mode = self::MODE_

if ($mode === self::MODE_DB) {
$this->connection_string = $connection_string_or_path ?: getenv('LIVE_PROFILER_CONNECTION_URL');
} elseif ($mode === self::MODE_API) {
if ($connection_string_or_path) {
$this->url = $connection_string_or_path;
} elseif (getenv('LIVE_PROFILER_API_URL')) {
$this->url = getenv('LIVE_PROFILER_API_URL');
}
} else {
$this->setPath($connection_string_or_path ?: getenv('LIVE_PROFILER_PATH'));
}
Expand Down Expand Up @@ -329,6 +340,24 @@ public function getPath()
return $this->path;
}

/**
* @param string $api_key
* @return $this
*/
public function setApiKey($api_key)
{
$this->api_key = $api_key;
return $this;
}

/**
* @return string
*/
public function getApiKey()
{
return $this->api_key;
}

/**
* @param string $app
* @return $this
Expand Down Expand Up @@ -499,9 +528,36 @@ protected function save($app, $label, $datetime, $data)
return $this->saveToDB($app, $label, $datetime, $data);
}

if ($this->mode === self::MODE_API) {
return $this->sendToAPI($app, $label, $datetime, $data);
}

return $this->saveToFile($app, $label, $datetime, $data);
}

/**
* @param string $app
* @param string $label
* @param string $datetime
* @param array $data
* @return bool
*/
protected function sendToAPI($app, $label, $datetime, $data)
{
$data = $this->DataPacker->pack($data);
$api_key = $this->api_key;
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$this->url);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query(compact('api_key', 'app', 'label', 'datetime', 'data')));
curl_exec($curl_handle);
$http_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);

return $http_code === 200;
}

/**
* @param string $app
* @param string $label
Expand Down
24 changes: 22 additions & 2 deletions tests/unit/Badoo/LiveProfiler/LiveProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public function testSettersGetters()
$test_total_divider = 2;
$test_connection_string = 'test_connection_string';
$test_path = 'test_path';
$test_api_key = 'test_api_key';

$Profiler = new \Badoo\LiveProfiler\LiveProfiler('sqlite:///:memory:');

Expand All @@ -305,7 +306,8 @@ public function testSettersGetters()
->setDataPacker($DataPacker)
->setConnection($ConnectionMock)
->setConnectionString($test_connection_string)
->setPath($test_path);
->setPath($test_path)
->setApiKey($test_api_key);

$mode = $this->getProtectedProperty($Profiler, 'mode');
$app = $this->getProtectedProperty($Profiler, 'app');
Expand All @@ -318,6 +320,7 @@ public function testSettersGetters()
$Connection = $this->getProtectedProperty($Profiler, 'Conn');
$connection_string = $this->getProtectedProperty($Profiler, 'connection_string');
$path = $this->getProtectedProperty($Profiler, 'path');
$api_key = $this->getProtectedProperty($Profiler, 'api_key');

self::assertEquals($test_mode, $mode);
self::assertEquals($test_mode, $Profiler->getMode());
Expand All @@ -336,6 +339,8 @@ public function testSettersGetters()
self::assertSame($test_connection_string, $connection_string);
self::assertSame($test_path, $path);
self::assertSame($test_path, $Profiler->getPath());
self::assertSame($test_api_key, $api_key);
self::assertSame($test_api_key, $Profiler->getApiKey());
}

public function testGetInstance()
Expand Down Expand Up @@ -400,7 +405,22 @@ public function testSaveToFile()
\Badoo\LiveProfiler\LiveProfiler::MODE_FILES
);

$result = $this->invokeMethod($Profiler, 'saveToFile', ['app', 'label', '1970-01-01', ['data']]);
$result = $this->invokeMethod($Profiler, 'save', ['app', 'label', '1970-01-01', ['data']]);
self::assertTrue($result);
}

/**
* @throws \ReflectionException
*/
public function testSendToAPI()
{
$Profiler = new \Badoo\LiveProfiler\LiveProfiler(
'',
\Badoo\LiveProfiler\LiveProfiler::MODE_API
);
$Profiler->setApiKey('70366397-97d6-41be-a83c-e9e649c824e1');

$result = $this->invokeMethod($Profiler, 'save', ['app', 'label', '1970-01-01', ['data']]);
self::assertTrue($result);
}

Expand Down

0 comments on commit df7abad

Please sign in to comment.