Skip to main content
The Splunk Agent Observability Context Manager provides a convenient way to control the logging behavior of your application. It allows you to:
  1. Set the project and Agent Stream for all logs
  2. Get the current logger
  3. Flush traces
In Python, you can also:
  1. Set the project and Agent Stream for a particular scope
  2. Manage sessions
  3. Get the current span and trace
In the Python SDK, this is available through the splunk_ao_context object.

Set the project and Agent Stream

By default, Splunk Agent Observability uses the SPLUNK_AO_PROJECT and SPLUNK_AO_AGENT_STREAM environment variables to determine which project and Agent Stream to log to. You can override this by initializing the Splunk Agent Observability context with a project and Agent Stream name.

Set the project and Agent Stream for a single scope in Python

You can set the project and Agent Stream for a scope using the Python splunk_ao_context, object in a with statement. Every log inside this block, including nested calls, will use the specified project and Agent Stream.
This also works with the @log decorator.
Third-party integrations like the OpenAI wrapper also support this.

Nesting scopes

You can nest splunk_ao_context calls to temporarily override the project or Agent Stream:

Get the current logger

The Splunk Agent Observability context management keeps track of loggers. You can get the current logger, which will create a new one if there isn’t an existing logger.
If you are using any of the decorators, wrappers, or third-party integrations then this allows you to get the logger created by those components. For example, if you are adding a call inside a method decorated by the Python @log decorator or created automatically by an experiment, then this will return that logger instance so you can manually add additional spans.

Flush logs

To keep your app performant, logs are not continually flushed. In some cases, you may want to flush traces explicitly:
This approach is particularly useful for long-running applications where you need to control when traces are flushed to Splunk Agent Observability.
When using a Python Notebook (such as Jupyter, Google Colab, etc.), you should use the splunk_ao_context and make sure to call splunk_ao_context.flush() at the end of your notebook.

Manage sessions in Python

With the Python SDK, you can manage sessions from the context level in addition to the logger level. All the session management functions are applied to the current logger if called from the context. If you want to apply these to a specific logger instance, call the same methods directly on that logger instance.

Create a new session

To create a new session, use the start_session function.
When you start a session you can optionally give it a name. If you don’t provide a name, one is created for you based off the traces in the session. You can also provide an external ID to link a session in Splunk Agent Observability to an external identifier.

Continue an existing session

If you want to add a trace to an existing session, you can use the set_session function, passing the session ID. This is useful if you want to persist a session, for example saving a chatbot conversation with a user mid-conversation, then resuming the next time a user connects.
You can also continue a conversation using an external ID using the start_session function.

End a session

To stop logging to a session, you can clear the current session.

Next steps

Basic logging components

Splunk Agent Observability logger

Log with full control over sessions, traces, and spans using the Splunk Agent Observability logger.

Log decorator

Quickly add logging to your code with the log decorator and wrapper.

Integrations with third-party SDKs

OpenAI wrapper

Automatically log calls to the OpenAI SDK with a wrapper.

OpenAI Agents trace processor

Automatically log all the steps in your OpenAI Agent SDK apps using the Splunk Agent Observability trace processor.

LangChain callback

Automatically log all the steps in your LangChain or LangGraph application with the Splunk Agent Observability callback.