Overview
This guide shows you how to log spans to Splunk Agent Observability using the@log decorator in Python.
In this guide you will:
Before you start
To complete this how-to, you will need:- An OpenAI API key
- A Splunk Agent Observability project configured
- Your Splunk Agent Observability API key
Install dependencies
To use Splunk Agent Observability, you need to install some package dependencies, and configure environment variables.1
Install Required Dependencies
Install the required dependencies for your app. If you are using Python, create a virtual environment using your preferred method, then install dependencies inside that environment:
2
Create a .env file, and add the following values
Create the basic app to call OpenAI
1
Create a file for your application called app.py or app.ts.
2
Add the following code to call OpenAI to ask a question
3
Run the app to ensure everything is working
Add simple logging with the Splunk Agent Observability log decorator or wrapper
Splunk Agent Observability has a@log decorator in Python that logs function calls as spans. If these decorated or wrapped calls are called whilst there is an active trace, they are added to that trace. If there is no active trace, a new one is created for this span.
In this guide, you will be adding the decorator or wrapper to log the function that calls OpenAI.
1
Import the log decorator
At the top of your file, add an import for the log decorator:
2
Decorate or wrap the function
Update the function definition to include the decorator or wrapper:This will log the function as an LLM span using the span type parameter. You can read more about these in our span types documentation.This also sets the name of the span to “OpenAI GPT-4o-mini”.
3
Run the app
prompt, the output as the returned response. The duration will also be logged.4
View the logged trace
From your Splunk Agent Observability homepage, open the Agent Stream for your project. You will see a trace with a single span containing the logged function call.Select the trace to see a detailed view:
Select the OpenAI span, then select the Evaluators tab to see the latency.
