8000 Add PROJECT_ID (#1953) · mihroot/python-docs-samples@633d03c · GitHub
[go: up one dir, main page]

Skip to content

Commit 633d03c

Browse files
authored
Add PROJECT_ID (GoogleCloudPlatform#1953)
* Update requirements.txt * Adding some rate limiting * Add PROJECT_ID * Update main.py * Update main.py * Update main.py * Update main.py * Use standard replacement project ID in sample * Remove unneeded import * Back to using environment for PROJECT ID * Change how PROJECT_ID is injected * Had used wrong object * Update main.py * Update main.py * Update main.py
1 parent 683d6aa commit 633d03c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

trace/main.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import argparse
1516
import random
1617
import time
1718

1819
from flask import Flask, redirect, url_for
1920

21+
2022
# [START trace_setup_python_configure]
2123
from opencensus.trace.exporters import stackdriver_exporter
2224
import opencensus.trace.tracer
2325

24-
exporter = stackdriver_exporter.StackdriverExporter()
25-
tracer = opencensus.trace.tracer.Tracer(exporter=exporter)
26+
27+
def initialize_tracer(project_id):
28+
exporter = stackdriver_exporter.StackdriverExporter(
29+
project_id=project_id
30+
)
31+
tracer = opencensus.trace.tracer.Tracer(exporter=exporter)
32+
33+
return tracer
2634
# [END trace_setup_python_configure]
2735

36+
2837
app = Flask(__name__)
2938

3039

@@ -36,6 +45,7 @@ def root():
3645
# [START trace_setup_python_quickstart]
3746
@app.route('/index.html', methods=['GET'])
3847
def index():
48+
tracer = app.config['TRACER']
3949
tracer.start_span(name='index')
4050

4151
# Add up to 1 sec delay, weighted toward zero
@@ -48,4 +58,15 @@ def index():
4858

4959

5060
if __name__ == '__main__':
61+
parser = argparse.ArgumentParser(
62+
description=__doc__,
63+
formatter_class=argparse.RawDescriptionHelpFormatter
64+
)
65+
parser.add_argument(
66+
'--project_id', help='Project ID you want to access.', required=True)
67+
args = parser.parse_args()
68+
69+
tracer = initialize_tracer(args.project_id)
70+
app.config['TRACER'] = tracer
71+
5172
app.run()

trace/main_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
import main
1617

1718

1819
def test_index():
20+
project_id = os.environ['GCLOUD_PROJECT']
1921
main.app.testing = True
22+
main.app.config['TRACER'] = main.initialize_tracer(project_id)
2023
client = main.app.test_client()
2124

2225
resp = client.get('/index.html')
@@ -25,7 +28,9 @@ def test_index():
2528

2629

2730
def test_redirect():
31+
project_id = os.environ['GCLOUD_PROJECT']
2832
main.app.testing = True
33+
main.app.config['TRACER'] = main.initialize_tracer(project_id)
2934
client = main.app.test_client()
3035

3136
resp = 37EC client.get('/')

0 commit comments

Comments
 (0)
0