Track deployments

You can enable the New Relic Track changes feature to monitor deployment events on your Commerce on cloud infrastructure project.

The Deployments data collection helps analyze the impact of deployment changes to overall performance, such as CPU, memory, response time, and more. See Track changes using NerdGraph in the New Relic documentation.

PREREQUISITES
  • NR_API_URL: New Relic API endpoint, in this case NerdGraph API URL https://api.newrelic.com/graphql
  • NR_API_KEY: Create a user key, see New Relic API Keys in the New Relic documentation.
  • NR_APP_GUID: An entity that reports data to New Relic has a unique ID (GUID). As an example, to enable on a Staging environment, adjust the Staging environment NR_APP_GUID cloud variable with the staging entity GUID from New Relic. See the Learn about New Relic entities and NerdGraph tutorial: View entity data in the New Relic documentation.

Enable Track deployments

Track your Commerce project deployment events in New Relic by creating a script integration.

To enable the track deployments:

  1. On your local workstation, change to your project directory.

  2. Create an action-integration.js file. Copy the following code and paste it in the action-integration.js file and save:

    code language-javascript
    function trackDeployments() {
      const envName = activity.payload.environment.name;
      let variables;
      activity.payload.deployment.variables.forEach(function(variable) {
        if (variable.name === "env:NR_CONFIG") {
          variables = variable.value;
        }
      });
      const config = JSON.parse(variables.replace(/'/g, '"'));
      const commitSha = activity.payload.commits ? activity.payload.commits[0].sha : activity.payload.environment.head_commit;
      const deploymentType = activity.type;
    
      if (!(envName in config)) {
        throw new Error('There is no configuration for ' + envName);
      }
    
      const configEnv = config[envName];
    
      if (!configEnv.NR_APP_GUID || !configEnv.NR_API_KEY || !configEnv.NR_API_URL) {
        throw new Error('You must define the next configuation in the env variable NR_CONFIG: NR_APP_GUID, NR_API_KEY and NR_API_URL');
      }
    
      const query = `mutation {
        changeTrackingCreateDeployment(
        deployment: {
            version: "${commitSha}",
            entityGuid: "${configEnv.NR_APP_GUID}",
            commit: "${commitSha}",
            changelog: "${deploymentType}"
        }
        ) {
          deploymentId
          entityGuid
        }
      }`;
    
      var resp = fetch(configEnv.NR_API_URL, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'API-Key': configEnv.NR_API_KEY
        },
        body: JSON.stringify({
            query
        })
      });
    
      if (!resp.ok) {
        console.log('Sending new relic change tracking failed: ' + resp.text());
      } else {
        console.log(resp.text());
      }
    }
    
    trackDeployments();
    
  3. Create a script integration using the magento-cloud CLI command and reference the action-integration.js file.

    code language-bash
    magento-cloud integration:add --type script --events='environment.restore, environment.push, environment.branch, environment.activate, environment.synchronize, environment.initialize, environment.merge, environment.redeploy, environment.variable.create, environment.variable.delete, environment.variable.update' --file ./action-integration.js --project=<YOUR_PROJECT_ID> --environments=<YOUR_ENVIRONMENT_ID>
    

    Sample response:

    code language-terminal
    Created integration 767u4hathojjw (type: script)
    +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
    | Property              | Value                                                                                                                                   |
    +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
    | id                    | 767u4hathojjw                                                                                                                           |
    | type                  | script                                                                                                                                  |
    | role                  |                                                                                                                                         |
    | events                | - environment.restore                                                                                                                   |
    |                       | - environment.push                                                                                                                      |
    |                       | - environment.branch                                                                                                                    |
    |                       | - environment.activate                                                                                                                  |
    |                       | - environment.synchronize                                                                                                               |
    |                       | - environment.initialize                                                                                                                |
    |                       | - environment.merge                                                                                                                     |
    |                       | - environment.redeploy                                                                                                                  |
    |                       | - environment.variable.create                                                                                                           |
    |                       | - environment.variable.delete                                                                                                           |
    |                       | - environment.variable.update                                                                                                           |
    | environments          | - staging                                                                                                                               |
    |                       | - production                                                                                                                            |
    | excluded_environments | {  }                                                                                                                                    |
    | states                | - complete                                                                                                                              |
    | result                | *                                                                                                                                       |
    | script                | function variables() {                                                                                                                  |
    |                       |     var vars = {};                                                                                                                      |
    |                       |     activity.payload.deployment.variables.forEach(function(variable) {                                                                  |
    |                       |         vars[variable.name] = variable.value;                                                                                           |
    |                       |     });                                                                                                                                 |
    |                       |     return vars;                                                                                                                        |
    |                       | }                                                                                                                                       |
    |                       |                                                                                                                                         |
    |                       | function trackDeployments() {                                                                                                           |
    |                       |     const envName = activity.payload.environment.name;                                                                                  |
    |                       |                                                                                                                                         |
    |                       |     const config = JSON.parse(variables()['env:NR_CONFIG'].replace(/'/g, '"'));                                                         |
    |                       |     const commitSha = activity.payload.commits ? activity.payload.commits[0].sha : activity.payload.environment.head_commit;            |
    |                       |     const deploymentType = activity.type;                                                                                               |
    |                       |                                                                                                                                         |
    |                       |     if (!(envName in config)) {                                                                                                         |
    |                       |         throw new Error('There is no configuration for ' + envName);                                                                    |
    |                       |     }                                                                                                                                   |
    |                       |                                                                                                                                         |
    |                       |     const configEnv = config[envName];                                                                                                  |
    |                       |                                                                                                                                         |
    |                       |     if (!configEnv.NR_APP_GUID || !configEnv.NR_API_KEY || !configEnv.NR_API_URL) {                                                     |
    |                       |         throw new Error('You must define the next configuation in the env variable NR_CONFIG: NR_APP_GUID, NR_API_KEY and NR_API_URL'); |
    |                       |     }                                                                                                                                   |
    |                       |                                                                                                                                         |
    |                       |     const query = `mutation {                                                                                                           |
    |                       |         changeTrackingCreateDeployment(                                                                                                 |
    |                       |           deployment: {                                                                                                                 |
    |                       |             version: "${commitSha}",                                                                                                    |
    |                       |             entityGuid: "${configEnv.NR_APP_GUID}",                                                                                     |
    |                       |             commit: "${commitSha}",                                                                                                     |
    |                       |             changelog: "${deploymentType}"                                                                                              |
    |                       |           }                                                                                                                             |
    |                       |         ) {                                                                                                                             |
    |                       |           deploymentId                                                                                                                  |
    |                       |           entityGuid                                                                                                                    |
    |                       |         }                                                                                                                               |
    |                       |     }`;                                                                                                                                 |
    |                       |                                                                                                                                         |
    |                       |     var resp = fetch(configEnv.NR_API_URL, {                                                                                            |
    |                       |         method: 'POST',                                                                                                                 |
    |                       |         headers: {                                                                                                                      |
    |                       |             'Content-Type': 'application/json',                                                                                         |
    |                       |             'API-Key': configEnv.NR_API_KEY                                                                                             |
    |                       |         },                                                                                                                              |
    |                       |         body: JSON.stringify({                                                                                                          |
    |                       |             query                                                                                                                       |
    |                       |         })                                                                                                                              |
    |                       |     });                                                                                                                                 |
    |                       |                                                                                                                                         |
    |                       |     if (!resp.ok) {                                                                                                                     |
    |                       |         console.log('Sending new relic change tracking failed: ' + resp.text());                                                        |
    |                       |     } else {                                                                                                                            |
    |                       |         console.log(resp.text());                                                                                                       |
    |                       |     }                                                                                                                                   |
    |                       | }                                                                                                                                       |
    |                       |                                                                                                                                         |
    |                       | trackDeployments();                                                                                                                     |
    |                       |                                                                                                                                         |
    +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
    
  4. Make a note of the integration ID for later use. In this example, the ID is:

    code language-terminal
    Created integration 767u4hathojjw (type: script)
    

    Optionally, you can verify the integration and note the integration ID using: magento-cloud integration:list

  5. Create the environment variable using the prerequisites.

    code language-bash
    magento-cloud variable:create --level project --name=env:NR_CONFIG --value='{"<YOUR_ENVIRONMENT_ID>":{"NR_API_KEY": "<YOUR_API_KEY>", "NR_API_URL": "https://api.newrelic.com/graphql", "NR_APP_GUID":"<YOUR_APP_GUID>"}}'  -p <YOUR_PROJECT_ID>
    
  6. Review the last activity log.

    code language-bash
    magento-cloud integration:activity:log <INTEGRATION_ID> -p <YOUR_PROJECT_ID> -e <YOUR_ENVIRONMENT_ID>
    

    Response:

    code language-terminal
    Integration ID: 767u4hathojjw
    Activity ID: poxqidsfajkmg
    Type: integration.script
    Description: Running activity script
    Created: 2023-08-28T20:32:02+00:00
    State: complete
    Log:
    HTTP request
    HTTP response
    {"data":{"changeTrackingCreateDeployment":{"deploymentId":"some-deployment-id","entityGuid":"SomeGUIDhere"}}}
    
  7. Log in to your New Relic account.

  8. In the Explorer navigation menu, click APM & Services. Select your environment Name and Account.

  9. Under Events, click Change tracking.

    Deployments

recommendation-more-help
05f2f56e-ac5d-4931-8cdb-764e60e16f26