8000 Update logging for cloud run sample (#2299) · crowdus/python-docs-samples@d2b2619 · GitHub
[go: up one dir, main page]

Skip to content

Commit d2b2619

Browse files
authored
Update logging for cloud run sample (GoogleCloudPlatform#2299)
* Update readmes and logging for cloud run sample * Update tests * Revert changes and add flush * Update gcloud commands * Update docker run for cred * Add link with more info
1 parent 2ad7af4 commit d2b2619

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

run/README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Sa
3636
2. [Build the sample container](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker):
3737
3838
```
39-
export SAMPLE=$sample
39+
export SAMPLE=<SAMPLE_NAME>
4040
cd $SAMPLE
41-
docker build --tag $sample .
41+
docker build --tag $SAMPLE .
4242
```
4343
4444
3. [Run containers locally](https://cloud.google.com/run/docs/testing/local)
@@ -57,25 +57,44 @@ For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Sa
5757
-v $PWD:/app $SAMPLE
5858
```
5959
60-
Injecting your service account key:
60+
Injecting your service account key for access to GCP services:
6161
6262
```
63+
# Set the name of the service account key within the container
6364
export SA_KEY_NAME=my-key-name-123
65+
6466
PORT=8080 && docker run --rm \
65-
-p 8080:${PORT} -e PORT=${PORT} \
67+
-p 8080:${PORT} \
68+
-e PORT=${PORT} \
6669
-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \
6770
-v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \
687 8000 1
-v $PWD:/app $SAMPLE
6972
```
7073
74+
* Use the --volume (-v) flag to inject the credential file into the container
75+
(assumes you have already set your `GOOGLE_APPLICATION_CREDENTIALS`
76+
environment variable on your machine)
77+
78+
* Use the --environment (-e) flag to set the `GOOGLE_APPLICATION_CREDENTIALS`
79+
variable inside the container
80+
81+
Learn more about [testing your container image locally.][testing]
82+
7183
## Deploying
7284
85+
1. Set an environment variable with your GCP Project ID
86+
```
87+
export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
88+
```
89+
90+
1. Submit a build using Google Cloud Build
7391
```
7492
gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
75-
gcloud beta run deploy $SAMPLE \
76-
# Needed for Manual Logging sample.< 8000 /span>
77-
--set-env-var GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT}
78-
--image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
93+
```
94+
95+
1. Deploy to Cloud Run
96+
```
97+
gcloud beta run deploy $SAMPLE --image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
7998
```
8099
81100
See [Building containers][run_build] and [Deploying container images][run_deploy]
@@ -88,3 +107,4 @@ for more information.
88107
[pubsub]: pubsub/
89108
[run_button_helloworld]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/knative/docs&cloudshell_working_dir=docs/serving/samples/hello-world/helloworld-python
90109
[run_button_pubsub]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/pubsub
110+
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services

run/pubsub/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ _Note: you may need to install `pytest` using `pip install pytest`._
3232
## Deploy
3333

3434
```
35+
# Set an environment variable with your GCP Project ID
36+
export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
37+
38+
# Submit a build using Google Cloud Build
3539
gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/pubsub-tutorial
36-
gcloud alpha run deploy pubsub-tutorial --image gcr.io/${GOOGLE_CLOUD_PROJECT}/pubsub-tutorial
40+
41+
# Deploy to Cloud Run
42+
gcloud beta run deploy pubsub-tutorial --image gcr.io/${GOOGLE_CLOUD_PROJECT}/pubsub-tutorial
3743
```

run/pubsub/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import base64
1717
from flask import Flask, request
1818
import os
19+
import sys
1920

2021
app = Flask(__name__)
2122
# [END run_pubsub_server_setup]
@@ -42,6 +43,10 @@ def index():
4243
name = base64.b64decode(pubsub_message['data']).decode('utf-8').strip()
4344

4445
print(f'Hello {name}!')
46+
47+
# Flush the stdout to avoid log buffering.
48+
sys.stdout.flush()
49+
4550
return ('', 204)
4651
# [END run_pubsub_handler]
4752

0 commit comments

Comments
 (0)
0