1
1
---
2
- date : ' 2021-05-07T21 :49:47.106Z '
2
+ date : ' 2021-08-30T16 :49:17.700Z '
3
3
docname : getting-started
4
4
images : {}
5
5
path : /getting-started
6
- title : " Getting Started"
7
- weight : 22
6
+ title : Getting Started
8
7
---
9
8
9
+ # Getting Started
10
+
10
11
This guide walks you through instrumenting a Python application with ` opentelemetry-python ` .
11
12
12
13
For more elaborate examples, see [ examples] ( https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/ ) .
@@ -38,8 +39,8 @@ The following example script emits a trace containing three named spans: “foo
38
39
from opentelemetry import trace
39
40
from opentelemetry.sdk.trace import TracerProvider
40
41
from opentelemetry.sdk.trace.export import (
41
- ConsoleSpanExporter,
42
42
BatchSpanProcessor,
43
+ ConsoleSpanExporter,
43
44
)
44
45
45
46
provider = TracerProvider()
@@ -54,9 +55,6 @@ with tracer.start_as_current_span("foo"):
54
55
with tracer.start_as_current_span("bar"):
55
56
with tracer.start_as_current_span("baz"):
56
57
print("Hello world from OpenTelemetry Python!")
57
-
58
- # Flush all ended spans that are yet to be written to stdout before process exit.
59
- provider.force_flush()
60
58
```
61
59
62
60
When you run the script you can see the traces printed to your console:
@@ -225,8 +223,8 @@ from opentelemetry.instrumentation.flask import FlaskInstrumentor
225
223
from opentelemetry.instrumentation.requests import RequestsInstrumentor
226
224
from opentelemetry.sdk.trace import TracerProvider
227
225
from opentelemetry.sdk.trace.export import (
228
- ConsoleSpanExporter,
229
226
BatchSpanProcessor,
227
+ ConsoleSpanExporter,
230
228
)
231
229
232
230
trace.set_tracer_provider(TracerProvider())
@@ -238,16 +236,17 @@ app = flask.Flask(__name__)
238
236
FlaskInstrumentor().instrument_app(app)
239
237
RequestsInstrumentor().instrument()
240
238
239
+ tracer = trace.get_tracer(__name__)
240
+
241
241
242
242
@app.route("/")
243
243
def hello():
244
- tracer = trace.get_tracer(__name__)
245
244
with tracer.start_as_current_span("example-request"):
246
245
requests.get("http://www.example.com")
247
246
return "hello"
248
247
249
248
250
- app.run(port=5000)
249
+ app.run(debug=True, port=5000)
251
250
```
252
251
253
252
Now run the script, hit the root URL ([ http://localhost:5000/ ] ( http://localhost:5000/ ) ) a few times, and watch your spans be emitted!
@@ -336,7 +335,6 @@ Finally, execute the following script:
336
335
337
336
```
338
337
# otcollector.py
339
- import time
340
338
341
339
from opentelemetry import trace
342
340
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
0 commit comments