|
| 1 | +# Google Cloud Authentication Extension for OpenTelemetry Java Agent |
| 2 | + |
| 3 | +The Google Cloud Auth Extension allows the users to export telemetry from their applications auto-instrumented using the OpenTelemetry Java Agent to Google Cloud using the built-in OTLP exporters. |
| 4 | +The extension takes care of the necessary configuration required to authenticate to GCP to successfully export telemetry. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +### Ensure the presence of Google Cloud Credentials on your machine/environment |
| 9 | + |
| 10 | +```shell |
| 11 | +gcloud auth application-default login |
| 12 | +``` |
| 13 | +Executing this command will save your application credentials to default path which will depend on the type of machine - |
| 14 | +- Linux, macOS: `$HOME/.config/gcloud/application_default_credentials.json` |
| 15 | +- Windows: `%APPDATA%\gcloud\application_default_credentials.json` |
| 16 | + |
| 17 | +**NOTE: This method of authentication is not recommended for production environments.** |
| 18 | + |
| 19 | +Next, export the credentials to `GOOGLE_APPLICATION_CREDENTIALS` environment variable - |
| 20 | + |
| 21 | +For Linux & MacOS: |
| 22 | +```shell |
| 23 | +export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json |
| 24 | +``` |
| 25 | + |
| 26 | +These credentials are built-in running in a Google App Engine, Google Cloud Shell or Google Compute Engine environment. |
| 27 | + |
| 28 | +### Configuring the extension |
| 29 | + |
| 30 | +The extension can be configured either by environment variables or system properties. |
| 31 | + |
| 32 | +Here is a list of configurable options for the extension: |
| 33 | + |
| 34 | +- `GOOGLE_CLOUD_PROJECT`: Environment variable that represents the Google Cloud Project ID to which the telemetry needs to be exported. |
| 35 | + - Can also be configured using `google.cloud.project` system property. |
| 36 | + - If this option is not configured, the extension would infer GCP Project ID from the application default credentials. For more information on application default credentials, see [here](https://cloud.google.com/docs/authentication/application-default-credentials). |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +The OpenTelemetry Java Agent Extension can be easily added to any Java application by modifying the startup command to the application. |
| 41 | +For more information on Extensions, see the [documentation here](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/examples/extension/README.md). |
| 42 | + |
| 43 | +Below is a snippet showing how to add the extension to a Java application using the Gradle build system. |
| 44 | + |
| 45 | +```gradle |
| 46 | +// Specify OpenTelemetry Autoinstrumentation Java Agent Path. |
| 47 | +def otelAgentPath = <OpenTelemetry Java Agent location> |
| 48 | +// Specify the path for Google Cloud Authentication Extension for the Java Agent. |
| 49 | +def extensionPath = <Google Cloud Authentication Extension location> |
| 50 | +def googleCloudProjectId = <Your Google Cloud Project ID> |
| 51 | +def googleOtlpEndpoint = <Google Cloud OTLP endpoint> |
| 52 | +
|
| 53 | +application { |
| 54 | + ... |
| 55 | + "-javaagent:${otelAgentPath}", |
| 56 | + "-Dotel.javaagent.extensions=${extensionPath}", |
| 57 | + // Configure the GCP Auth extension using system properties. |
| 58 | + // This can also be configured using environment variables. |
| 59 | + "-Dgoogle.cloud.project=${googleCloudProjectId}", |
| 60 | + // Configure auto instrumentation. |
| 61 | + "-Dotel.exporter.otlp.traces.endpoint=${googleOtlpEndpoint}", |
| 62 | + '-Dotel.java.global-autoconfigure.enabled=true', |
| 63 | + // Optionally enable the built-in GCP resource detector |
| 64 | + '-Dotel.resource.providers.gcp.enabled=true' |
| 65 | + '-Dotel.traces.exporter=otlp', |
| 66 | + '-Dotel.metrics.exporter=logging', |
| 67 | +} |
| 68 | +``` |
0 commit comments