Comments (3)
Ok, after looking at the new_relic_apdex_t example. I came up with this:
<?php
// No need to log this script operation in New Relic's stats.
// PROTIP: you might also want to use this snippet if you have PHP code handling
// very fast things like redirects or the like.
if (extension_loaded('newrelic')) {
newrelic_ignore_transaction();
}
$app_info = get_app_info( PANTHEON_ENVIRONMENT );
// Fail fast if we're not going to be able to call New Relic.
if ($app_info == FALSE) {
echo "\n\nALERT! No New Relic metadata could be found.\n\n";
exit();
}
// This is one example that handles code pushes, dashboard
// commits, and deploys between environments. To make sure we
// have good deploy markers, we gather data differently depending
// on the context.
if ($_POST['wf_type'] == 'sync_code') {
// commit 'subject'
$description = trim(`git log --pretty=format:"%s" -1`);
$revision = trim(`git log --pretty=format:"%h" -1`);
if ($_POST['user_role'] == 'super') {
// This indicates an in-dashboard SFTP commit.
$user = trim(`git log --pretty=format:"%ae" -1`);
$changelog = trim(`git log --pretty=format:"%b" -1`);
$changelog .= "\n\n" . '(Commit made via Pantheon dashbaord.)';
}
else {
$user = $_POST['user_email'];
$changelog = trim(`git log --pretty=format:"%b" -1`);
$changelog .= "\n\n" . '(Triggered by remote git push.)';
}
}
elseif ($_POST['wf_type'] == 'deploy') {
// Topline description:
$description = 'Deploy to environment triggered via Pantheon';
// Find out if there's a deploy tag:
$revision = `git describe --tags`;
// Get the annotation:
$changelog = `git tag -l -n99 $deploy_tag`;
$user = $_POST['user_email'];
}
// Use New Relic's v2 curl command-line example.
$structure = [
"deployment" => [
"revision" => $revision,
"changelog" => $changelog,
"description" => $description,
"user" => $user
]
];
$curl = "curl -X POST https://api.newrelic.com/v2/applications/" . $app_info['id'] . "/deployments.json ";
$curl .= " -H 'X-Api-Key:" . $app_info['api_key'] . "' -i ";
$curl .= " -H 'Content-Type: application/json' ";
$curl .= " -d '" . json_encode($structure) . "' ";
// The below can be helpful debugging.
echo "\n\nCURLing... \n\n$curl\n\n";
echo "Logging deployment in New Relic...\n";
passthru($curl);
echo "Done!";
/**
* Gets the New Relic API Key so that further requests can be made.
*
* Also gets New Relic's name for the given environment.
*/
function get_nr_connection_info( $env = 'dev' ) {
$output = array();
$req = pantheon_curl( 'https://api.live.getpantheon.com/sites/self/bindings?type=newrelic', null, 8443 );
$meta = json_decode( $req['body'], true );
foreach ( $meta as $data ) {
if ( $data['environment'] === $env ) {
if ( empty( $data['api_key'] ) ) {
echo "Failed to get API Key\n";
return;
}
$output['api_key'] = $data['api_key'];
if ( empty( $data['app_name'] ) ) {
echo "Failed to get app name\n";
return;
}
$output['app_name'] = $data['app_name'];
}
}
return $output;
}
/**
* Get the id of the current multidev environment.
*/
function get_app_id( $api_key, $app_name ) {
$return = '';
$s = curl_init();
curl_setopt( $s, CURLOPT_URL, 'https://api.newrelic.com/v2/applications.json' );
curl_setopt( $s, CURLOPT_HTTPHEADER, array( 'X-API-KEY:' . $api_key ) );
curl_setopt( $s, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec( $s );
curl_close( $s );
$result = json_decode( $result, true );
foreach ( $result['applications'] as $application ) {
if ( $application['name'] === $app_name ) {
$return = $application['id'];
break;
}
}
return $return;
}
/**
* Get New Relic information about a given environment.
*/
function get_app_info( $env = 'dev' ) {
$nr_connection_info = get_nr_connection_info($env);
if ( empty( $nr_connection_info ) ) {
echo "Unable to get New Relic connection info\n";
return;
}
$api_key = $nr_connection_info['api_key'];
$app_name = $nr_connection_info['app_name'];
$app_id = get_app_id( $api_key, $app_name );
$url = "https://api.newrelic.com/v2/applications/$app_id.json";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$headers = [
'X-API-KEY:' . $api_key
];
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
$response = curl_exec( $ch );
if ( curl_errno( $ch ) ) {
echo 'Error:' . curl_error( $ch );
}
curl_close( $ch );
$output = json_decode( $response, true );
$output['application']['api_key'] = $api_key;
return $output['application'];
}
from quicksilver-examples.
FYI, With this I've had some errors with $changelog being too long when using compose and circleci setup
from quicksilver-examples.
Tested this on a few sites, works for me. Also this returns an error if your API key is invalid, which our V1 code does not do, bonus. :)
from quicksilver-examples.
Related Issues (20)
- Can sync_code detect a git tag creation? HOT 1
- URL Checker value always 1 HOT 1
- Slack Notifications don't work with multidev HOT 2
- undefined variable: deploy tag in new_relic_deploy.php script HOT 2
- Example of Programmatic Backup?
- Example wanted: Post Solr schema after creating new multidev env with Drupal HOT 5
- [docs] Timeout 60 seconds
- Reducing secrets.json credential overlap or confusion
- Add curl timeout to New Relic API request, to prevent hung php worker / failed deploy workflow
- Edit readme to make it clearer
- Remove SpotBot VRT script
- False positive quicksilver status on passthru HOT 10
- Is it possible to setup database version ? HOT 2
- diffy.website integration is unnecessarily tedious HOT 1
- autopilot_vrt example
- Slack integration - correct script location
- Script path and 404 while running your Quicksilver operation HOT 2
- Copying example.pantheon.yml file may cause some error
- GitHub Action from GitHub App example
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quicksilver-examples.