Skip to content

Commit 03fe9f2

Browse files
authored
Update GCP resource detection sample (#561)
1 parent 297de76 commit 03fe9f2

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

resource-detection-gcp/README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,37 @@ SET GOOGLE_APPLICATION_CREDENTIALS=%APPDATA%\gcloud\application_default_credenti
3535
export GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id"
3636
```
3737

38+
#### Setup Google Cloud Artifact Registry
39+
40+
> [!NOTE]
41+
> This step is only required if you want to run the application in GKE
42+
43+
```shell
44+
export ARTIFACT_REGISTRY="my-container-registry"
45+
export REGISTRY_LOCATION="us-central1"
46+
gcloud artifacts repositories create ${ARTIFACT_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="Resource detection sample on GKE"
47+
```
48+
49+
3850
## Running in Google Kubernetes Engine
3951

52+
> [!NOTE]
53+
> Make sure that kubectl is installed and configured to access a GKE cluster. Relevant documentation can be found [here](https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#run_against_a_specific_cluster).
54+
4055
To spin it up on your own GKE cluster, run the following:
4156
```shell
42-
./gradlew :opentelemetry-examples-resource-detection-gcp:jib --image="gcr.io/$GOOGLE_CLOUD_PROJECT/hello-resource-java"
57+
./gradlew :opentelemetry-examples-resource-detection-gcp:jib --image="$REGISTRY_LOCATION-docker.pkg.dev/$GOOGLE_CLOUD_PROJECT/$ARTIFACT_REGISTRY/hello-resource-java"
4358

44-
sed s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \
59+
sed -e s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \
60+
-e s/%REGISTRY_LOCATION%/$REGISTRY_LOCATION/g \
61+
-e s/%ARTIFACT_REGISTRY%/$ARTIFACT_REGISTRY/g \
4562
resource-detection-gcp/k8s/job.yaml | kubectl apply -f -
4663
```
4764

4865
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.
4966

5067
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.
68+
The detected attributes will contain the attributes detected by the GCP resource detector as well as the attributes attached by the autoconfigure module.
5169

5270
## Running the application locally
5371

@@ -71,3 +89,15 @@ After running the example successfully, you should see the emitted span in JSON
7189
```
7290
INFO: {"resource":{"attributes": ... }}
7391
```
92+
93+
## Cleanup
94+
95+
Cleanup any Google Cloud Resources created to run this sample.
96+
97+
```shell
98+
# Delete the Job
99+
kubectl delete job hello-resource-java
100+
101+
# Delete Artifact Registry
102+
gcloud artifacts repositories delete $ARTIFACT_REGISTRY --location=$REGISTRY_LOCATION
103+
```

resource-detection-gcp/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dependencies {
3030
implementation("io.opentelemetry:opentelemetry-api")
3131
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
3232
implementation("io.opentelemetry:opentelemetry-exporter-logging-otlp")
33-
implementation("io.opentelemetry.contrib:opentelemetry-gcp-resources:1.34.0-alpha")
33+
implementation("io.opentelemetry.contrib:opentelemetry-gcp-resources:1.42.0-alpha")
3434
}
3535

3636
jib {

resource-detection-gcp/k8s/job.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ spec:
1212
spec:
1313
containers:
1414
- name: hello-resource-java
15-
image: gcr.io/%GOOGLE_CLOUD_PROJECT%/hello-resource-java:latest
15+
image: "%REGISTRY_LOCATION%-docker.pkg.dev/%GOOGLE_CLOUD_PROJECT%/%ARTIFACT_REGISTRY%/hello-resource-java:latest"
1616
env:
1717
- name: OTEL_TRACES_EXPORTER
1818
value: none
@@ -28,12 +28,14 @@ spec:
2828
valueFrom:
2929
fieldRef:
3030
fieldPath: metadata.name
31-
- name: NAMESPACE
31+
- name: NAMESPACE_NAME
3232
valueFrom:
3333
fieldRef:
3434
fieldPath: metadata.namespace
3535
- name: CONTAINER_NAME
3636
value: hello-resource-java
37+
- name: OTEL_RESOURCE_ATTRIBUTES
38+
value: k8s.pod.name=$(POD_NAME),k8s.namespace.name=$(NAMESPACE_NAME),k8s.container.name=$(CONTAINER_NAME)
3739
# Do not restart containers after they exit
3840
restartPolicy: Never
3941
# of retries before marking as failed.

0 commit comments

Comments
 (0)