|
| 1 | +# Google Cloud Authentication Extension |
| 2 | + |
| 3 | +The Google Cloud Auth Extension allows the users to export telemetry from their applications 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 | + |
| 14 | +Executing this command will save your application credentials to default path which will depend on the type of machine - |
| 15 | + |
| 16 | +- Linux, macOS: `$HOME/.config/gcloud/application_default_credentials.json` |
| 17 | +- Windows: `%APPDATA%\gcloud\application_default_credentials.json` |
| 18 | + |
| 19 | +**NOTE: This method of authentication is not recommended for production environments.** |
| 20 | + |
| 21 | +Next, export the credentials to `GOOGLE_APPLICATION_CREDENTIALS` environment variable - |
| 22 | + |
| 23 | +For Linux & MacOS: |
| 24 | + |
| 25 | +```shell |
| 26 | +export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json |
| 27 | +``` |
| 28 | + |
| 29 | +These credentials are built-in running in a Google App Engine, Google Cloud Shell or Google Compute Engine environment. |
| 30 | + |
| 31 | +### Configuring the extension |
| 32 | + |
| 33 | +The extension can be configured either by environment variables or system properties. |
| 34 | + |
| 35 | +Here is a list of configurable options for the extension: |
| 36 | + |
| 37 | +- `GOOGLE_CLOUD_PROJECT`: Environment variable that represents the Google Cloud Project ID to which the telemetry needs to be exported. |
| 38 | + - Can also be configured using `google.cloud.project` system property. |
| 39 | + - 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). |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +### With OpenTelemetry Java agent |
| 44 | + |
| 45 | +The OpenTelemetry Java Agent Extension can be easily added to any Java application by modifying the startup command to the application. |
| 46 | +For more information on Extensions, see the [documentation here](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/examples/extension/README.md). |
| 47 | + |
| 48 | +Below is a snippet showing how to add the extension to a Java application using the Gradle build system. |
| 49 | + |
| 50 | +```gradle |
| 51 | +// Specify OpenTelemetry Autoinstrumentation Java Agent Path. |
| 52 | +def otelAgentPath = <OpenTelemetry Java Agent location> |
| 53 | +// Specify the path for Google Cloud Authentication Extension for the Java Agent. |
| 54 | +def extensionPath = <Google Cloud Authentication Extension location> |
| 55 | +def googleCloudProjectId = <Your Google Cloud Project ID> |
| 56 | +def googleOtlpEndpoint = <Google Cloud OTLP endpoint> |
| 57 | +
|
| 58 | +val autoconf_config = listOf( |
| 59 | + "-javaagent:${otelAgentPath}", |
| 60 | + "-Dotel.javaagent.extensions=${extensionPath}", |
| 61 | + // Configure the GCP Auth extension using system properties. |
| 62 | + // This can also be configured using environment variables. |
| 63 | + "-Dgoogle.cloud.project=${googleCloudProjectId}", |
| 64 | + // Configure auto instrumentation. |
| 65 | + "-Dotel.exporter.otlp.traces.endpoint=${googleOtlpEndpoint}", |
| 66 | + '-Dotel.java.global-autoconfigure.enabled=true', |
| 67 | + // Optionally enable the built-in GCP resource detector |
| 68 | + '-Dotel.resource.providers.gcp.enabled=true' |
| 69 | + '-Dotel.traces.exporter=otlp', |
| 70 | + '-Dotel.metrics.exporter=logging' |
| 71 | +) |
| 72 | +
|
| 73 | +application { |
| 74 | + ... |
| 75 | + applicationDefaultJvmArgs = autoconf_config |
| 76 | + ... |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +### Without OpenTelemetry Java agent |
| 81 | + |
| 82 | +This extension can be used without the OpenTelemetry Java agent by leveraging the [OpenTelemetry SDK Autoconfigure](https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/README.md) module.\ |
| 83 | +When using the autoconfigured SDK, simply adding this extension as a dependency automatically configures authentication headers and resource attributes for spans, enabling export to Google Cloud. |
| 84 | + |
| 85 | +Below is a snippet showing how to use this extension as a dependency when the application is not instrumented using the OpenTelemetry Java agent. |
| 86 | + |
| 87 | +```gradle |
| 88 | +dependencies { |
| 89 | + implementation("io.opentelemetry:opentelemetry-api") |
| 90 | + implementation("io.opentelemetry:opentelemetry-sdk") |
| 91 | + implementation("io.opentelemetry:opentelemetry-exporter-otlp") |
| 92 | + implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") |
| 93 | + // include the auth extension dependency |
| 94 | + implementation("io.opentelemetry.contrib:opentelemetry-gcp-auth-extension") |
| 95 | +
|
| 96 | + // other dependencies |
| 97 | + ... |
| 98 | +
|
| 99 | +} |
| 100 | +
|
| 101 | +val autoconf_config = listOf( |
| 102 | + '-Dgoogle.cloud.project=your-gcp-project-id', |
| 103 | + '-Dotel.exporter.otlp.endpoint=https://your.otlp.endpoint:1234', |
| 104 | + '-Dotel.traces.exporter=otlp', |
| 105 | + '-Dotel.java.global-autoconfigure.enabled=true' |
| 106 | +
|
| 107 | + // any additional args |
| 108 | + ... |
| 109 | +) |
| 110 | +
|
| 111 | +application { |
| 112 | + applicationDefaultJvmArgs = autoconf_config |
| 113 | +
|
| 114 | + // additional configuration |
| 115 | + ... |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Component Owners |
| 120 | + |
| 121 | +- [Josh Suereth](https://github.com/jsuereth), Google |
| 122 | +- [Pranav Sharma](https://github.com/psx95), Google |
| 123 | + |
| 124 | +Learn more about component owners in [component_owners.yml](../.github/component_owners.yml). |
0 commit comments