Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Membuat profil aplikasi yang berjalan di luar Google Cloud
Halaman ini menjelaskan cara membuat profil aplikasi yang berjalan di luar Google Cloud.
Dalam skenario ini, aplikasi dan agen Cloud Profiler Anda berjalan di luar Google Cloud, tetapi Anda menggunakan antarmuka Cloud Profiler untuk menganalisis data pembuatan profil.
Menggunakan antarmuka Profiler untuk menganalisis data pembuatan profil
memerlukan Google Cloud project. Agen pembuatan profil yang berjalan di tempat lain
harus dapat mengirimkan kembali profil untuk dianalisis. Untuk mengaktifkannya, Anda harus:
Buat Google Cloud project dan aktifkan API.
Dapatkan kredensial untuk digunakan agen pembuatan profil saat mengupload profil.
Konfigurasi agen untuk menggunakan kredensial dan ID projectGoogle Cloud .
Buat Google Cloud project
In the Google Cloud console, on the project selector page,
click Create project to begin creating a new
Google Cloud project.
Berikan peranroles/cloudprofiler.agent kepada akun layanan, sehingga akun layanan dapat menulis data pembuatan profil. Misalnya, menggunakan
Google Cloud CLI:
Di komputer tempat agen pembuatan profil akan berjalan:
Letakkan salinan file yang berisi kunci JSON yang baru saja Anda buat.
Tetapkan variabel lingkungan GOOGLE_APPLICATION_CREDENTIALS
ke nama file yang sepenuhnya memenuhi syarat yang berisi kunci JSON.
Variabel lingkungan ini harus terlihat oleh proses yang menjalankan
agen pembuatan profil, jadi jika Anda menggunakan skrip atau Dockerfile untuk menjalankan
proses, sertakan variabel lingkungan di sana.
Menggunakan kredensial default aplikasi
Untuk mengizinkan agen menggunakan kredensial default aplikasi,
Anda mendapatkan kredensial akses pengguna melalui alur web
dan menempatkannya di tempat yang diharapkan oleh library Kredensial Default Aplikasi.
Kredensial ini bertindak sebagai proxy untuk akun layanan.
Untuk menggunakan kredensial default aplikasi, jalankan perintah Google Cloud CLI berikut:
gcloud auth application-default login
dan ikuti langkah-langkah yang dipandu oleh perintah ini.
Menautkan agen ke project Google Cloud
Agen pembuatan profil harus dikonfigurasi untuk menentukan ID project Google Cloud
Anda agar dapat mengupload profil.
Mekanisme untuk melakukannya bergantung pada bahasa.
Go
Tentukan parameter tambahan, ProjectID, dalam objek profiler.Config
yang dijelaskan dalam Membuat profil aplikasi Go:
profiler.Config{ProjectID:"GCP_PROJECT_ID",...}
Java
Tentukan flag konfigurasi agen Java tambahan,
cprof_project_id, pada pemanggilan Java:
-cprof_project_id=GCP_PROJECT_ID
Jika aplikasi Anda tidak dapat mengakses server metadata Compute Engine, pesan yang mirip dengan berikut akan ditampilkan:
Error making HTTP request for 169.254.169.254:80/computeMetadata/v1/instance/zone
Untuk menghentikan pesan ini, tambahkan -cprof_zone_name=VALUE ke flag konfigurasi agen Anda dan mulai ulang aplikasi Anda. Untuk skenario ini,
ganti VALUE dengan string deskriptif seperti "test".
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-02 UTC."],[],[],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)"]]