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

Slack Notify with Drush Site Audit on Deploy #77

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
88a512c
'Title' uses better formatting than pretext in Slack Attachments. Emo…
jordan8037310 May 13, 2016
618acfd
Slack Attachments Title should really be set per-case
jordan8037310 May 13, 2016
3f2b0ba
Drush Site Audit on deploy.
jordan8037310 May 13, 2016
c9ad888
Align with New Relic Pro product release
Jun 27, 2016
7b1a852
Merge pull request #81 from pantheon-systems/new-relic-pro
nataliejeremy Jun 27, 2016
e970d2f
Update to handle commit/push use-case
Jun 28, 2016
abd0748
Add new hooks to Slack example README
ari-gold Aug 11, 2016
376969b
Merge pull request #85 from pantheon-systems/slack-new-hooks
greg-1-anderson Aug 15, 2016
9c5b0c4
Add new features to example.pantheon.yml
ari-gold Aug 10, 2016
6c1875e
Merge pull request #84 from pantheon-systems/advanced-site-configurat…
ari-gold Aug 16, 2016
2f36d22
Add Solr Power indexing on multidev creation example
ataylorme Aug 18, 2016
114e704
Adding new_relic_apdex_t
stevector Aug 19, 2016
32f89df
Whitespace fix
stevector Aug 19, 2016
a68b608
Fetch app name from Pantheon; Pull apdex_t from existing New Relic se…
ataylorme Aug 22, 2016
1abc75d
Update README as hard-coded values for T score or no longer needed
ataylorme Aug 22, 2016
2c7a28e
Update new_relic_apdex_t.php
stevector Aug 23, 2016
2dcaad0
Bug fix in new_relic_apdex_t
stevector Aug 23, 2016
7aaffef
Repost schema before indexing
ataylorme Aug 30, 2016
020ce14
Clarify WordPress for this example
ataylorme Aug 30, 2016
18f30bf
Remove duplicate directory
ataylorme Aug 30, 2016
6386be0
Add WordPress Solr Power indexing on multidev creation example (#87)
ari-gold Aug 30, 2016
9706b9a
Update slack_notification.php
lauren-kelly Sep 7, 2016
de43362
Merge pull request #91 from pantheon-systems/lauren-kelly-patch-1
Sep 8, 2016
b062424
Add drush_version to example.pantheon.yml
ari-gold Sep 16, 2016
a3a09cb
Merge pull request #93 from pantheon-systems/drush_version
populist Sep 19, 2016
c17051c
Update README.md
Sep 29, 2016
a3700b3
Add newrelic_ignore_transaction()
Sep 29, 2016
c300579
Merge pull request #89 from pantheon-systems/86-set-apdex
ataylorme Oct 7, 2016
c98bf9e
Fix spelling of "snippet" in new_relic_deploy
rbayliss Oct 18, 2016
031308a
Merge pull request #95 from rbayliss/patch-1
ari-gold Oct 18, 2016
4a86852
fixing a minor typo in description
allenfear Oct 23, 2016
a56b234
Merge pull request #96 from pantheon-systems/fix-typo
ari-gold Oct 27, 2016
27a3251
Improve message format so default
Nov 16, 2016
6bb4a3b
'Title' uses better formatting than pretext in Slack Attachments. Emo…
jordan8037310 May 13, 2016
0a4cf3d
Slack Attachments Title should really be set per-case
jordan8037310 May 13, 2016
3592e4d
Drush Site Audit on deploy.
jordan8037310 May 13, 2016
44a6ffb
Merge branch 'site-audit-on-deploy' of github.com:jordan8037310/quick…
jordan8037310 Nov 24, 2016
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
59 changes: 47 additions & 12 deletions example.pantheon.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
---
api_version: 1

# PHP Version:
# https://pantheon.io/docs/pantheon-yml#php-version
# Set site's PHP version to 7.0
php_version: 7.0

# Drush Version
# https://pantheon.io/docs/pantheon-yml/#drush-version
drush_version: 8

# Protected Web Paths
# https://pantheon.io/docs/pantheon-yml#protected-web-paths
protected_web_paths:
- /example.txt
- /example_directory

# Nested Docroot
# https://pantheon.io/docs/pantheon-yml#nested-docroot
web_docroot: true

# Quicksilver Platform Integration Hooks
# https://pantheon.io/docs/pantheon-yml#quicksilver
workflows:

# Commits: Notify team of new commit to master (dev)
# Site Creation
deploy_product:
after:
- type: webphp
description: Post to Slack after site creation
script: private/scripts/slack_after_site_creation.php

# Multidev Creation
create_cloud_development_environment:
after:
- type: webphp
description: Post to Slack after Multidev creation
script: private/scripts/slack_after_multidev_creation.php

# Commits
sync_code:
after:
- type: webphp
description: post to slack after each code pushed
description: Post to Slack after each code pushed
script: private/scripts/slack_after_code_push.php

# Database Clones: Notify, sanitize, generate content, and notify on db clone
# Database Clones
clone_database:
before:
- type: webphp
description: post to slack before cloning the database
description: Post to Slack before cloning the database
script: private/scripts/slack_before_db_clone.php
after:
- type: webphp
Expand All @@ -24,19 +59,19 @@ workflows:
description: generate development article content after the database clones
script: private/scripts/generate_dev_content.php
- type: webphp
description: post to slack after the database clones
description: Post to Slack after the database clones
script: private/scripts/slack_after_db_clone.php

# Code Deploys: Notify, Sanitize (if on test), post to new relic, update db, and notify completion
# Code Deploys: Notify, Sanitize (if on test), Post to new relic, update db, and notify completion
deploy:
before:
- type: webphp
description: post to slack before cloning the database
description: Post to Slack before cloning the database
script: private/scripts/slack_before_deploy.php
after:
- type: webphp
description: post to new relic always
script: private/scripts/post_new_relic.php
description: Post to new relic always
script: private/scripts/Post_new_relic.php
- type: webphp
description: sanitize the db after deploy to test
script: private/scripts/sanitize_after_db_clone.php
Expand All @@ -50,12 +85,12 @@ workflows:
description: do a visual regression test with Backtrac.io
script: private/scripts/backtrac_visualregression.php
- type: webphp
description: post to slack after each deploy
description: Post to Slack after each deploy
script: private/scripts/slack_after_deploy.php

# Cache Clears: Post to slack after clearing cache
# Cache Clears: Post to Slack after clearing cache
clear_cache:
after:
- type: webphp
description: post to slack after cache clear
description: Post to Slack after cache clear
script: private/scripts/slack_after_clear_cache.php
2 changes: 1 addition & 1 deletion generate_dev_content/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ workflows:
clone_database:
after:
- type: webphp
description: enerate development article content after the database clones
description: Generate development article content after the database clones
script: private/scripts/generate_dev_content.php
```

55 changes: 55 additions & 0 deletions new_relic_apdex_t/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Set New Relic Apdex T values on Multidev Creation #


[All sites on Pantheon include access to New Relic APM Pro](https://pantheon.io/features/new-relic). This application performance monitoring relies on the site owners setting time benchmark to measure against. Ideally, your Drupal or WordPress responds quickly to requests. And if the site is not responding quickly, New Relic can alert you.

The question is, where do you want to set the bar? By default, New Relic uses 0.5 as the target number of seconds. This value is called "T" in the [Apdex (Application Performance Index) formula](https://docs.newrelic.com/docs/apm/new-relic-apm/apdex/apdex-measuring-user-satisfaction).

In addition to monitoring how fast the server (Drupal or WordPress) respond, New Relic can monitor how fast real world browsers render your site. Browser performance is measured with the same Apdex formula. By default, New Relic uses a much more generous 7 seconds as the T value in browser Apdex.

We recommend that any team working on a site discuss expectations for server-side and client-side performance and set T values accordingly. As you are developing new features with [Pantheon Multidev,](https://pantheon.io/features/multidev-cloud-environments) you might even want the Multidev environments to have more stringent T values than Test or Live environments.

This Quicksilver example shows how you can set custom T values for Multidev environments when they are created. Otherwise each environment will use the default values of 0.5 and 7 for server and browser respectively.

To do the actual setting of default values this script first gets an API key and then uses that key to interact with New Relic's REST API to set a T values based on the existing values from the dev (or test/live) environment.

## Instructions ##

To use this example:

1. [Activate New Relic Pro](https://pantheon.io/docs/new-relic/#activate-new-relic-pro) within your site dashboard.
2. Add the example `new_relic_apdex_t.php` script to the `private/scripts` directory of your code repository.
3. Optionally, modify the environment to pull existing threshold T values from at the top of the file. This defaults to `dev` but can also be `test` or `live`.

```php
// get New Relic info from the dev environment
// Change to test or live as you wish
$app_info = get_app_info( 'dev' );

```

4. Add a Quicksilver operation to your `pantheon.yml` to fire the script after a deploy. (One gotcha is that this script cannot be the first or only script called as part of Multidev creation. Before the New Relic API recognizes the the Multidev environment, that environment needs to have received at least one previous request.)


### Example `pantheon.yml` ###

Here's an example of what your `pantheon.yml` would look like if this were the only Quicksilver operation you wanted to use:

```yaml
api_version: 1

workflows:
create_cloud_development_environment:
after:
# The setting of Apdex cannot be the first traffic the new Multidev environment
# receives. A New Relic application ID is not available until after the
# environment receives some traffic. So run another script prior to calling
# new_relic_apdex_t.php. In this case drush_config_import.php is an
# arbitrary example.
- type: webphp
description: Drush Config Import
script: private/scripts/drush_config_import.php
- type: webphp
description: Set Apdex T values
script: private/scripts/new_relic_apdex_t.php
```
176 changes: 176 additions & 0 deletions new_relic_apdex_t/new_relic_apdex_t.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

/**
* @file
* Sets New Relic Apdex T values for newly created multidev environments.
*/

// get New Relic info from the dev environment
// Change to test or live as you wish
$app_info = get_app_info( 'dev' );

$settings = $app_info['settings'];

// The "t" value (number of seconds) for your server-side apdex.
// https://docs.newrelic.com/docs/apm/new-relic-apm/apdex/apdex-measuring-user-satisfaction
$app_apdex_threshold = $app_info['settings']['app_apdex_threshold'];
// Do you want New Relic to add JavaScript to pages to analyze rendering time?
// https://newrelic.com/browser-monitoring
$enable_real_user_monitoring = $app_info['settings']['enable_real_user_monitoring'];
// The "t" value (number of seconds) for browser apdex. (The "real user
// monitoring turned off or on with $enable_real_user_monitoring")
$end_user_apdex_threshold = $app_info['settings']['end_user_apdex_threshold'];

set_thresholds( $app_apdex_threshold, $end_user_apdex_threshold, $enable_real_user_monitoring );

/**
* 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.
*
* Used to retrive T values for a pre-existing environment.
*/
function get_app_info( $env = 'dev' ) {
$nr_connection_info = get_nr_connection_info();
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 );

return $output['application'];
}

/**
* Sets the apdex thresholds.
*/
function set_thresholds( $app_apdex_threshold, $end_user_apdex_threshold, $enable_real_user_monitoring ) {

$nr_connection_info = get_nr_connection_info( PANTHEON_ENVIRONMENT );
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 );

echo "===== Setting New Relic Values for the App '$app_name' =====\n";
echo "Application Apdex Threshold: $app_apdex_threshold\n";
echo "End User Apdex Threshold: $end_user_apdex_threshold\n";
echo "Enable Real User Monitoring: $enable_real_user_monitoring\n";

$url = 'https://api.newrelic.com/v2/applications/' . $app_id . '.json';

$settings = [
'application' => [
'name' => $app_name,
'settings' => [
'app_apdex_threshold' => $app_apdex_threshold,
'end_user_apdex_threshold' => $end_user_apdex_threshold,
'enable_real_user_monitoring' => $enable_real_user_monitoring,
],
],
];

$data_json = json_encode( $settings );

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_json );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT" );
$headers = [
'X-API-KEY:' . $api_key,
'Content-Type: application/json'
];
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
$result = curl_exec( $ch );
if ( curl_errno( $ch ) ) {
echo 'Error:' . curl_error( $ch );
}
curl_close( $ch );

echo "===== Finished Setting New Relic Values =====\n";
}
17 changes: 15 additions & 2 deletions new_relic_deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ This example will show you how you can automatically log changes to your site in

This script uses a couple clever tricks to get data about the platform. First of all it uses the `pantheon_curl()` command to fetch the extended metadata information for the site/environment, which includes the New Relic API key. It also uses data within the git repository on the platform to pull out deploy tag numbers and log messages.

> **Note:** New Relic's Deployments feature is not available for sites using the APM Lite plan. This example will only work for sites with New Relic APM Professional. It will work with all Pantheon sites during the two-week free trial of New Relic Pro that begins after you enable the add-on.
> **Note:** This example will work for all Pantheon sites once the bundled [New Relic APM Pro feature](https://pantheon.io/features/new-relic) is activated, regardless of service level.

## Instructions ##

Setting up this example is easy:

1. [Enable New Relic](https://pantheon.io/docs/articles/sites/newrelic/) for your site.
1. [Activate New Relic Pro](https://pantheon.io/docs/new-relic/#activate-new-relic-pro) within your site Dashboard.
2. Add the example `new_relic_deploy.php` script to the `private` directory of your code repository.
3. Add a Quicksilver operation to your `pantheon.yml` to fire the script after a deploy.
4. Test a deploy out!
Expand All @@ -22,12 +22,25 @@ Optionally, you may want to use the `terminus workflows watch` command to get im
Here's an example of what your `pantheon.yml` would look like if this were the only Quicksilver operation you wanted to use:

```yaml
# Always need to specify the pantheon.yml API version.
api_version: 1

# You might also want some of the following here:
# php_version: 7.0
# drush_version: 8

workflows:
# Log to New Relic when deploying to test or live.
deploy:
after:
- type: webphp
description: Log to New Relic
script: private/scripts/new_relic_deploy.php
# Also log sync_code so you can track new code going into dev/multidev.
sync_code:
after:
- type: webphp
description: Log to New Relic
script: private/scripts/new_relic_deploy.php

```
Loading