lab
This procedure is part of a lab that teaches you how to manually instrument your application with OpenTelemetry. If you haven't already, check out the lab introduction.
Before you can walk through this lab, you need to set up your development environment. Here, you:
- Spin up your Python application
- Send traffic to your app with a simple load generator
Clone the lab repository:
$git clone https://github.com/newrelic-experimental/otel-manual-instrumentation-lab-materials
This repository contains a simple Python application, in db.py, that interacts with a trivial datastore:
import logging
class DuplicateKeyError(Exception): pass
class KeyDoesNotExistError(Exception): pass
db = {}
def read(key): """Read key from the database.""" global db
try: value = db[key] logging.debug("Successful read") return value except KeyError as ke: msg = f"Key `{key}` doesn't exist" logging.debug(msg) raise KeyDoesNotExistError(msg)
def create(key, value): """Write key:value to the database.""" global db
if key in db: msg = f"Key `{key}` already exists" logging.debug(msg) raise DuplicateKeyError(msg)
db[key] = value logging.debug("Successful create") return value
def update(key, value): """Update key in the database.""" global db
if key in db: db[key] = value logging.debug("Successful update") return value
msg = f"Key `{key}` doesn't exist" logging.debug(msg) raise KeyDoesNotExistError(msg)
def delete(key): """Delete key from the database.""" global db
if key in db: del db[key] logging.debug("Successful delete") return True
return False
It provides functions for reading, creating, updating, and deleting records from the database. Some of these functions raise exceptions when assumptions are violated, such as when a user tries to read a key that's not there.
The repository also includes a load generator, in simulator.py that sends semi-random traffic to the database application.
Create a new virtual environment:
$python3 -m venv venv$source venv/bin/activate
While you don't need to install dependencies now, you will soon. When you do, you'll want to install them into this virtual environment, rather than the global one.
Run the load generator:
$python simulator.py
You should see debug logs detailing the responses from your database app:
DEBUG:root:Successful createDEBUG:root:Key `key1` doesn't existDEBUG:root:Successful deleteDEBUG:root:Key `key3` doesn't existDEBUG:root:Key `key2` doesn't existDEBUG:root:Successful createDEBUG:root:Successful create
The simulator randomly chooses database actions to perform, so your output should look similar to this, but may not be identical.
Now that you've got your application and load generator running, it's time to see what OpenTelemetry's all about.
lab
This procedure is part of a lab that teaches you how to instrument your application with OpenTelemetry. Now that you've set up your environment, instrument your application.