Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example files
Browse files Browse the repository at this point in the history
ocelotl committed Dec 19, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 2cb7e06 commit 577d5c6
Showing 6 changed files with 242 additions and 0 deletions.
76 changes: 76 additions & 0 deletions examples/auto_instrumentation/README.md
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 git@github.com: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 -
```
38 changes: 38 additions & 0 deletions examples/auto_instrumentation/formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from flask import Flask, request

from opentelemetry import propagators, trace
from opentelemetry.context.propagation.tracecontexthttptextformat import (
TraceContextHTTPTextFormat,
)
from opentelemetry.propagators import set_global_httptextformat
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleExportSpanProcessor,
)
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_request")
def format_request():

with tracer.start_as_current_span(
"format_request",
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)
57 changes: 57 additions & 0 deletions examples/auto_instrumentation/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import sys
import time

import requests
from flask import Flask

from opentelemetry import propagators, trace
from opentelemetry.context.propagation.tracecontexthttptextformat import (
TraceContextHTTPTextFormat,
)
from opentelemetry.propagators import set_global_httptextformat
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleExportSpanProcessor,
)

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)

requested = requests.get(
"http://localhost:{}/{}".format(port, path),
params={param: value},
headers=headers,
)

assert requested.status_code == 200
return requested.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_request", "helloTo", hello_to)

with tracer.start_as_current_span("hello-publish", parent=hello_span):
http_get(8082, "publish_request", "helloStr", hello_str)

# yield to IOLoop to flush the spans
time.sleep(2)
38 changes: 38 additions & 0 deletions examples/auto_instrumentation/publisher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from flask import Flask, request

from opentelemetry import propagators, trace
from opentelemetry.context.propagation.tracecontexthttptextformat import (
TraceContextHTTPTextFormat,
)
from opentelemetry.propagators import set_global_httptextformat
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleExportSpanProcessor,
)
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_request")
def publish_request():

with tracer.start_as_current_span(
"publish_request", 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)
27 changes: 27 additions & 0 deletions examples/auto_instrumentation/publisher_untraced.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from flask import Flask, request

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleExportSpanProcessor,
)

app = Flask(__name__)

trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())

trace.tracer_source().add_span_processor(
SimpleExportSpanProcessor(ConsoleSpanExporter())
)


@app.route("/publish_request")
def publish_request():
hello_str = request.args.get("helloStr")
print(hello_str)
return "published"


if __name__ == "__main__":
app.run(port=8082)
6 changes: 6 additions & 0 deletions examples/auto_instrumentation/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def get_as_list(dict_object, key):
value = dict_object.get(key)
return value if value is not None else []


__all__ = ["get_as_list"]

0 comments on commit 577d5c6

Please sign in to comment.