Last active
August 29, 2015 14:17
-
-
Save Nimrod007/805f072c0f43f7b448ac to your computer and use it in GitHub Desktop.
Get error rate from new relic and send to LittleBits CloudBit PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('Asia/Jerusalem'); | |
echo "New Relic + Little Bits<br>"; | |
$appId = YOUR-NEWRELIC-APP-ID; | |
$timePicker = 30; //get last 30 minutes error rate from newrelic | |
$relicKey = 'YOUR-RELIC-JEY'; | |
$deviceId = 'YOUR-CLOUD-BIT-DEVICE-ID'; | |
$cloudBitKey = 'YOUR-CLOUD-BIT-KEY'; | |
$errorURL = 'Errors/all&values[]=error_count'; | |
$countURL = 'HttpDispatcher&values[]=call_count'; | |
$otherTransactionsURL = 'OtherTransaction/all&values[]=call_count'; | |
$errorKey = 'error_count'; | |
$countKey = 'call_count'; | |
$valueType = 'percent'; | |
$nowMinus30 = strtotime("-$timePicker minutes"); | |
$startTimeDate = date('Y-m-d', $nowMinus30); | |
$startTimeMinutes = date('h:i:s', $nowMinus30); | |
$startTimeStr = "{$startTimeDate}T{$startTimeMinutes}+02:00"; | |
$now = time(); | |
$endTimeDate = date('Y-m-d', $now); | |
$endTimeMinutes = date('h:i:s', $now); | |
$endTimeStr = "{$endTimeDate}T{$endTimeMinutes}+02:00"; | |
$errorCount = getMetricFromRelic($errorURL, $appId, $errorKey, $startTimeStr, $endTimeStr); | |
$callCount = getMetricFromRelic($countURL, $appId, $countKey, $startTimeStr, $endTimeStr); | |
$otherTransactionsCount = getMetricFromRelic($otherTransactionsURL, $appId, $countKey, $startTimeStr, $endTimeStr); | |
echo "Error Count: $errorCount<br>"; | |
echo "Call Count: $callCount<br>"; | |
$errorRate = 100 * $errorCount / ($callCount + $otherTransactionsCount); | |
echo "The eror rate is : $errorRate<br>"; | |
sendToCloudBit($cloudBitKey, $deviceId, 60000, $valueType, $errorRate); | |
function getMetricFromRelic($url, $appId, $key, $start, $end){ | |
$url = "https://api.newrelic.com/v2/applications/$appId/metrics/data.JSON?names[]=$url&from=$start&to=$end&summarize=true"; | |
$opts = array( | |
'http'=>array( | |
'method'=>"GET", | |
'header'=>"X-Api-Key: $relicKey\r\n" . | |
"Accept: application/json\r\n" | |
) | |
); | |
$context = stream_context_create($opts); | |
$result = file_get_contents($url, false, $context); | |
$json = json_decode($result,true); | |
$count = $json['metric_data']['metrics'][0]['timeslices'][0]['values'][$key]; | |
return $count; | |
} | |
function sendToCloudBit($cloudBitKey, $deviceId, $durationInMillis, $valueType, $value){ | |
$url = "https://api-http.littlebitscloud.cc/v3/devices/$deviceId/output"; | |
$data = array($valueType => $value, 'duration_ms' => $durationInMillis); | |
$options = array( | |
'http' => array( | |
'header' => "Authorization: Bearer $cloudBitKey\r\n", | |
'method' => 'POST', | |
'content' => http_build_query($data), | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
echo "<br>Little Bit said: $result<br>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment