Skip to content

Commit 577d5c6

Browse files
committed
Add example files
1 parent 2cb7e06 commit 577d5c6

File tree

6 files changed

+242
-0
lines changed

6 files changed

+242
-0
lines changed

Diff for: examples/auto_instrumentation/README.md

+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+
```

Diff for: examples/auto_instrumentation/formatter.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from flask import Flask, request
2+
3+
from opentelemetry import propagators, trace
4+
from opentelemetry.context.propagation.tracecontexthttptextformat import (
5+
TraceContextHTTPTextFormat,
6+
)
7+
from opentelemetry.propagators import set_global_httptextformat
8+
from opentelemetry.sdk.trace import TracerSource
9+
from opentelemetry.sdk.trace.export import (
10+
ConsoleSpanExporter,
11+
SimpleExportSpanProcessor,
12+
)
13+
from utils import get_as_list
14+
15+
app = Flask(__name__)
16+
17+
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
18+
tracer = trace.tracer_source().get_tracer(__name__)
19+
20+
trace.tracer_source().add_span_processor(
21+
SimpleExportSpanProcessor(ConsoleSpanExporter())
22+
)
23+
set_global_httptextformat(TraceContextHTTPTextFormat)
24+
25+
26+
@app.route("/format_request")
27+
def format_request():
28+
29+
with tracer.start_as_current_span(
30+
"format_request",
31+
parent=propagators.extract(get_as_list, request.headers),
32+
):
33+
hello_to = request.args.get("helloTo")
34+
return "Hello, %s!" % hello_to
35+
36+
37+
if __name__ == "__main__":
38+
app.run(port=8081)

Diff for: examples/auto_instrumentation/hello.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import sys
2+
import time
3+
4+
import requests
5+
from flask import Flask
6+
7+
from opentelemetry import propagators, trace
8+
from opentelemetry.context.propagation.tracecontexthttptextformat import (
9+
TraceContextHTTPTextFormat,
10+
)
11+
from opentelemetry.propagators import set_global_httptextformat
12+
from opentelemetry.sdk.trace import TracerSource
13+
from opentelemetry.sdk.trace.export import (
14+
ConsoleSpanExporter,
15+
SimpleExportSpanProcessor,
16+
)
17+
18+
app = Flask(__name__)
19+
20+
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
21+
tracer = trace.tracer_source().get_tracer(__name__)
22+
23+
trace.tracer_source().add_span_processor(
24+
SimpleExportSpanProcessor(ConsoleSpanExporter())
25+
)
26+
set_global_httptextformat(TraceContextHTTPTextFormat)
27+
28+
29+
def http_get(port, path, param, value):
30+
31+
headers = {}
32+
propagators.inject(tracer, dict.__setitem__, headers)
33+
34+
requested = requests.get(
35+
"http://localhost:{}/{}".format(port, path),
36+
params={param: value},
37+
headers=headers,
38+
)
39+
40+
assert requested.status_code == 200
41+
return requested.text
42+
43+
44+
assert len(sys.argv) == 2
45+
46+
hello_to = sys.argv[1]
47+
48+
with tracer.start_as_current_span("hello") as hello_span:
49+
50+
with tracer.start_as_current_span("hello-format", parent=hello_span):
51+
hello_str = http_get(8081, "format_request", "helloTo", hello_to)
52+
53+
with tracer.start_as_current_span("hello-publish", parent=hello_span):
54+
http_get(8082, "publish_request", "helloStr", hello_str)
55+
56+
# yield to IOLoop to flush the spans
57+
time.sleep(2)

Diff for: examples/auto_instrumentation/publisher.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from flask import Flask, request
2+
3+
from opentelemetry import propagators, trace
4+
from opentelemetry.context.propagation.tracecontexthttptextformat import (
5+
TraceContextHTTPTextFormat,
6+
)
7+
from opentelemetry.propagators import set_global_httptextformat
8+
from opentelemetry.sdk.trace import TracerSource
9+
from opentelemetry.sdk.trace.export import (
10+
ConsoleSpanExporter,
11+
SimpleExportSpanProcessor,
12+
)
13+
from utils import get_as_list
14+
15+
app = Flask(__name__)
16+
17+
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
18+
tracer = trace.tracer_source().get_tracer(__name__)
19+
20+
trace.tracer_source().add_span_processor(
21+
SimpleExportSpanProcessor(ConsoleSpanExporter())
22+
)
23+
set_global_httptextformat(TraceContextHTTPTextFormat)
24+
25+
26+
@app.route("/publish_request")
27+
def publish_request():
28+
29+
with tracer.start_as_current_span(
30+
"publish_request", propagators.extract(get_as_list, request.headers)
31+
):
32+
hello_str = request.args.get("helloStr")
33+
print(hello_str)
34+
return "published"
35+
36+
37+
if __name__ == "__main__":
38+
app.run(port=8082)

Diff for: examples/auto_instrumentation/publisher_untraced.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from flask import Flask, request
2+
3+
from opentelemetry import trace
4+
from opentelemetry.sdk.trace import TracerSource
5+
from opentelemetry.sdk.trace.export import (
6+
ConsoleSpanExporter,
7+
SimpleExportSpanProcessor,
8+
)
9+
10+
app = Flask(__name__)
11+
12+
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
13+
14+
trace.tracer_source().add_span_processor(
15+
SimpleExportSpanProcessor(ConsoleSpanExporter())
16+
)
17+
18+
19+
@app.route("/publish_request")
20+
def publish_request():
21+
hello_str = request.args.get("helloStr")
22+
print(hello_str)
23+
return "published"
24+
25+
26+
if __name__ == "__main__":
27+
app.run(port=8082)

Diff for: examples/auto_instrumentation/utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def get_as_list(dict_object, key):
2+
value = dict_object.get(key)
3+
return value if value is not None else []
4+
5+
6+
__all__ = ["get_as_list"]

0 commit comments

Comments
 (0)