|
1 | 1 | # Resource detection Example |
2 | 2 |
|
3 | | -An example application that shows what resource attributes will be detected. |
| 3 | +> [!NOTE] |
| 4 | +> Running this example does not generate any telemetry. This example merely demonstrates resource attributes detetcted by the GCP Resource Detector. |
4 | 5 |
|
5 | | -To spin it up on your own GKE cluster, run the following: |
| 6 | +An example application that shows what resource attributes will be detected by the [GCP Resource Detector](https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/gcp-resources) and how the [Autoconfigure Resource Provider SPI](https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure#resource-provider-spi) automatically *attaches* the detected resource attributes to the generated telemetry. |
| 7 | + |
| 8 | +### Prerequisites |
| 9 | + |
| 10 | +##### Get Google Cloud Credentials on your machine |
| 11 | + |
| 12 | +```shell |
| 13 | +gcloud auth application-default login |
| 14 | +``` |
| 15 | +Executing this command will save your application credentials to default path which will depend on the type of machine - |
| 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 | +```shell |
| 25 | +export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json |
| 26 | +``` |
| 27 | + |
| 28 | +For Windows: |
| 29 | +```shell |
| 30 | +SET GOOGLE_APPLICATION_CREDENTIALS=%APPDATA%\gcloud\application_default_credentials.json |
| 31 | +``` |
| 32 | + |
| 33 | +##### Export the Google Cloud Project ID to `GOOGLE_CLOUD_PROJECT` environment variable: |
| 34 | + |
| 35 | +```shell |
| 36 | +export GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id" |
6 | 37 | ``` |
7 | | -export GOOGLE_CLOUD_PROJECT={your-project} |
8 | 38 |
|
| 39 | +## Running in Google Kubernetes Engine |
| 40 | + |
| 41 | +To spin it up on your own GKE cluster, run the following: |
| 42 | +```shell |
9 | 43 | ./gradlew :examples-resource:jib --image="gcr.io/$GOOGLE_CLOUD_PROJECT/hello-resource-java" |
10 | 44 |
|
11 | 45 | sed s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \ |
12 | 46 | examples/resource/job.yaml | kubectl apply -f - |
13 | | -``` |
| 47 | +``` |
| 48 | + |
| 49 | +This will run the application as a GKE workload. You can view it from the `Workloads` tab under the `Resource Management` section on GKE console. |
| 50 | + |
| 51 | +The generated logs can be viewed under the `Logs` tab on the `Job Details` page. These logs will show the detected resource attributes for GKE. |
| 52 | + |
| 53 | +## Running the application locally |
| 54 | + |
| 55 | +> [!NOTE] |
| 56 | +> Resource attributes won't be detected in unsupported environments. You can find a list of environments supported by the GCP detector [here](https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/gcp-resources). |
| 57 | +
|
| 58 | +You can run the application locally as well: |
| 59 | + |
| 60 | +From the root of the repository, |
| 61 | +```shell |
| 62 | +cd examples/resource && gradle run |
| 63 | +``` |
| 64 | + |
| 65 | +The detected resource attributes would depend on the environment in which the application is run. |
0 commit comments