-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Overview | ||
|
||
This example shows how to use the auto-instrumentation agent in OpenTelemetry. | ||
|
||
A uninstrumented script will be executed once without the agent and then a instrumented script will | ||
be run with the agent. The results should show a `Span` being started in both cases. | ||
|
||
## Preparation | ||
|
||
This example will be executed in a separate virtual environment: | ||
|
||
```sh | ||
$ mkdir auto_instrumentation | ||
$ virtualenv auto_instrumentation | ||
$ source auto_instrumentation/bin/activate | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
$ git clone [email protected]:open-telemetry/opentelemetry-python.git | ||
$ cd opentelemetry-python | ||
$ git checkout issue_300 | ||
$ pip3 install -e opentelemetry-api | ||
$ pip3 install -e opentelemetry-sdk | ||
$ pip3 install -e ext/opentelemetry-flask | ||
$ pip3 install flask | ||
$ pip3 install requests | ||
``` | ||
|
||
## Execution of manually traced publisher | ||
|
||
This is done in 3 separate consoles, one to run each of the scripts that make up this example: | ||
|
||
```sh | ||
$ source auto_instrumentation/bin/activate | ||
$ python3 opentelemetry-python/examples/auto_instrumentation/formatter.py | ||
``` | ||
|
||
```sh | ||
$ source auto_instrumentation/bin/activate | ||
$ python3 opentelemetry-python/examples/auto_instrumentation/publisher.py | ||
``` | ||
|
||
```sh | ||
$ source auto_instrumentation/bin/activate | ||
$ python3 opentelemetry-python/examples/auto_instrumentation/hello.py testing | ||
``` | ||
|
||
The execution of `publisher.py` should return an output similar to: | ||
|
||
```sh | ||
Hello, testing! | ||
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) | ||
127.0.0.1 - - [18/Dec/2019 19:11:12] "GET /publish?helloStr=Hello%2C+testing%21 HTTP/1.1" 200 - | ||
``` | ||
|
||
Now, kill the execution of `publisher.py` with `ctrl + c` and run this instead: | ||
|
||
```sh | ||
$ auto_agent python3 opentelemetry-python/examples/auto_instrumentation/hello.py testing | ||
``` | ||
|
||
In the console where you previously executed `hello.py`, run again this: | ||
|
||
```sh | ||
$ python3 opentelemetry-python/examples/auto_instrumentation/hello.py testing | ||
``` | ||
|
||
That should produce an output similar to this in the console where the `auto_agent` was executed: | ||
|
||
```sh | ||
Hello, testing! | ||
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) | ||
127.0.0.1 - - [18/Dec/2019 19:11:12] "GET /publish?helloStr=Hello%2C+testing%21 HTTP/1.1" 200 - | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from flask import Flask, request | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerSource | ||
from opentelemetry import propagators | ||
from opentelemetry.context.propagation.tracecontexthttptextformat import ( | ||
TraceContextHTTPTextFormat | ||
) | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter | ||
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor | ||
from opentelemetry.propagators import set_global_httptextformat | ||
from utils import get_as_list | ||
|
||
app = Flask(__name__) | ||
|
||
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) | ||
tracer = trace.tracer_source().get_tracer(__name__) | ||
|
||
trace.tracer_source().add_span_processor( | ||
SimpleExportSpanProcessor(ConsoleSpanExporter()) | ||
) | ||
set_global_httptextformat(TraceContextHTTPTextFormat) | ||
|
||
|
||
@app.route("/format") | ||
def format(): | ||
|
||
with tracer.start_as_current_span( | ||
'format', parent=propagators.extract(get_as_list, request.headers) | ||
): | ||
hello_to = request.args.get('helloTo') | ||
return 'Hello, %s!' % hello_to | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(port=8081) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import requests | ||
import sys | ||
import time | ||
from flask import Flask | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerSource | ||
from opentelemetry import propagators | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter | ||
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor | ||
from opentelemetry.context.propagation.tracecontexthttptextformat import ( | ||
TraceContextHTTPTextFormat | ||
) | ||
from opentelemetry.propagators import set_global_httptextformat | ||
|
||
app = Flask(__name__) | ||
|
||
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) | ||
tracer = trace.tracer_source().get_tracer(__name__) | ||
|
||
trace.tracer_source().add_span_processor( | ||
SimpleExportSpanProcessor(ConsoleSpanExporter()) | ||
) | ||
set_global_httptextformat(TraceContextHTTPTextFormat) | ||
|
||
|
||
def http_get(port, path, param, value): | ||
|
||
headers = {} | ||
propagators.inject(tracer, dict.__setitem__, headers) | ||
|
||
r = requests.get( | ||
'http://localhost:{}/{}'.format(port, path), | ||
params={param: value}, | ||
headers=headers | ||
) | ||
|
||
assert r.status_code == 200 | ||
return r.text | ||
|
||
|
||
assert len(sys.argv) == 2 | ||
|
||
hello_to = sys.argv[1] | ||
|
||
with tracer.start_as_current_span('hello') as hello_span: | ||
|
||
with tracer.start_as_current_span('hello-format', parent=hello_span): | ||
hello_str = http_get(8081, 'format', 'helloTo', hello_to) | ||
|
||
with tracer.start_as_current_span('hello-publish', parent=hello_span): | ||
http_get(8082, 'publish', 'helloStr', hello_str) | ||
|
||
# yield to IOLoop to flush the spans | ||
time.sleep(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from flask import Flask, request | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerSource | ||
from opentelemetry import propagators | ||
from opentelemetry.context.propagation.tracecontexthttptextformat import ( | ||
TraceContextHTTPTextFormat | ||
) | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter | ||
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor | ||
from opentelemetry.propagators import set_global_httptextformat | ||
from utils import get_as_list | ||
|
||
app = Flask(__name__) | ||
|
||
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) | ||
tracer = trace.tracer_source().get_tracer(__name__) | ||
|
||
trace.tracer_source().add_span_processor( | ||
SimpleExportSpanProcessor(ConsoleSpanExporter()) | ||
) | ||
set_global_httptextformat(TraceContextHTTPTextFormat) | ||
|
||
|
||
@app.route("/publish") | ||
def publish(): | ||
|
||
with tracer.start_as_current_span( | ||
'publish', propagators.extract(get_as_list, request.headers) | ||
): | ||
hello_str = request.args.get('helloStr') | ||
print(hello_str) | ||
return 'published' | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(port=8082) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from flask import Flask, request | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerSource | ||
# from opentelemetry.context.propagation.tracecontexthttptextformat import ( | ||
# TraceContextHTTPTextFormat | ||
# ) | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter | ||
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor | ||
# from opentelemetry.propagators import set_global_httptextformat | ||
|
||
app = Flask(__name__) | ||
|
||
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) | ||
# tracer = trace.tracer_source().get_tracer(__name__) | ||
|
||
trace.tracer_source().add_span_processor( | ||
SimpleExportSpanProcessor(ConsoleSpanExporter()) | ||
) | ||
# set_global_httptextformat(TraceContextHTTPTextFormat) | ||
|
||
|
||
@app.route("/publish") | ||
def publish(): | ||
hello_str = request.args.get('helloStr') | ||
print(hello_str) | ||
return 'published' | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(port=8082) |