Skip to content

Instantly share code, notes, and snippets.

@tedsuo
tedsuo / server.py
Created November 24, 2020 17:51
Python recording errors
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 November 24, 2020 17:50
Python span context management
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 November 24, 2020 17:45
Python basic span lifecycle
# 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 November 24, 2020 17:44
Python add data to current span
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 November 24, 2020 17:43
python basic client
import requests
for i in range(5):
r = requests.get("http://localhost:8000/hello")
print(r.text)
@tedsuo
tedsuo / server.py
Created November 24, 2020 17:42
python basic server
#!/usr/bin/env python3
from flask import Flask
from time import sleep
PORT = 8000
app = Flask(__name__)
@app.route("/hello")
def hello():
@tedsuo
tedsuo / node_errors.js
Created November 11, 2020 23:28
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 });
@tedsuo
tedsuo / node_with_span.js
Created November 11, 2020 23:25
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');
@tedsuo
tedsuo / node_span_start.js
Created November 11, 2020 23:21
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 November 11, 2020 23:09
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);
});