|
17 | 17 | the requests library to perform downstream requests
|
18 | 18 | """
|
19 | 19 | import flask
|
20 |
| -import pkg_resources |
21 | 20 | import requests
|
22 | 21 |
|
23 | 22 | import opentelemetry.ext.http_requests
|
24 | 23 | from opentelemetry import trace
|
25 | 24 | from opentelemetry.ext.flask import instrument_app
|
26 | 25 | from opentelemetry.sdk.trace import TracerProvider
|
| 26 | +from opentelemetry.sdk.trace.export import ConsoleSpanExporter |
| 27 | +from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor |
27 | 28 |
|
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 | +) |
50 | 33 |
|
51 | 34 | app = flask.Flask(__name__)
|
| 35 | +opentelemetry.ext.http_requests.enable(trace.get_tracer_provider()) |
| 36 | +instrument_app(app) |
52 | 37 |
|
53 | 38 |
|
54 | 39 | @app.route("/")
|
55 | 40 | 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__) |
61 | 42 | with tracer.start_as_current_span("example-request"):
|
62 | 43 | requests.get("http://www.example.com")
|
63 | 44 | return "hello"
|
64 | 45 |
|
65 | 46 |
|
66 |
| -configure_opentelemetry(app) |
| 47 | +app.run(debug=True) |
0 commit comments