An open API service indexing awesome lists of open source software.

https://github.com/ev2900/opentelemetry_tracing_python

Example implementing Opentelemetry via. Python. Visualize spans and traces via. Jaeger
https://github.com/ev2900/opentelemetry_tracing_python

jaeger jaegertracing opentelemetry python

Last synced: 6 months ago
JSON representation

Example implementing Opentelemetry via. Python. Visualize spans and traces via. Jaeger

Awesome Lists containing this project

README

          

# Opentelemetry Traces, Spans and Events via. Python

map-user map-user

This repository has examples implementing traces, spans and event analytics via. Opentelemetry in Python. An example of visualizing these via. Jaeger (running on docker) is also provided.

## What are Traces, Spans, Events ...

The major components of trace analytics via. Opentelemetry are as follows

* **Trace** is the overall view of a request's journey

* **Span(s)** are individual operations or units of work within that journey

* **Event(s)** are discrete points of interest within a span

For example, if you had a button on a website that submitted an order, loaded a new web page with a confirmation and sent a confirmation email, each click of the button could be a trace ie. the order trace. Under each order trace could would be multiple spans. The first span for writing the order information to a database, the second for the be the GET request and loading of the confirmation page and a third span for sending the confirmation email.

Structuring the trace and spans like ^ would give you a nice graph showing how long the overall process (trace) took and how long each of the subprocess (span) took.

If you had issues with this process would could look a visual of the traces and/or spans to see where a process failed or incurred additional latency.

Additionally at any point in a span we can also record an event which would be a discrete point of interest.

## Python Code Examples

The [opentelem-example.py](https://github.com/ev2900/Opentelemetry_Tracing_Python/blob/main/opentelem-example.py) file has example code to generate traces, spans and events in python.

The code has two main sections. The first option show how you can add span generation that is automatically triggered when a function is called. To understand this pattern better check out this section of the example

```
@add_function_trace.start_as_current_span("automatic-child-span-1-add-function") # Everytime the add function is called a span will automaticlly be added to the trace
def add(first, second): # Simple example function adding two numbers

current_span = trace.get_current_span() # Optional - Customize the current span
current_span.set_status(trace.StatusCode.OK) # Optional - Set status OK or ERROR

# Optional - Add an event
print(first)
current_span.add_event(name="print first number", attributes={"first_number": first}, timestamp = time.time_ns())
time.sleep(1)

return first + second
```

The second option you can more directly control the generation of spans. To understand this pattern better check out this section of the example

```
# Create a root span
with add_function_trace.start_as_current_span("manual-root-trace") as root_span:

# Create child span under root
with add_function_trace.start_as_current_span("manual-child-span-1") as child_span_1:
time.sleep(0.5)

# Optional - Add you additonal code HERE

child_span_1.add_event("Some event in child span 1 ...") # Optional - Add an event
child_span_1.set_attribute("custom", "attribute 1") # Optinal - Set custom attribute
child_span_1.set_status(trace.StatusCode.OK) # Optional - Set status OK

# Create another child span under root
with add_function_trace.start_as_current_span("manual-child-span-2") as child_span_2:
time.sleep(0.8)

# Optional - Add you additonal code HERE

child_span_2.add_event("Some event in child span 2 ...") # Optional - Add an event
child_span_2.set_attribute("custom", "attribute 2") # Optinal - Set custom attribute
child_span_2.set_status(trace.StatusCode.ERROR) # Optional - Set status ERROR
```

## Visualizing Traces, Spans, Events via. Jaeger

Multiple tools exist to collecting and visualizing Opentelemetry output. For this example Jaeger is a light weight and simple method. I would recommend running Jaeger via. docker on the same machine you run [opentelem-example.py](https://github.com/ev2900/Opentelemetry_Tracing_Python/blob/main/opentelem-example.py)

Assuming you already have docker installed you can run the following command to set up Jager

```docker run -d --name jaeger -e COLLECTOR_OTLP_ENABLED=true -p 16686:16686 -p 4317:4317 -p 4318:4318 jaegertracing/all-in-one:latest```

You can access the Jaeger UI via. [http://localhost:16686/search](http://localhost:16686/search). Once you can see the UI run the [opentelem-example.py](https://github.com/ev2900/Opentelemetry_Tracing_Python/blob/main/opentelem-example.py) and use the UI to find the trace.

An example of trace generated by the script is pictured below

opensearch_nginx_yaml