|
| 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 |
0 commit comments