Skip to content

Commit 8d951ee

Browse files
toumorokoshic24t
andauthored
Feature/getting started (open-telemetry#494)
As a requirement for the opentelemetry beta, providing a documentation page that runs through the use of traces, metrics, and common exporters. In the future, the code snippets in the page will be reconciled to use existing snippets, after some significant refactors around documentation is merged in. Fixes open-telemetry#491 Co-authored-by: Chris Kleinknecht <libc@google.com>
1 parent 264e6c3 commit 8d951ee

File tree

6 files changed

+486
-50
lines changed

6 files changed

+486
-50
lines changed

docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/flask_example.py

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,31 @@
1717
the requests library to perform downstream requests
1818
"""
1919
import flask
20-
import pkg_resources
2120
import requests
2221

2322
import opentelemetry.ext.http_requests
2423
from opentelemetry import trace
2524
from opentelemetry.ext.flask import instrument_app
2625
from opentelemetry.sdk.trace import TracerProvider
26+
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
27+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
2728

28-
29-
def configure_opentelemetry(flask_app: flask.Flask):
30-
"""Configure a flask application to use OpenTelemetry.
31-
32-
This activates the specific components:
33-
34-
* sets tracer to the SDK's Tracer
35-
* enables requests integration on the Tracer
36-
* uses a WSGI middleware to enable configuration
37-
"""
38-
# Start by configuring all objects required to ensure a complete end to end
39-
# workflow.
40-
trace.set_tracer_provider(TracerProvider())
41-
42-
# Next, we need to configure how the values that are used by traces and
43-
# metrics are propagated (such as what specific headers carry this value).
44-
# Integrations are the glue that binds the OpenTelemetry API and the
45-
# frameworks and libraries that are used together, automatically creating
46-
# Spans and propagating context as appropriate.
47-
opentelemetry.ext.http_requests.enable(trace.get_tracer_provider())
48-
instrument_app(flask_app)
49-
29+
trace.set_tracer_provider(TracerProvider())
30+
trace.get_tracer_provider().add_span_processor(
31+
SimpleExportSpanProcessor(ConsoleSpanExporter())
32+
)
5033

5134
app = flask.Flask(__name__)
35+
opentelemetry.ext.http_requests.enable(trace.get_tracer_provider())
36+
instrument_app(app)
5237

5338

5439
@app.route("/")
5540
def hello():
56-
# Emit a trace that measures how long the sleep takes
57-
version = pkg_resources.get_distribution(
58-
"opentelemetry-example-app"
59-
).version
60-
tracer = trace.get_tracer(__name__, version)
41+
tracer = trace.get_tracer(__name__)
6142
with tracer.start_as_current_span("example-request"):
6243
requests.get("http://www.example.com")
6344
return "hello"
6445

6546

66-
configure_opentelemetry(app)
47+
app.run(debug=True)

0 commit comments

Comments
 (0)