[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[],[],null,["# Profiling applications running outside Google Cloud\n===================================================\n\nThis page describes how to profile applications running outside Google Cloud.\n\nIn this scenario, your application and the Cloud Profiler agent\nrun outside Google Cloud, but you use the Cloud Profiler\ninterface to analyze the profiling data.\n\nUsing the Profiler interface to analyze profiling data\nrequires a Google Cloud project. The profiling agent running elsewhere\nmust be able to send the profiles back for analysis. To enable this, you must:\n\n1. Create a Google Cloud project and enable the API.\n2. Obtain credentials for the profiling agent to use when uploading profiles.\n3. Configure the agent to use the credentials and the ID of the Google Cloud project.\n\nCreate a Google Cloud project\n-----------------------------\n\nIn the Google Cloud console, on the project selector page,\nclick **Create project** to begin creating a new\nGoogle Cloud project.\n\n[Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n\nEnable the Profiler API\n-----------------------\n\n1.\n\n\n Enable the required API.\n\n\n [Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=cloudprofiler.googleapis.com)\n2. \u003cbr /\u003e\n\n If **API enabled** is displayed, then the API is already enabled. If not, click the **Enable** button.\n\n \u003cbr /\u003e\n\nObtain credentials for the agent\n--------------------------------\n\nThere are two ways to obtain credentials for the agent to use:\n\n- Let the agent use a service account with private-key authentication\n- Let the agent use application default credentials (ADC).\n\n### Using service accounts\n\nTo enable the agent to use a service account with private-key authentication,\nyou must:\n\n1. Create a service account. For example, using the Google Cloud CLI:\n\n gcloud iam service-accounts create \u003cvar translate=\"no\"\u003eMY_SVC_ACCT_ID\u003c/var\u003e --display-name \"my service account\"\n\n See [Creating a service account](/iam/docs/creating-managing-service-accounts#creating_a_service_account) for more information.\n2. Grant the service account the **roles/cloudprofiler.agent**\n [role](/profiler/docs/iam), so that it can write profiling data. For example, using\n the Google Cloud CLI:\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eGCP_PROJECT_ID\u003c/span\u003e\u003c/var\u003e \\\n --member serviceAccount:\u003cvar translate=\"no\"\u003eMY_SVC_ACCT_ID\u003c/var\u003e@\u003cvar translate=\"no\"\u003eGCP_PROJECT_ID\u003c/var\u003e.iam.gserviceaccount.com \\\n --role roles/cloudprofiler.agent\n\n See [Granting roles to service accounts](/iam/docs/granting-roles-to-service-accounts#granting_access_to_a_service_account_for_a_resource) for more\n information.\n3. Create a JSON key for the service account. For example, using the\n Google Cloud CLI:\n\n gcloud iam service-accounts keys create \\\n ~/key.json \\\n --iam-account \u003cvar translate=\"no\"\u003eMY_SVC_ACCT_ID\u003c/var\u003e@\u003cvar translate=\"no\"\u003eGCP_PROJECT_ID\u003c/var\u003e.iam.gserviceaccount.com\n\n See [Creating service account keys](/iam/docs/creating-managing-service-account-keys) for more\n information.\n4. On the machine where the profiling agent will run:\n\n 1. Put a copy of the file containing the JSON key you just created.\n 2. Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the fully qualified name of the file containing the JSON key. This environment variable must be visible to the process running the profiling agent, so if you use a script or Dockerfile to run the process, include the environment variable there.\n\n### Using application default credentials\n\nTo enable the agent to use application default credentials,\nyou obtain user-access credentials via a web flow\nand put them where the Application Default Credentials library expects them.\nThese credentials act as a proxy for a service account.\n\nTo use application default credentials, run the following Google Cloud CLI command: \n\n gcloud auth application-default login\n\nand follow the steps this command guides you through.\n\nLinking the agent to a Google Cloud project\n-------------------------------------------\n\nThe profiling agent must be configured to specify the ID of\nyour Google Cloud project so it can upload profiles.\nThe mechanism for doing this depends on the language. \n\n### Go\n\nSpecify an additional parameter, `ProjectID`, in the `profiler.Config`\nobject described in [Profiling Go applications](/profiler/docs/profiling-go#using-profiler): \n\n profiler.Config{ProjectID: \"\u003cvar translate=\"no\"\u003eGCP_PROJECT_ID\u003c/var\u003e\", ...}\n\n### Java\n\nSpecify an additional [Java agent configuration](/profiler/docs/profiling-java#agent_configuration) flag,\n`cprof_project_id`, on the Java invocation: \n\n -cprof_project_id=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eGCP_PROJECT_ID\u003c/span\u003e\u003c/var\u003e\n\nWhen your application isn't able to access the Compute Engine metadata\nserver, messages similar to the following are displayed: \n\n```sh\n Error making HTTP request for 169.254.169.254:80/computeMetadata/v1/instance/zone\n```\nTo stop these messages, add `-cprof_zone_name=`\u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e to your agent configuration flags and restart your application. For this scenario, replace \u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e with a descriptive string such as \"test\".\n\n\u003cbr /\u003e\n\n### Node.js\n\nSpecify an additional parameter, `projectID`, in the `serviceContext`\nobject described in [Profiling Node.js applications](/profiler/docs/profiling-nodejs#using-profiler): \n\n projectId: '\u003cvar translate=\"no\"\u003eGCP_PROJECT_ID\u003c/var\u003e',\n serviceContext: {\n ...\n }\n\n### Python\n\nSpecify an additional parameter, `project_id`, in the `start`\nmethod call described in [Profiling Python applications](/profiler/docs/profiling-python#using-profiler): \n\n googlecloudprofiler.start(..., project_id='\u003cvar translate=\"no\"\u003eGCP_PROJECT_ID\u003c/var\u003e')\n\nWhat's next\n-----------\n\n- [Select the profiles to analyze](/profiler/docs/selecting-profiles)\n- [Interact with the flame graph](/profiler/docs/interacting-flame-graph)\n- [Filter the flame graph](/profiler/docs/filtering-profiles)\n- [Focus the flame graph](/profiler/docs/focusing-profiles)\n- [Compare profiles](/profiler/docs/comparing-profiles)"]]