Skip to content

Commit 6247c0e

Browse files
authored
Merge branch 'main' into issue_3172
2 parents fb7c5ec + 7e67d52 commit 6247c0e

File tree

5 files changed

+134
-7
lines changed

5 files changed

+134
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.db

docs/examples/opencensus-shim/README.rst

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,44 @@ Alternatively, you can install the Python dependencies separately:
4444
opentelemetry-api \
4545
opentelemetry-sdk \
4646
opentelemetry-exporter-jaeger \
47-
opentelemetry-opencensus-shim
47+
opentelemetry-opencensus-shim \
48+
opentelemetry-instrumentation-sqlite3 \
49+
opencensus \
50+
opencensus-ext-flask
4851
4952
5053
Run the Application
5154
-------------------
5255

53-
.. TODO implement the example
56+
Start the application in a terminal.
57+
58+
.. code-block:: sh
59+
60+
flask --app app run -h 0.0.0.0
61+
62+
Point your browser to the address printed out (probably http://127.0.0.1:5000). Alternatively, just use curl to trigger a request:
63+
64+
.. code-block:: sh
65+
66+
curl http://127.0.0.1:5000
5467
5568
Jaeger UI
5669
*********
5770

58-
Open the Jaeger UI in your browser at
59-
`<http://localhost:16686>`_ and view traces for the
60-
"OpenCensus Shim Example" service.
71+
Open the Jaeger UI in your browser at `<http://localhost:16686>`_ and view traces for the
72+
"opencensus-shim-example-flask" service. Click on a span named "span" in the scatter plot. You
73+
will see a span tree with the following structure:
74+
75+
* ``span``
76+
* ``query movies from db``
77+
* ``SELECT``
78+
* ``build response html``
6179

62-
Note that tags and logs (OpenCensus) and attributes and events (OpenTelemetry)
63-
from both tracing systems appear in the exported trace.
80+
The root span comes from OpenCensus Flask instrumentation. The children ``query movies from
81+
db`` and ``build response html`` come from the manual instrumentation using OpenTelemetry's
82+
:meth:`opentelemetry.trace.Tracer.start_as_current_span`. Finally, the ``SELECT`` span is
83+
created by OpenTelemetry's SQLite3 instrumentation. Everything is exported to Jaeger using the
84+
OpenTelemetry exporter.
6485

6586
Useful links
6687
------------
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Copyright The OpenTelemetry Authors
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 sqlite3
16+
17+
from flask import Flask
18+
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
19+
20+
from opentelemetry import trace
21+
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
22+
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
23+
from opentelemetry.sdk.resources import Resource
24+
from opentelemetry.sdk.trace import TracerProvider
25+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
26+
from opentelemetry.shim.opencensus import install_shim
27+
28+
DB = "example.db"
29+
30+
# Set up OpenTelemetry
31+
tracer_provider = TracerProvider(
32+
resource=Resource(
33+
{
34+
"service.name": "opencensus-shim-example-flask",
35+
}
36+
)
37+
)
38+
trace.set_tracer_provider(tracer_provider)
39+
40+
# Configure OTel to export traces to Jaeger
41+
tracer_provider.add_span_processor(
42+
BatchSpanProcessor(
43+
JaegerExporter(
44+
agent_host_name="localhost",
45+
agent_port=6831,
46+
)
47+
)
48+
)
49+
tracer = tracer_provider.get_tracer(__name__)
50+
51+
# Install the shim to start bridging spans from OpenCensus to OpenTelemetry
52+
install_shim()
53+
54+
# Instrument sqlite3 library
55+
SQLite3Instrumentor().instrument()
56+
57+
# Setup Flask with OpenCensus instrumentation
58+
app = Flask(__name__)
59+
FlaskMiddleware(app)
60+
61+
62+
# Setup the application database
63+
def setup_db():
64+
with sqlite3.connect(DB) as con:
65+
cur = con.cursor()
66+
cur.execute(
67+
"""
68+
CREATE TABLE IF NOT EXISTS movie(
69+
title,
70+
year,
71+
PRIMARY KEY(title, year)
72+
)
73+
"""
74+
)
75+
cur.execute(
76+
"""
77+
INSERT OR IGNORE INTO movie(title, year) VALUES
78+
('Mission Telemetry', 2000),
79+
('Observing the World', 2010),
80+
('The Tracer', 1999),
81+
('The Instrument', 2020)
82+
"""
83+
)
84+
85+
86+
setup_db()
87+
88+
89+
@app.route("/")
90+
def hello_world():
91+
lines = []
92+
with tracer.start_as_current_span("query movies from db"), sqlite3.connect(
93+
DB
94+
) as con:
95+
cur = con.cursor()
96+
for title, year in cur.execute("SELECT title, year from movie"):
97+
lines.append(f"<li>{title} is from the year {year}</li>")
98+
99+
with tracer.start_as_current_span("build response html"):
100+
html = f"<ul>{''.join(lines)}</ul>"
101+
102+
return html

docs/examples/opencensus-shim/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ opentelemetry-api
22
opentelemetry-sdk
33
opentelemetry-exporter-jaeger
44
opentelemetry-opencensus-shim
5+
opentelemetry-instrumentation-sqlite3
6+
opencensus
7+
opencensus-ext-flask
File renamed without changes.

0 commit comments

Comments
 (0)