-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable metrics-exporter example to run as cloud run job (#249)
* Add readme with steps to run the example * Add script to run example as Cloud Run Job * Replace GCR with Artifact Registry * Rename GOOGLE_CLOUD_RUN_JOB_REGION to GOOGLE_CLOUD_RUN_REGION
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
### Running the example to export metrics to Google Cloud | ||
|
||
You can run this example to generate sample metrics on any Google Cloud project. | ||
|
||
#### 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` | ||
|
||
##### Export the retrieved credentials to `GOOGLE_APPLICATION_CREDENTIALS` environment variable. | ||
|
||
```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 the example | ||
|
||
#### Run the example locally | ||
|
||
You can run the example application via gradle. From the project root: | ||
|
||
```shell | ||
cd examples/metrics/ && gradle run | ||
``` | ||
|
||
#### Run the example as a Cloud Run Job | ||
|
||
You can run the example application as a Google Cloud Run Job. A convenience script has been provided for this. | ||
|
||
First, export your preferred Google cloud region where you want to create and run the job: | ||
|
||
```shell | ||
# This can be any valid Google Cloud Region | ||
export GOOGLE_CLOUD_RUN_REGION="us-central1" | ||
``` | ||
|
||
Then, from the project root: | ||
|
||
```shell | ||
cd examples/metrics/ && ./run_as_cloud-run-job.sh | ||
``` | ||
|
||
*Note: When using the convenience script, it will create a Google Cloud Artifact Registry named `cloud-run-applications` in your | ||
selected project.* | ||
|
||
You should now see the exported metrics in your Google Cloud project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/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. | ||
# | ||
CONTAINER_REGISTRY=cloud-run-applications | ||
REGISTRY_LOCATION=us-central1 | ||
|
||
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 | ||
|
||
if [[ -z "${GOOGLE_CLOUD_RUN_REGION}" ]]; then | ||
echo "GOOGLE_CLOUD_RUN_REGION environment variable not set" | ||
exit 1 | ||
fi | ||
|
||
echo "ENVIRONMENT VARIABLES VERIFIED" | ||
|
||
echo "CREATING CLOUD ARTIFACT REPOSITORY" | ||
gcloud artifacts repositories create ${CONTAINER_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="Sample applications to run on Google Cloud Run" | ||
echo "CREATED ${CONTAINER_REGISTRY} in ${REGISTRY_LOCATION}" | ||
|
||
echo "BUILDING SAMPLE APP IMAGE" | ||
gradle clean jib --image "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/metrics-export-java" | ||
|
||
echo "CREATING A CLOUD RUN JOB TO RUN THE CONTAINER" | ||
gcloud run jobs create job-metrics-export \ | ||
--image "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/metrics-export-java" \ | ||
--max-retries 5 \ | ||
--region ${GOOGLE_CLOUD_RUN_REGION} \ | ||
--project="${GOOGLE_CLOUD_PROJECT}" | ||
|
||
echo "SETTING CLOUD RUN JOB REGION" | ||
gcloud config set run/region "${GOOGLE_CLOUD_RUN_REGION}" | ||
|
||
echo "RUNNING THE CREATED JOB" | ||
gcloud run jobs execute job-metrics-export |