Skip to content

Commit 28e212d

Browse files
aravinsivabusunkim96gguuss
authored
Trace python opentelemetry (#4026)
Adds new directory for opentelemetry version of quickstart with port of python trace sample docs to opentelemetry Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Co-authored-by: Gus Class <gguuss@gmail.com>
1 parent 22ba0ce commit 28e212d

File tree

6 files changed

+255
-0
lines changed

6 files changed

+255
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Stackdriver Trace Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=trace/README.rst
8+
9+
10+
This directory contains samples for Stackdriver Trace. `Stackdriver Trace`_ collects latency data from applications and displays it in near real time in the Google Cloud Platform Console.
11+
12+
13+
14+
15+
.. _Stackdriver Trace: https://cloud.google.com/trace/docs
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications (only for authentication).
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples:
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. Change directory to the sample directory you want to use:
41+
42+
.. code-block:: bash
43+
44+
$ cd python-docs-samples/trace/trace-python-sample-opentelemetry
45+
46+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions:
47+
48+
.. _Python Development Environment Setup Guide:
49+
https://cloud.google.com/python/setup
50+
51+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+:
52+
53+
.. code-block:: bash
54+
55+
$ virtualenv env
56+
$ source env/bin/activate
57+
58+
#. Install the dependencies needed to run the samples:
59+
60+
.. code-block:: bash
61+
62+
$ pip install -r requirements.txt
63+
64+
.. _pip: https://pip.pypa.io/
65+
.. _virtualenv: https://virtualenv.pypa.io/
66+
67+
Samples
68+
-------------------------------------------------------------------------------
69+
70+
Web Server
71+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
72+
73+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
74+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=trace/main.py,trace/README.rst
75+
76+
77+
78+
79+
To run this sample:
80+
81+
.. code-block:: bash
82+
83+
$ python main.py
84+
85+
86+
87+
88+
The client library
89+
-------------------------------------------------------------------------------
90+
91+
This sample uses the `Google Cloud Client Library for Python`_.
92+
You can read the documentation for more details on API usage and use GitHub
93+
to `browse the source`_ and `report issues`_.
94+
95+
.. _Google Cloud Client Library for Python:
96+
https://googlecloudplatform.github.io/google-cloud-python/
97+
.. _browse the source:
98+
https://github.com/GoogleCloudPlatform/google-cloud-python
99+
.. _report issues:
100+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
101+
102+
103+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Stackdriver Trace
5+
short_name: Stackdriver Trace
6+
url: https://cloud.google.com/trace/docs
7+
description: >
8+
`Stackdriver Trace`_ collects latency data from applications and displays
9+
it in near real time in the Google Cloud Platform Console.
10+
11+
setup:
12+
- auth
13+
- install_deps
14+
15+
samples:
16+
- name: Web Server
17+
file: main.py
18+
19+
cloud_client_library: true
20+
21+
folder: trace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import argparse
16+
import os
17+
import random
18+
import time
19+
20+
from flask import Flask, redirect, url_for
21+
22+
# [START trace_setup_python_configure]
23+
from opentelemetry import trace
24+
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
25+
from opentelemetry.sdk.trace import TracerProvider
26+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
27+
28+
29+
def initialize_tracer(project_id):
30+
trace.set_tracer_provider(TracerProvider())
31+
cloud_trace_exporter = CloudTraceSpanExporter(project_id)
32+
trace.get_tracer_provider().add_span_processor(
33+
SimpleExportSpanProcessor(cloud_trace_exporter)
34+
)
35+
opentelemetry_tracer = trace.get_tracer(__name__)
36+
37+
return opentelemetry_tracer
38+
39+
40+
# [END trace_setup_python_configure]
41+
42+
app = Flask(__name__)
43+
44+
45+
@app.route("/", methods=["GET"])
46+
def root():
47+
return redirect(url_for("index"))
48+
49+
50+
# [START trace_setup_python_quickstart]
51+
@app.route("/index.html", methods=["GET"])
52+
def index():
53+
tracer = app.config["TRACER"]
54+
tracer.start_as_current_span(name="index")
55+
# Add up to 1 sec delay, weighted toward zero
56+
time.sleep(random.random() ** 2)
57+
result = "Tracing requests"
58+
59+
return result
60+
61+
62+
# [END trace_setup_python_quickstart]
63+
64+
65+
if __name__ == "__main__":
66+
parser = argparse.ArgumentParser(
67+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
68+
)
69+
parser.add_argument(
70+
"--project_id",
71+
help="Project ID you want to access.",
72+
default=os.environ["GOOGLE_CLOUD_PROJECT"],
73+
required=True,
74+
)
75+
args = parser.parse_args()
76+
77+
tracer = initialize_tracer(args.project_id)
78+
app.config["TRACER"] = tracer
79+
80+
app.run()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import main
18+
19+
20+
def test_index():
21+
project_id = os.environ['GOOGLE_CLOUD_PROJECT']
22+
main.app.testing = True
23+
main.app.config['TRACER'] = main.initialize_tracer(project_id)
24+
client = main.app.test_client()
25+
26+
resp = client.get('/index.html')
27+
assert resp.status_code == 200
28+
assert 'Tracing requests' in resp.data.decode('utf-8')
29+
30+
31+
def test_redirect():
32+
project_id = os.environ['GOOGLE_CLOUD_PROJECT']
33+
main.app.testing = True
34+
main.app.config['TRACER'] = main.initialize_tracer(project_id)
35+
client = main.app.test_client()
36+
37+
resp = client.get('/')
38+
assert resp.status_code == 302
39+
assert '/index.html' in resp.headers.get('location', '')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==5.3.2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Flask==1.1.2
2+
google-cloud-trace==0.23.0
3+
opentelemetry-api==0.9b0
4+
opentelemetry-auto-instrumentation==0.8b0
5+
opentelemetry-exporter-cloud-trace==0.9b0
6+
opentelemetry-ext-flask==0.8b0
7+
opentelemetry-ext-grpc==0.9b0
8+
opentelemetry-ext-jaeger==0.8b0
9+
opentelemetry-ext-requests==0.8b0
10+
opentelemetry-ext-wsgi==0.8b0
11+
opentelemetry-sdk==0.9b0

0 commit comments

Comments
 (0)