• Log in

Set up your lab environment

5 min

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:

  1. Spin up your Python application
  2. Send traffic to your app with a simple load generator
Step 1 of 3

Clone the lab repository:

bash
$
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.

Step 2 of 3

Create a new virtual environment:

bash
$
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.

Step 3 of 3

Run the load generator:

bash
$
python simulator.py

You should see debug logs detailing the responses from your database app:

bash
DEBUG:root:Successful create
DEBUG:root:Key `key1` doesn't exist
DEBUG:root:Successful delete
DEBUG:root:Key `key3` doesn't exist
DEBUG:root:Key `key2` doesn't exist
DEBUG:root:Successful create
DEBUG: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.

Copyright © 2022 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.