Collect additional resource metrics using the Ops Agent

This document describes how to create and run a Batch job that automatically installs the Ops Agent. Install the Ops Agent to provide additional metrics in Cloud Monitoring about the performance of a job's resources. To learn more about using resource performance metrics for a job, see Monitor and optimize job resources by viewing metrics.

Before you begin

Ops Agent requirements

To create and run a job that uses the Ops Agent, your job must comply with all the following requirements:

For more information about the features and requirements of the Ops Agent, see Ops Agent overview in the Google Cloud Observability documentation.

Create a job that automatically installs the Ops Agent

Use the Google Cloud CLI or REST API to create a job that includes the installOpsAgent field set to true in the allocationPolicy.instances field in the main body in the JSON file:

"allocationPolicy": {
  "instances": [
    {
      "installOpsAgent": true
    }
  ]
}

For example, a job that automatically installs the Ops Agent can have a JSON configuration file that is similar to the following:

{
  "taskGroups": [
    {
      "taskSpec": {
        "runnables": [
          {
            "script": {
              "text": "echo Hello World! This is task $BATCH_TASK_INDEX."
            }
          }
        ]
      },
      "taskCount": 3,
    }
  ],
  "allocationPolicy": {
    "instances": [
      {
        "installOpsAgent": true
      }
    ]
  },
  "logsPolicy": {
      "destination": "CLOUD_LOGGING"
  }
}

After the job's VMs start running, you can see the Ops Agent metrics the same as any other resource metric. For more information, see Monitor and optimize job resources by viewing metrics.

What's next