Suivi des déploiements

Vous pouvez activer la fonction Suivi des modifications de New Relic pour surveiller les événements de déploiement sur votre projet d’infrastructure cloud Commerce.

La collecte de données Déploiements permet d’analyser l’impact des modifications de déploiement sur les performances globales, telles que le processeur, la mémoire, le temps de réponse, etc. Voir Suivi des modifications à l’aide de NerdGraph dans la documentation de New Relic.

PREREQUISITES
  • NR_API_URL : point d’entrée de l’API New Relic, dans ce cas, URL de l’API NerdGraph https://api.newrelic.com/graphql
  • NR_API_KEY : Créez une clé utilisateur, voir New Relic API Keys dans la documentation New Relic.
  • NR_APP_GUID : une entité qui signale des données à New Relic possède un identifiant unique (GUID). Par exemple, pour activer dans un environnement d’évaluation, ajustez la variable cloud de l’environnement d’évaluation NR_APP_GUID avec le GUID de l’entité d’évaluation de New Relic. Consultez le tutorielet NerdGraph sur les entités New Relic : Afficher les données d’entitédans la documentation New Relic.

Activation du suivi des déploiements

Effectuez le suivi des événements de déploiement de projet Commerce dans New Relic en créant une intégration script.

Pour activer le suivi des déploiements :

  1. Sur votre poste de travail local, modifiez le répertoire de votre projet.

  2. Créez un fichier action-integration.js. Copiez le code suivant et collez-le dans le fichier action-integration.js et enregistrez :

    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. Créez une intégration script à l’aide de la commande d’interface de ligne de commande magento-cloud et référencez le fichier action-integration.js.

    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>
    

    Exemple de réponse :

    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. Notez l’ID d’intégration pour une utilisation ultérieure. Dans cet exemple, l’ID est :

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

    Vous pouvez éventuellement vérifier l’intégration et noter l’ID d’intégration à l’aide de : magento-cloud integration:list

  5. Créez la variable d'environnement à l'aide des prérequis.

    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. Examinez le dernier journal d’activité.

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

    Réponse :

    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. Connectez-vous à votre compte New Relic.

  8. Dans le menu de navigation de l'Explorateur, cliquez sur APM & Services. Sélectionnez votre environnement Name et Account.

  9. Sous Events, cliquez sur Change tracking.

    Déploiements

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