Skip to content

Instantly share code, notes, and snippets.

@tedsuo
tedsuo / server.py
Created Nov 24, 2020
Python recording errors
View server.py
from opentelemetry.trace.status import StatusCode
@app.route("/hello")
def hello():
span = trace.propagation.get_current_span()
try:
1 / 0
except ZeroDivisionError as error:
# record an exception
span.record_exception(error)
@tedsuo
tedsuo / server.py
Created Nov 24, 2020
Python span context management
View server.py
from opentelemetry import trace
# create a tracer and name it after your package
tracer = trace.get_tracer(__name__)
@app.route("/hello")
def hello():
# add latency to the parent span
sleep(20 / 1000)
@tedsuo
tedsuo / server.py
Created Nov 24, 2020
Python basic span lifecycle
View server.py
# Start the span with a name and a parent span
child = tracer.start_span("my_operation", parent=parent)
try:
# pass the span around as a parameter
do_work(span=child)
finally:
# End the span, which measures the span duration and
# triggers the span data to be exported.
# WARNING: failing to end a span will create a leak.
@tedsuo
tedsuo / perver.py
Created Nov 24, 2020
Python add data to current span
View perver.py
from opentelemetry import trace
@app.route("/hello")
def hello():
# get the current span, created by flask
span = trace.get_current_span()
# add more attributes to the server span
span.set_attribute("http.route", "some_route")
# add events (AKA structured logging)
span.add_event("event message",
@tedsuo
tedsuo / client.py
Created Nov 24, 2020
python basic client
View client.py
import requests
for i in range(5):
r = requests.get("http://localhost:8000/hello")
print(r.text)
@tedsuo
tedsuo / server.py
Created Nov 24, 2020
python basic server
View server.py
#!/usr/bin/env python3
from flask import Flask
from time import sleep
PORT = 8000
app = Flask(__name__)
@app.route("/hello")
def hello():
View node_errors.js
try {
throw ("ooops");
} catch (error) {
// Add the exception as a properly formatted event.
span.recordException(error);
// Set the status code to make the exception count
// as an error.
span.setStatus({ code:
opentelemetry.CanonicalCode.UNKNOWN });
View node_with_span.js
app.get('/hello', (req, res) => {
// start a new span named “sleeper”
const childSpan = tracer.startSpan("sleeper");
// use withSpan to create a new context
tracer.withSpan(childSpan,()=> {
setTimeout(()=> {
// getCurrentSpan now correctly returns childSpan
const span = tracer.getCurrentSpan();
span.addEvent('sending response');
View node_span_start.js
app.get('/hello', (req, res) => {
// start a new span named “sleeper”
const childSpan = tracer.startSpan("sleeper");
setTimeout(()=> {
// childSpan works normally when referenced
childSpan.addEvent('finished sleeping');
// However, starting a span does not automatically
// set it to the current span. getCurrentSpan still
// returns the parent span.
@tedsuo
tedsuo / node_span_chaining.js
Created Nov 11, 2020
node_span_chaining.js
View node_span_chaining.js
app.get('/hello', (req, res) => {
tracer.getCurrentSpan()
.setAttribute('projectID', '123')
.addEvent('setting timeout', { sleep: 300 });
setTimeout(()=> {
tracer.getCurrentSpan().addEvent('sending response');
res.status(200).send('Hello World');
}, 300);
});