Skip to content

Commit

Permalink
Add script to run example in docker container (#229)
Browse files Browse the repository at this point in the history
This just adds a convenience script that allows users to run auto-instrument example in a local docker container instead of running on GKE.
  • Loading branch information
psx95 authored May 5, 2023
1 parent a64fdd0 commit 4cc2f4d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
44 changes: 41 additions & 3 deletions examples/autoinstrument/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
# Autoinstrumentation Example

An example spring webapp deployed and instrumented using the OpenTelemetry Java Auto-instrumentation agent deployed to GKE.
An example spring webapp deployed and instrumented using the OpenTelemetry Java Auto-instrumentation agent.

### Prerequisites

##### Get Google Cloud Credentials on your machine

```shell
gcloud auth application-default login
```
Executing 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:
```shell
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json
```

##### Export the Google Cloud Project ID to `GOOGLE_CLOUD_PROJECT` environment variable:

```shell
export GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id"
```

## Running in Google Kubernetes Engine

To spin it up on your own GKE cluster, run the following:
```bash
export GOOGLE_CLOUD_PROJECT={your-project}

./gradlew :examples-autoinstrument:jib --image="gcr.io/$GOOGLE_CLOUD_PROJECT/hello-autoinstrument-java"

sed s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \
Expand All @@ -26,3 +52,15 @@ Or, if you'd like to synthesize a parent trace:
```bash
curl -H "traceparent: 00-ff000000000000000000000000000041-ff00000000000041-01" ${cluster_ip}
```

## Running locally in a docker container

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.

A convenience script has been provided which will run the example in a docker container.

From the root of the repository,
```shell
cd examples/autoinstrument && ./run_in_docker.sh
```
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.
39 changes: 39 additions & 0 deletions examples/autoinstrument/run_in_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
#
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
if [[ -z "${GOOGLE_CLOUD_PROJECT}" ]]; then
echo "GOOGLE_CLOUD_PROJECT environment variable not set"
exit 1
fi

if [[ -z "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then
echo "GOOGLE_APPLICATION_CREDENTIALS environment variable not set"
exit 1
fi
echo "ENVIRONMENT VARIABLES VERIFIED"

echo "BUILDING SAMPLE APP IMAGE"
gradle clean jib --image "gcr.io/${GOOGLE_CLOUD_PROJECT}/hello-autoinstrument-java"


echo "RUNNING SAMPLE APP ON PORT 8080"
docker run \
--rm \
-e "GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT}" \
-e "GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS}" \
-v "${GOOGLE_APPLICATION_CREDENTIALS}:${GOOGLE_APPLICATION_CREDENTIALS}:ro" \
-p 8080:8080 \
"gcr.io/${GOOGLE_CLOUD_PROJECT}/hello-autoinstrument-java"

0 comments on commit 4cc2f4d

Please sign in to comment.