Skip to content

Commit 1b300de

Browse files
committed
Add example files
1 parent 2cb7e06 commit 1b300de

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Overview
2+
3+
This example shows how to use the auto-instrumentation agent in OpenTelemetry.
4+
5+
A uninstrumented script will be executed once without the agent and then a instrumented script will
6+
be run with the agent. The results should show a `Span` being started in both cases.
7+
8+
## Preparation
9+
10+
This example will be executed in a separate virtual environment:
11+
12+
```sh
13+
$ mkdir auto_instrumentation
14+
$ virtualenv auto_instrumentation
15+
$ source auto_instrumentation/bin/activate
16+
```
17+
18+
## Installation
19+
20+
```sh
21+
$ git clone [email protected]:open-telemetry/opentelemetry-python.git
22+
$ cd opentelemetry-python
23+
$ git checkout issue_300
24+
$ pip3 install -e opentelemetry-api
25+
$ pip3 install -e opentelemetry-sdk
26+
$ pip3 install -e ext/opentelemetry-flask
27+
$ pip3 install flask
28+
$ pip3 install requests
29+
```
30+
31+
## Execution of manually traced publisher
32+
33+
This is done in 3 separate consoles, one to run each of the scripts that make up this example:
34+
35+
```sh
36+
$ source auto_instrumentation/bin/activate
37+
$ python3 opentelemetry-python/examples/auto_instrumentation/formatter.py
38+
```
39+
40+
```sh
41+
$ source auto_instrumentation/bin/activate
42+
$ python3 opentelemetry-python/examples/auto_instrumentation/publisher.py
43+
```
44+
45+
```sh
46+
$ source auto_instrumentation/bin/activate
47+
$ python3 opentelemetry-python/examples/auto_instrumentation/hello.py testing
48+
```
49+
50+
The execution of `publisher.py` should return an output similar to:
51+
52+
```sh
53+
Hello, testing!
54+
Span(name="publish", context=SpanContext(trace_id=0xd18be4c644d3be57a8623bbdbdbcef76, span_id=0x6162c475bab8d365, trace_state={}), kind=SpanKind.SERVER, parent=SpanContext(trace_id=0xd18be4c644d3be57a8623bbdbdbcef76, span_id=0xdafb264c5b1b6ed0, trace_state={}), start_time=2019-12-19T01:11:12.172866Z, end_time=2019-12-19T01:11:12.173383Z)
55+
127.0.0.1 - - [18/Dec/2019 19:11:12] "GET /publish?helloStr=Hello%2C+testing%21 HTTP/1.1" 200 -
56+
```
57+
58+
Now, kill the execution of `publisher.py` with `ctrl + c` and run this instead:
59+
60+
```sh
61+
$ auto_agent python3 opentelemetry-python/examples/auto_instrumentation/hello.py testing
62+
```
63+
64+
In the console where you previously executed `hello.py`, run again this:
65+
66+
```sh
67+
$ python3 opentelemetry-python/examples/auto_instrumentation/hello.py testing
68+
```
69+
70+
That should produce an output similar to this in the console where the `auto_agent` was executed:
71+
72+
```sh
73+
Hello, testing!
74+
Span(name="publish", context=SpanContext(trace_id=0xd18be4c644d3be57a8623bbdbdbcef76, span_id=0x6162c475bab8d365, trace_state={}), kind=SpanKind.SERVER, parent=SpanContext(trace_id=0xd18be4c644d3be57a8623bbdbdbcef76, span_id=0xdafb264c5b1b6ed0, trace_state={}), start_time=2019-12-19T01:11:12.172866Z, end_time=2019-12-19T01:11:12.173383Z)
75+
127.0.0.1 - - [18/Dec/2019 19:11:12] "GET /publish?helloStr=Hello%2C+testing%21 HTTP/1.1" 200 -
76+
```
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from flask import Flask, request
2+
3+
from opentelemetry import trace
4+
from opentelemetry.sdk.trace import TracerSource
5+
from opentelemetry import propagators
6+
from opentelemetry.context.propagation.tracecontexthttptextformat import (
7+
TraceContextHTTPTextFormat
8+
)
9+
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
10+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
11+
from opentelemetry.propagators import set_global_httptextformat
12+
from utils import get_as_list
13+
14+
app = Flask(__name__)
15+
16+
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
17+
tracer = trace.tracer_source().get_tracer(__name__)
18+
19+
trace.tracer_source().add_span_processor(
20+
SimpleExportSpanProcessor(ConsoleSpanExporter())
21+
)
22+
set_global_httptextformat(TraceContextHTTPTextFormat)
23+
24+
25+
@app.route("/format")
26+
def format():
27+
28+
with tracer.start_as_current_span(
29+
'format', parent=propagators.extract(get_as_list, request.headers)
30+
):
31+
hello_to = request.args.get('helloTo')
32+
return 'Hello, %s!' % hello_to
33+
34+
35+
if __name__ == "__main__":
36+
app.run(port=8081)
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import requests
2+
import sys
3+
import time
4+
from flask import Flask
5+
6+
from opentelemetry import trace
7+
from opentelemetry.sdk.trace import TracerSource
8+
from opentelemetry import propagators
9+
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
10+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
11+
from opentelemetry.context.propagation.tracecontexthttptextformat import (
12+
TraceContextHTTPTextFormat
13+
)
14+
from opentelemetry.propagators import set_global_httptextformat
15+
16+
app = Flask(__name__)
17+
18+
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
19+
tracer = trace.tracer_source().get_tracer(__name__)
20+
21+
trace.tracer_source().add_span_processor(
22+
SimpleExportSpanProcessor(ConsoleSpanExporter())
23+
)
24+
set_global_httptextformat(TraceContextHTTPTextFormat)
25+
26+
27+
def http_get(port, path, param, value):
28+
29+
headers = {}
30+
propagators.inject(tracer, dict.__setitem__, headers)
31+
32+
r = requests.get(
33+
'http://localhost:{}/{}'.format(port, path),
34+
params={param: value},
35+
headers=headers
36+
)
37+
38+
assert r.status_code == 200
39+
return r.text
40+
41+
42+
assert len(sys.argv) == 2
43+
44+
hello_to = sys.argv[1]
45+
46+
with tracer.start_as_current_span('hello') as hello_span:
47+
48+
with tracer.start_as_current_span('hello-format', parent=hello_span):
49+
hello_str = http_get(8081, 'format', 'helloTo', hello_to)
50+
51+
with tracer.start_as_current_span('hello-publish', parent=hello_span):
52+
http_get(8082, 'publish', 'helloStr', hello_str)
53+
54+
# yield to IOLoop to flush the spans
55+
time.sleep(2)
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from flask import Flask, request
2+
3+
from opentelemetry import trace
4+
from opentelemetry.sdk.trace import TracerSource
5+
from opentelemetry import propagators
6+
from opentelemetry.context.propagation.tracecontexthttptextformat import (
7+
TraceContextHTTPTextFormat
8+
)
9+
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
10+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
11+
from opentelemetry.propagators import set_global_httptextformat
12+
from utils import get_as_list
13+
14+
app = Flask(__name__)
15+
16+
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
17+
tracer = trace.tracer_source().get_tracer(__name__)
18+
19+
trace.tracer_source().add_span_processor(
20+
SimpleExportSpanProcessor(ConsoleSpanExporter())
21+
)
22+
set_global_httptextformat(TraceContextHTTPTextFormat)
23+
24+
25+
@app.route("/publish")
26+
def publish():
27+
28+
with tracer.start_as_current_span(
29+
'publish', propagators.extract(get_as_list, request.headers)
30+
):
31+
hello_str = request.args.get('helloStr')
32+
print(hello_str)
33+
return 'published'
34+
35+
36+
if __name__ == "__main__":
37+
app.run(port=8082)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from flask import Flask, request
2+
3+
from opentelemetry import trace
4+
from opentelemetry.sdk.trace import TracerSource
5+
# from opentelemetry.context.propagation.tracecontexthttptextformat import (
6+
# TraceContextHTTPTextFormat
7+
# )
8+
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
9+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
10+
# from opentelemetry.propagators import set_global_httptextformat
11+
12+
app = Flask(__name__)
13+
14+
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
15+
# tracer = trace.tracer_source().get_tracer(__name__)
16+
17+
trace.tracer_source().add_span_processor(
18+
SimpleExportSpanProcessor(ConsoleSpanExporter())
19+
)
20+
# set_global_httptextformat(TraceContextHTTPTextFormat)
21+
22+
23+
@app.route("/publish")
24+
def publish():
25+
hello_str = request.args.get('helloStr')
26+
print(hello_str)
27+
return 'published'
28+
29+
30+
if __name__ == "__main__":
31+
app.run(port=8082)

0 commit comments

Comments
 (0)