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
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) |
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
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) |
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
# 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. |
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
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", |
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
import requests | |
for i in range(5): | |
r = requests.get("http://localhost:8000/hello") | |
print(r.text) |
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
#!/usr/bin/env python3 | |
from flask import Flask | |
from time import sleep | |
PORT = 8000 | |
app = Flask(__name__) | |
@app.route("/hello") | |
def hello(): |
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
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 }); |
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
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'); |
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
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. |
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
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); | |
}); |
NewerOlder