Skip to content

Commit 4cc2f4d

Browse files
authored
Add script to run example in docker container (#229)
This just adds a convenience script that allows users to run auto-instrument example in a local docker container instead of running on GKE.
1 parent a64fdd0 commit 4cc2f4d

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

examples/autoinstrument/README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
# Autoinstrumentation Example
22

3-
An example spring webapp deployed and instrumented using the OpenTelemetry Java Auto-instrumentation agent deployed to GKE.
3+
An example spring webapp deployed and instrumented using the OpenTelemetry Java Auto-instrumentation agent.
4+
5+
### Prerequisites
6+
7+
##### Get Google Cloud Credentials on your machine
8+
9+
```shell
10+
gcloud auth application-default login
11+
```
12+
Executing this command will save your application credentials to default path which will depend on the type of machine -
13+
- Linux, macOS: `$HOME/.config/gcloud/application_default_credentials.json`
14+
- Windows: `%APPDATA%\gcloud\application_default_credentials.json`
15+
16+
**NOTE: This method of authentication is not recommended for production environments.**
17+
18+
Next, export the credentials to `GOOGLE_APPLICATION_CREDENTIALS` environment variable -
19+
20+
For Linux & MacOS:
21+
```shell
22+
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json
23+
```
24+
25+
##### Export the Google Cloud Project ID to `GOOGLE_CLOUD_PROJECT` environment variable:
26+
27+
```shell
28+
export GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id"
29+
```
30+
31+
## Running in Google Kubernetes Engine
432

533
To spin it up on your own GKE cluster, run the following:
634
```bash
7-
export GOOGLE_CLOUD_PROJECT={your-project}
8-
935
./gradlew :examples-autoinstrument:jib --image="gcr.io/$GOOGLE_CLOUD_PROJECT/hello-autoinstrument-java"
1036

1137
sed s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \
@@ -26,3 +52,15 @@ Or, if you'd like to synthesize a parent trace:
2652
```bash
2753
curl -H "traceparent: 00-ff000000000000000000000000000041-ff00000000000041-01" ${cluster_ip}
2854
```
55+
56+
## Running locally in a docker container
57+
58+
In case you do not want to spin up your own GKE cluster, but still want telemetry to be published to Google Cloud, you can run the example in a docker container.
59+
60+
A convenience script has been provided which will run the example in a docker container.
61+
62+
From the root of the repository,
63+
```shell
64+
cd examples/autoinstrument && ./run_in_docker.sh
65+
```
66+
You can now interact with the sample spring example on **localhost:8080**. The metrics and traces from this example can be viewed in Google Cloud Console.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2023 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
if [[ -z "${GOOGLE_CLOUD_PROJECT}" ]]; then
18+
echo "GOOGLE_CLOUD_PROJECT environment variable not set"
19+
exit 1
20+
fi
21+
22+
if [[ -z "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then
23+
echo "GOOGLE_APPLICATION_CREDENTIALS environment variable not set"
24+
exit 1
25+
fi
26+
echo "ENVIRONMENT VARIABLES VERIFIED"
27+
28+
echo "BUILDING SAMPLE APP IMAGE"
29+
gradle clean jib --image "gcr.io/${GOOGLE_CLOUD_PROJECT}/hello-autoinstrument-java"
30+
31+
32+
echo "RUNNING SAMPLE APP ON PORT 8080"
33+
docker run \
34+
--rm \
35+
-e "GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT}" \
36+
-e "GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS}" \
37+
-v "${GOOGLE_APPLICATION_CREDENTIALS}:${GOOGLE_APPLICATION_CREDENTIALS}:ro" \
38+
-p 8080:8080 \
39+
"gcr.io/${GOOGLE_CLOUD_PROJECT}/hello-autoinstrument-java"

0 commit comments

Comments
 (0)