Using highlight.io with Other Python Frameworks
Learn how to set up highlight.io in your Python app.
Configure client-side Highlight. (optional)
If you're using Highlight on the frontend for your application, make sure you've initialized it correctly and followed the fullstack mapping guide.
Install the highlight-io python package.
Download the package from pypi and save it to your requirements. If you use a zip or s3 file upload to publish your function, you will want to make sure highlight-io
is part of the build.
poetry add highlight-io
# or with pip
pip install highlight-io
Initialize the Highlight SDK.
Setup the SDK.
import highlight_io
# `instrument_logging=True` sets up logging instrumentation.
# if you do not want to send logs or are using `loguru`, pass `instrument_logging=False`
H = highlight_io.H(
"<YOUR_PROJECT_ID>",
instrument_logging=True,
service_name="my-app",
service_version="git-sha",
environment="production",
)
Verify your installation.
Check that your installation is valid by throwing an error. Try raising an exception somewhere in your code. You should see a DivideByZero
error in the Highlight errors page within a few moments.
import logging
import random
import time
import highlight_io
# `instrument_logging=True` sets up logging instrumentation.
# if you do not want to send logs or are using `loguru`, pass `instrument_logging=False`
H = highlight_io.H(
"<YOUR_PROJECT_ID>",
instrument_logging=True,
service_name="my-app",
service_version="git-sha",
environment="production",
)
def main():
with H.trace(span_name="my_span"):
logging.info('hello, world!', {'favorite_number': 7})
return f"<h1>bad idea { 5/0 }</h1>"
if __name__ == "__main__":
main()
Call the built-in Python logging library.
Logs are reported automatically from the builtin logging methods (as long as instrument_logging=True
is provided to the highlight_io.H
constructor). Visit the highlight logs portal and check that backend logs are coming in. Arguments passed as a dictionary as the second parameter will be interpreted as structured key-value pairs that logs can be easily searched by.
import logging
def main():
logging.info('hello, world!')
logging.warn('whoa there', {'key': 'value'})
Verify your backend logs are being recorded.
Visit the highlight logs portal and check that backend logs are coming in.
Run your code with the H.trace() wrapper.
Wrap your code with H.trace(), and run your code.
import logging
def main():
with H.trace(span_name="my_span"):
logging.info('hello, world!', {'favorite_number': 7})
return f"<h1>Hello world</h1>"
if __name__ == "__main__":
main()
Use a decorator to trace your functions.
Use the highlight_io.trace()
decorator to create spans for your functions.
import logging
@highlight_io.trace
def my_cool_method():
logging.info("hello my_cool_method", {"customer": "unknown", "trace": "inside"})
time.sleep(random.randint(0, 10) / 1000)
logging.info("goodbye my_cool_method", {"customer": "unknown", "trace": "inside"})
def main():
with H.trace(span_name="my_span"):
logging.info('hello, world!', {'favorite_number': 7})
my_cool_method()
return f"<h1>Hello world</h1>"
if __name__ == "__main__":
main()
Verify your backend traces are being recorded.
Visit the highlight traces portal and check that backend traces are coming in.