An example application that shows what resource attributes will be detected by the GCP Resource Detector and how the Autoconfigure Resource Provider SPI automatically attaches the detected resource attributes to the generated telemetry.
For users looking to use the GCP Resource Detector with OpenTelemetry Java Agent for automatic instrumentation, refer to the instructions for enabling resource providers that are disabled by default.
gcloud auth application-default loginExecuting this command will save your application credentials to default path which will depend on the type of machine -
- Linux, macOS:
$HOME/.config/gcloud/application_default_credentials.json - Windows:
%APPDATA%\gcloud\application_default_credentials.json
NOTE: This method of authentication is not recommended for production environments.
Next, export the credentials to GOOGLE_APPLICATION_CREDENTIALS environment variable -
For Linux & MacOS:
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.jsonFor Windows:
SET GOOGLE_APPLICATION_CREDENTIALS=%APPDATA%\gcloud\application_default_credentials.jsonexport GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id"Note
This step is only required if you want to run the application in GKE
export ARTIFACT_REGISTRY="my-container-registry"
export REGISTRY_LOCATION="us-central1"
gcloud artifacts repositories create ${ARTIFACT_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="Resource detection sample on GKE"Note
Make sure that kubectl is installed and configured to access a GKE cluster. Relevant documentation can be found in the GKE cluster access guide.
To spin it up on your own GKE cluster, run the following:
./gradlew :opentelemetry-examples-resource-detection-gcp:jib --image="$REGISTRY_LOCATION-docker.pkg.dev/$GOOGLE_CLOUD_PROJECT/$ARTIFACT_REGISTRY/hello-resource-java"
sed -e s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \
-e s/%REGISTRY_LOCATION%/$REGISTRY_LOCATION/g \
-e s/%ARTIFACT_REGISTRY%/$ARTIFACT_REGISTRY/g \
resource-detection-gcp/k8s/job.yaml | kubectl apply -f -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.
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.
The detected attributes will contain the attributes detected by the GCP resource detector as well as the attributes attached by the autoconfigure module.
Note
Resource attributes won't be detected in unsupported environments. You can find a list of supported environments in the GCP detector documentation.
You can run the application locally as well:
From the root of the repository,
cd resource-detection-gcp && gradle runThe detected resource attributes would depend on the environment in which the application is run.
The example produces a single span and uses logging OTLP JSON exporter to export the produced span. After running the example successfully, you should see the emitted span in JSON form via JUL. It should look something like:
INFO: {"resource":{"attributes": ... }}
Cleanup any Google Cloud Resources created to run this sample.
# Delete the Job
kubectl delete job hello-resource-java
# Delete Artifact Registry
gcloud artifacts repositories delete $ARTIFACT_REGISTRY --location=$REGISTRY_LOCATION