Skip to content

Commit f8062b0

Browse files
authored
add flask example and region tags (#128)
* add region tags to advanced_trace.py * flask e2e example with region tags * remove region tags from client
1 parent ec148c7 commit f8062b0

6 files changed

Lines changed: 257 additions & 0 deletions

File tree

docs/examples/cloud_trace_exporter/advanced_trace.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
import random
1616
import time
1717

18+
# [START opentelemetry_trace_import]
19+
1820
from opentelemetry import trace
1921
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
2022
from opentelemetry.sdk.trace import TracerProvider
2123
from opentelemetry.sdk.trace.export import BatchSpanProcessor
2224
from opentelemetry.trace import Link
2325

26+
# [END opentelemetry_trace_import]
27+
28+
# [START opentelemetry_setup_exporter]
29+
2430
tracer_provider = TracerProvider()
2531
cloud_trace_exporter = CloudTraceSpanExporter()
2632
tracer_provider.add_span_processor(
@@ -33,11 +39,15 @@
3339

3440
tracer = trace.get_tracer(__name__)
3541

42+
# [END opentelemetry_setup_exporter]
43+
3644

3745
def do_work() -> None:
3846
time.sleep(random.random() * 0.5)
3947

4048

49+
# [START opentelemetry_trace_custom_span]
50+
4151
with tracer.start_span("foo_with_attribute") as current_span:
4252
do_work()
4353

@@ -47,12 +57,19 @@ def do_work() -> None:
4757
current_span.set_attribute("int_attribute", 3)
4858
current_span.set_attribute("float_attribute", 3.14)
4959

60+
# [END opentelemetry_trace_custom_span]
61+
62+
# [START opentelemetry_trace_custom_span_events]
5063

5164
# Adding events to spans
5265
with tracer.start_as_current_span("foo_with_event") as current_span:
5366
do_work()
5467
current_span.add_event(name="event_name")
5568

69+
# [END opentelemetry_trace_custom_span_events]
70+
71+
# [START opentelemetry_trace_custom_span_links]
72+
5673
# Adding links to spans
5774
with tracer.start_as_current_span("link_target") as link_target:
5875
# Using start_as_current_span() instead of start_span() will make spans
@@ -73,3 +90,5 @@ def do_work() -> None:
7390
links=[Link(link_target.context, attributes={"link_attr": "string"})],
7491
):
7592
do_work()
93+
94+
# [END opentelemetry_trace_custom_span_links]

docs/examples/flask_e2e/README.rst

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
=============================
2+
End-to-End Example with Flask
3+
=============================
4+
5+
This end-to-end example shows how to instrument a Flask app with
6+
OpenTelemetry to send traces to Cloud Trace. In addition, there is a client
7+
script that uses ``requests`` to call the Flask app and propagate context with
8+
the GCP context propagator.
9+
10+
To run this example you first need to:
11+
* Create a Google Cloud project. You can `create one here <https://console.cloud.google.com/projectcreate>`_.
12+
* Enable Cloud Trace API (listed in the Cloud Console as Stackdriver Trace API) in the project `here <https://console.cloud.google.com/apis/library?q=cloud%20trace&filter=visibility:public>`_. If the page says "API Enabled" then you're done! No need to do anything.
13+
* Enable Default Application Credentials by creating setting `GOOGLE_APPLICATION_CREDENTIALS <https://cloud.google.com/docs/authentication/getting-started>`_ or by `installing gcloud sdk <https://cloud.google.com/sdk/install>`_ and calling ``gcloud auth application-default login``.
14+
15+
It is also recommended to create a fresh virtualenv for running this example:
16+
17+
.. code-block:: sh
18+
19+
python3 -m venv venv
20+
source venv/bin/activate
21+
22+
Flask Server
23+
============
24+
25+
26+
Install Dependencies
27+
--------------------
28+
29+
.. code-block:: sh
30+
31+
pip install opentelemetry-exporter-gcp-trace \
32+
opentelemetry-propagator-gcp \
33+
opentelemetry-api \
34+
opentelemetry-sdk \
35+
flask \
36+
requests \
37+
opentelemetry-instrumentation-requests \
38+
opentelemetry-instrumentation-flask
39+
40+
41+
Write the Flask Server
42+
----------------------
43+
44+
.. literalinclude:: server.py
45+
:language: python
46+
:lines: 1-
47+
48+
Write the Client
49+
----------------
50+
51+
.. literalinclude:: client.py
52+
:language: python
53+
:lines: 1-
54+
55+
Run
56+
---
57+
58+
In one terminal, start the flask app:
59+
60+
.. code-block:: sh
61+
62+
FLASK_APP=server.py flask run -p 6000
63+
64+
In another terminal, run the client:
65+
66+
.. code-block:: sh
67+
68+
python client.py
69+
70+
Checking Output
71+
---------------
72+
73+
After running any of these examples, you can go to `Cloud Trace overview
74+
<https://console.cloud.google.com/traces/list>`_ to see the results. You
75+
should see something like the image below with a root span covering the whole
76+
client request and a child span covering the Flask server processing the
77+
request.
78+
79+
.. image:: gct_result.png
80+
:alt: GCT result
81+
82+
83+
Further Reading
84+
---------------
85+
86+
* `More information about exporters in general <https://opentelemetry-python.readthedocs.io/en/stable/getting-started.html#configure-exporters-to-emit-spans-elsewhere>`_

docs/examples/flask_e2e/client.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2021 The OpenTelemetry Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import requests
17+
from opentelemetry import trace
18+
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
19+
from opentelemetry.instrumentation.requests import RequestsInstrumentor
20+
from opentelemetry.propagate import set_global_textmap
21+
from opentelemetry.propagators.cloud_trace_propagator import (
22+
CloudTraceFormatPropagator,
23+
)
24+
from opentelemetry.sdk.trace import TracerProvider
25+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
26+
27+
set_global_textmap(CloudTraceFormatPropagator())
28+
29+
tracer_provider = TracerProvider()
30+
cloud_trace_exporter = CloudTraceSpanExporter()
31+
tracer_provider.add_span_processor(
32+
# BatchSpanProcessor buffers spans and sends them in batches in a
33+
# background thread. The default parameters are sensible, but can be
34+
# tweaked to optimize your performance
35+
BatchSpanProcessor(cloud_trace_exporter)
36+
)
37+
trace.set_tracer_provider(tracer_provider)
38+
39+
tracer = trace.get_tracer(__name__)
40+
41+
RequestsInstrumentor().instrument()
42+
43+
res = requests.get("http://localhost:6000")
44+
print(res.text)
266 KB
Loading
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
asgiref==3.3.4
2+
cachetools==4.2.1
3+
certifi==2020.12.5
4+
chardet==4.0.0
5+
click==7.1.2
6+
Flask==1.1.2
7+
google-api-core==1.26.3
8+
google-auth==1.28.0
9+
google-cloud-core==1.6.0
10+
google-cloud-trace==0.24.0
11+
googleapis-common-protos==1.53.0
12+
grpcio==1.37.0
13+
idna==2.10
14+
itsdangerous==1.1.0
15+
Jinja2==2.11.3
16+
MarkupSafe==1.1.1
17+
opentelemetry-api==1.0.0
18+
../../../opentelemetry-exporter-gcp-trace
19+
../../../opentelemetry-propagator-gcp
20+
opentelemetry-instrumentation==0.19b0
21+
opentelemetry-instrumentation-flask==0.19b0
22+
opentelemetry-instrumentation-requests==0.19b0
23+
opentelemetry-instrumentation-wsgi==0.19b0
24+
opentelemetry-sdk==1.0.0
25+
opentelemetry-util-http==0.19b0
26+
packaging==20.9
27+
protobuf==3.15.8
28+
pyasn1==0.4.8
29+
pyasn1-modules==0.2.8
30+
pyparsing==2.4.7
31+
pytz==2021.1
32+
requests==2.25.1
33+
rsa==4.7.2
34+
six==1.15.0
35+
urllib3==1.26.4
36+
Werkzeug==1.0.1
37+
wrapt==1.12.1

docs/examples/flask_e2e/server.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2021 The OpenTelemetry Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# [START opentelemetry_flask_import]
17+
18+
import time
19+
20+
from flask import Flask
21+
from opentelemetry import trace
22+
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
23+
from opentelemetry.instrumentation.flask import FlaskInstrumentor
24+
from opentelemetry.propagate import set_global_textmap
25+
from opentelemetry.propagators.cloud_trace_propagator import (
26+
CloudTraceFormatPropagator,
27+
)
28+
from opentelemetry.sdk.trace import TracerProvider
29+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
30+
31+
# [END opentelemetry_flask_import]
32+
33+
# [START opentelemetry_flask_setup_propagator]
34+
35+
set_global_textmap(CloudTraceFormatPropagator())
36+
37+
# [END opentelemetry_flask_setup_propagator]
38+
39+
# [START opentelemetry_flask_setup_exporter]
40+
41+
tracer_provider = TracerProvider()
42+
cloud_trace_exporter = CloudTraceSpanExporter()
43+
tracer_provider.add_span_processor(
44+
# BatchSpanProcessor buffers spans and sends them in batches in a
45+
# background thread. The default parameters are sensible, but can be
46+
# tweaked to optimize your performance
47+
BatchSpanProcessor(cloud_trace_exporter)
48+
)
49+
trace.set_tracer_provider(tracer_provider)
50+
51+
tracer = trace.get_tracer(__name__)
52+
53+
# [END opentelemetry_flask_setup_exporter]
54+
55+
# [START opentelemetry_flask_instrument]
56+
57+
app = Flask(__name__)
58+
FlaskInstrumentor().instrument_app(app)
59+
60+
61+
@app.route("/")
62+
def hello_world():
63+
# You can still use the OpenTelemetry API as usual to create custom spans
64+
# within your trace
65+
with tracer.start_as_current_span("do_work"):
66+
time.sleep(0.1)
67+
68+
return "Hello, World!"
69+
70+
71+
# [END opentelemetry_flask_instrument]

0 commit comments

Comments
 (0)