Skip to content

Commit b0b88bf

Browse files
psx95dashpole
andauthored
Update auto-instrumentation sample (#397)
* Use OTLP for trace export * Fix otlp auth in cloud-run * Fix k8s deployment for sample * Update Cloud Run deployment to use no-throttling * Update examples/autoinstrument/deployment.yaml Co-authored-by: David Ashpole <[email protected]> --------- Co-authored-by: David Ashpole <[email protected]>
1 parent 744db58 commit b0b88bf

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

examples/autoinstrument/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ export GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id"
3232

3333
To spin it up on your own GKE cluster, run the following:
3434
```bash
35-
./gradlew :examples-autoinstrument:jib --image="gcr.io/$GOOGLE_CLOUD_PROJECT/hello-autoinstrument-java"
35+
# Create an artifact repository to upload the containerized application image.
36+
# Skip this step if you wish to re-use an existing artifact repository.
37+
gcloud artifacts repositories create opentelemetry-sample-apps --repository-format=docker --location=us-central1 --description="OpenTelemetry auto-instrumentation sample applications"
38+
39+
./gradlew :examples-autoinstrument:jib --image="us-central1-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/opentelemetry-sample-apps/hello-autoinstrument-java"
3640

3741
sed s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \
3842
examples/autoinstrument/deployment.yaml | kubectl apply -f -
3943

4044
kubectl expose deployment hello-autoinstrument-java --type LoadBalancer --port 80 --target-port 8080
4145
```
4246

43-
4447
This will expose the simple http server at port 80. You can try out the tracing instrumentation via:
4548

4649
```bash
@@ -53,6 +56,9 @@ Or, if you'd like to synthesize a parent trace:
5356
curl -H "traceparent: 00-ff000000000000000000000000000041-ff00000000000041-01" ${cluster_ip}
5457
```
5558

59+
> ![IMPORTANT]
60+
> Make sure that your cluster has the necessary permissions to write metrics and traces to your selected project.
61+
5662
## Running in Google Cloud Run
5763

5864
To run this example in Google Cloud Run, you need to run the convenience script provided. After following the prerequisites,

examples/autoinstrument/build.gradle

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ configurations {
2929
agent
3030
}
3131

32-
mainClassName = 'com.google.example.TestMain'
32+
// the shaded variant name for gcp auth extension is an empty string
33+
def gcp_auth_extension_shaded_classifier = ''
3334

3435
dependencies {
3536
agent agentLibraries.agent
3637
agent project(path: ':exporter-auto', configuration: 'shadow')
38+
agent "${libraries.opentelemetry_gcp_auth_extension}:${gcp_auth_extension_shaded_classifier}"
3739

3840
implementation(libraries.spring_boot_starter_web)
3941
implementation(libraries.spring_cloud_starter_openfeign)
@@ -46,7 +48,8 @@ task copyAgent(type: Copy) {
4648
into layout.buildDirectory.dir("otelagent")
4749
from configurations.agent {
4850
rename "opentelemetry-javaagent(.*).jar", "otel_agent.jar"
49-
rename "exporter-auto(.*).jar", "gcp_ext.jar"
51+
rename "exporter-auto(.*).jar", "gcp_exporter_ext.jar"
52+
rename "opentelemetry-gcp-auth-extension(.*).jar", "gcp_auth_ext.jar"
5053
}
5154
}
5255

@@ -67,15 +70,24 @@ jib {
6770
// Use the downloaded java agent.
6871
'-javaagent:/otelagent/otel_agent.jar',
6972
// Use the GCP exporter extensions.
70-
'-Dotel.javaagent.extensions=/otelagent/gcp_ext.jar',
73+
'-Dotel.javaagent.extensions=/otelagent/gcp_exporter_ext.jar,/otelagent/gcp_auth_ext.jar',
7174
// Configure auto instrumentation.
72-
'-Dotel.traces.exporter=google_cloud_trace',
75+
// Configure traces to be exported via OTLP exporters
76+
'-Dotel.traces.exporter=otlp',
77+
'-Dotel.exporter.otlp.endpoint=https://telemetry.googleapis.com',
78+
'-Dotel.exporter.otlp.traces.protocol=http/protobuf',
79+
// Configure metrics to be exported via Google cloud exporters
7380
'-Dotel.metrics.exporter=google_cloud_monitoring',
7481
'-Dotel.logs.exporter=none',
82+
'-Dotel.service.name=gcp-autoinstrument-spring-demo',
7583
'-Dotel.resource.providers.gcp.enabled=true'
7684
]
7785
}
7886

87+
application {
88+
mainClassName = 'com.google.example.TestMain'
89+
}
90+
7991
tasks.named('jib').configure {
8092
dependsOn copyAgent
8193
}

examples/autoinstrument/deployment.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@ spec:
2929
spec:
3030
containers:
3131
- name: hello-autoinstrument-java
32-
image: gcr.io/%GOOGLE_CLOUD_PROJECT%/hello-autoinstrument-java:latest
32+
image: us-central1-docker.pkg.dev/%GOOGLE_CLOUD_PROJECT%/opentelemetry-sample-apps/hello-autoinstrument-java:latest
3333
# required for resource detection in GKE environment
3434
env:
35-
- name: NAMESPACE
35+
- name: GOOGLE_CLOUD_PROJECT
36+
value: %GOOGLE_CLOUD_PROJECT%
37+
- name: POD_NAME
3638
valueFrom:
3739
fieldRef:
38-
fieldPath: metadata.namespace
39-
- name: CONTAINER_NAME
40+
fieldPath: metadata.name
41+
- name: NAMESPACE_NAME
4042
valueFrom:
4143
fieldRef:
42-
fieldPath: metadata.name
44+
fieldPath: metadata.namespace
45+
- name: CONTAINER_NAME
46+
value: hello-autoinstrument-java
47+
- name: OTEL_RESOURCE_ATTRIBUTES
48+
value: k8s.pod.name=$(POD_NAME),k8s.namespace.name=$(NAMESPACE_NAME),k8s.container.name=$(CONTAINER_NAME)

examples/autoinstrument/run_in_cloud-run.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
CONTAINER_REGISTRY=cloud-run-applications
17+
CONTAINER_REGISTRY=opentelemetry-sample-apps
1818
REGISTRY_LOCATION=us-central1
1919
IMAGE_NAME="${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java"
2020

@@ -36,13 +36,17 @@ fi
3636
echo "ENVIRONMENT VARIABLES VERIFIED"
3737

3838
echo "CREATING CLOUD ARTIFACT REPOSITORY"
39-
gcloud artifacts repositories create ${CONTAINER_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="Sample applications to run on Google Cloud Run"
39+
gcloud artifacts repositories create ${CONTAINER_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="OpenTelemetry auto-instrumentation sample applications"
4040
echo "CREATED ${CONTAINER_REGISTRY} in ${REGISTRY_LOCATION}"
4141

4242
echo "BUILDING SAMPLE APP IMAGE"
4343
gradle clean jib --image "${IMAGE_NAME}"
4444

4545
echo "RUNNING SAMPLE APP ON PORT 8080"
46+
# We use --no-cpu-throttling for the sample to ensure that traces can be exported in the background.
47+
# See https://cloud.google.com/sdk/gcloud/reference/run/deploy#--[no-]cpu-throttling for details.
4648
gcloud run deploy hello-autoinstrument-cloud-run \
49+
--set-env-vars="GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT}" \
4750
--image="${IMAGE_NAME}" \
48-
--region="${GOOGLE_CLOUD_RUN_REGION}"
51+
--region="${GOOGLE_CLOUD_RUN_REGION}" \
52+
--no-cpu-throttling

0 commit comments

Comments
 (0)