Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Profilerstellung für Code außerhalb von Google Cloud
Auf dieser Seite wird beschrieben, wie Sie Profile für Anwendungen erstellen, die außerhalb von Google Cloudausgeführt werden.
In diesem Szenario werden Ihre Anwendung und der Cloud Profiler-Agent außerhalb von Google Cloudausgeführt. Sie verwenden jedoch die Cloud Profiler-Oberfläche, um die Profildaten zu analysieren.
Damit Sie Profildaten mit der Profiler-Oberfläche analysieren können, benötigen Sie ein Google Cloud -Projekt. Der an einem anderen Ort ausgeführte Profiler-Agent muss in der Lage sein, die Profile zur Analyse zurückzusenden. Damit dies möglich ist, müssen Sie Folgendes tun:
Erstellen Sie ein Google Cloud -Projekt und aktivieren Sie die API.
Rufen Sie Anmeldedaten für den Profiler-Agent zum Hochladen von Profilen ab.
Konfigurieren Sie den Agent für die Verwendung der Anmeldedaten und der ID desGoogle Cloud -Projekts.
Google Cloud -Projekt erstellen
In the Google Cloud console, on the project selector page,
click Create project to begin creating a new
Google Cloud project.
Weisen Sie dem Dienstkonto die Rolleroles/cloudprofiler.agent zu, damit es zum Schreiben von Profildaten berechtigt ist. Verwenden Sie dazu beispielsweise Google Cloud CLI:
Gehen Sie auf der Maschine, auf der der Profiler-Agent ausgeführt wird, so vor:
Legen Sie eine Kopie der Datei ab, die den soeben erstellten JSON-Schlüssel enthält.
Legen Sie die Umgebungsvariable GOOGLE_APPLICATION_CREDENTIALS auf den vollständig qualifizierten Namen der Datei fest, die den JSON-Schlüssel enthält.
Diese Umgebungsvariable muss für den Prozess sichtbar sein, der den Profiler-Agent ausführt. Wenn Sie also ein Skript oder Dockerfile zum Ausführen des Prozesses verwenden, fügen Sie die Umgebungsvariable dort ein.
Standardanmeldedaten für Anwendungen verwenden
Damit der Agent Standardanmeldedaten für Anwendungen verwenden kann, rufen Sie Nutzeranmeldedaten über einen Webablauf ab und legen diese dort ab, wo die Bibliothek der Standardanmeldedaten für Anwendungen sie erwartet.
Diese Anmeldedaten fungieren als Proxy für ein Dienstkonto.
Führen Sie folgenden Google Cloud CLI-Befehl aus, um Standardanmeldedaten für Anwendungen zu verwenden:
gcloud auth application-default login
Folgen Sie dann den Schritten, durch die Sie dieser Befehl führt.
Agent mit einem Google Cloud -Projekt verknüpfen
Damit der Profiler-Agent Profile hochladen kann, muss in seiner Konfiguration die ID Ihres Google Cloud -Projekts angegeben werden.
Wie Sie das tun, hängt von der Sprache ab.
Go
Geben Sie den zusätzlichen Parameter ProjectID im Objekt profiler.Config an, das unter Profilerstellung für Go-Anwendungen beschrieben wird:
Wenn Ihre Anwendung nicht auf den Compute Engine-Metadatenserver zugreifen kann, werden Meldungen wie diese angezeigt:
Error making HTTP request for 169.254.169.254:80/computeMetadata/v1/instance/zone
Wenn Sie diese Meldungen nicht erhalten möchten, fügen Sie den Flags für die Agent-Konfiguration den Parameter -cprof_zone_name=VALUE hinzu und starten Sie die Anwendung neu. Ersetzen Sie für dieses Szenario VALUE durch einen beschreibenden String wie „test“.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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)"]]