|
| 1 | +# Overview |
| 2 | + |
| 3 | +This example shows how to use auto-instrumentation in OpenTelemetry. This example is also based on a previous example |
| 4 | +for OpenTracing that can be found [here](https://github.com/yurishkuro/opentracing-tutorial/tree/master/python). |
| 5 | + |
| 6 | +This example uses 2 scripts whose main difference is they being instrumented manually or not: |
| 7 | + |
| 8 | +1. `server_instrumented.py` which has been instrumented manually |
| 9 | +2. `server_uninstrumented.py` which has not been instrumented manually |
| 10 | + |
| 11 | +The former will be run without the automatic instrumentation agent and the latter with the automatic instrumentation |
| 12 | +agent. They should produce the same result, showing that the automatic instrumentation agent does the equivalent |
| 13 | +of what manual instrumentation does. |
| 14 | + |
| 15 | +In order to understand this better, here is the relevant part of both scripts: |
| 16 | + |
| 17 | +## Manually instrumented server |
| 18 | + |
| 19 | +`server_instrumented.py` |
| 20 | + |
| 21 | +```python |
| 22 | +@app.route("/server_request") |
| 23 | +def server_request(): |
| 24 | + with tracer.start_as_current_span( |
| 25 | + "server_request", |
| 26 | + parent=propagators.extract( |
| 27 | + lambda dict_, key: dict_.get(key, []), request.headers |
| 28 | + )["current-span"], |
| 29 | + ): |
| 30 | + print(request.args.get("param")) |
| 31 | + return "served" |
| 32 | +``` |
| 33 | + |
| 34 | +## Publisher not instrumented manually |
| 35 | + |
| 36 | +`server_uninstrumented.py` |
| 37 | + |
| 38 | +```python |
| 39 | +@app.route("/server_request") |
| 40 | +def server_request(): |
| 41 | + print(request.args.get("param")) |
| 42 | + return "served" |
| 43 | +``` |
| 44 | + |
| 45 | +# Preparation |
| 46 | + |
| 47 | +This example will be executed in a separate virtual environment: |
| 48 | + |
| 49 | +```sh |
| 50 | +$ mkdir auto_instrumentation |
| 51 | +$ virtualenv auto_instrumentation |
| 52 | +$ source auto_instrumentation/bin/activate |
| 53 | +``` |
| 54 | + |
| 55 | +# Installation |
| 56 | + |
| 57 | +```sh |
| 58 | +$ pip install opentelemetry-api |
| 59 | +$ pip install opentelemetry-sdk |
| 60 | +$ pip install opentelemetry-auto-instrumentation |
| 61 | +$ pip install ext/opentelemetry-ext-flask |
| 62 | +$ pip install flask |
| 63 | +$ pip install requests |
| 64 | +``` |
| 65 | + |
| 66 | +# Execution |
| 67 | + |
| 68 | +## Execution of the manually instrumented server |
| 69 | + |
| 70 | +This is done in 2 separate consoles, one to run each of the scripts that make up this example: |
| 71 | + |
| 72 | +```sh |
| 73 | +$ source auto_instrumentation/bin/activate |
| 74 | +$ python opentelemetry-python/opentelemetry-auto-instrumentation/example/server_instrumented.py |
| 75 | +``` |
| 76 | + |
| 77 | +```sh |
| 78 | +$ source auto_instrumentation/bin/activate |
| 79 | +$ python opentelemetry-python/opentelemetry-auto-instrumentation/example/client.py testing |
| 80 | +``` |
| 81 | + |
| 82 | +The execution of `server_instrumented.py` should return an output similar to: |
| 83 | + |
| 84 | +```sh |
| 85 | +Hello, testing! |
| 86 | +Span(name="serv_request", context=SpanContext(trace_id=0x9c0e0ce8f7b7dbb51d1d6e744a4dad49, span_id=0xd1ba3ec4c76a0d7f, trace_state={}), kind=SpanKind.INTERNAL, parent=None, start_time=2020-03-19T00:06:31.275719Z, end_time=2020-03-19T00:06:31.275920Z) |
| 87 | +127.0.0.1 - - [18/Mar/2020 18:06:31] "GET /serv_request?helloStr=Hello%2C+testing%21 HTTP/1.1" 200 - |
| 88 | +``` |
| 89 | + |
| 90 | +## Execution of an automatically instrumented server |
| 91 | + |
| 92 | +Now, kill the execution of `server_instrumented.py` with `ctrl + c` and run this instead: |
| 93 | + |
| 94 | +```sh |
| 95 | +$ opentelemetry-auto-instrumentation opentelemetry-python/opentelemetry-auto-instrumentation/example/server_uninstrumented.py |
| 96 | +``` |
| 97 | + |
| 98 | +In the console where you previously executed `client.py`, run again this again: |
| 99 | + |
| 100 | +```sh |
| 101 | +$ python opentelemetry-python/opentelemetry-auto-instrumentation/example/client.py testing |
| 102 | +``` |
| 103 | + |
| 104 | +The execution of `server_uninstrumented.py` should return an output similar to: |
| 105 | + |
| 106 | +```sh |
| 107 | +Hello, testing! |
| 108 | +Span(name="serv_request", context=SpanContext(trace_id=0xf26b28b5243e48f5f96bfc753f95f3f0, span_id=0xbeb179a095d087ed, trace_state={}), kind=SpanKind.SERVER, parent=<opentelemetry.trace.DefaultSpan object at 0x7f1a20a54908>, start_time=2020-03-19T00:24:18.828561Z, end_time=2020-03-19T00:24:18.845127Z) |
| 109 | +127.0.0.1 - - [18/Mar/2020 18:24:18] "GET /serv_request?helloStr=Hello%2C+testing%21 HTTP/1.1" 200 - |
| 110 | +``` |
| 111 | + |
| 112 | +As you can see, both outputs are equivalentsince the automatic instrumentation does what the manual instrumentation does too. |
0 commit comments