Skip to content

Commit 2dcb122

Browse files
authored
Support OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to turn on message tracing in Agents SDK (#42445)
1 parent 41663d6 commit 2dcb122

14 files changed

+118
-28
lines changed

sdk/ai/azure-ai-agents/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
### Features Added
77

88
- Add support for Browser Automation tool.
9+
- Add support for environment variable `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` as defined by
10+
[OpenTelemetry](https://opentelemetry.io/), to control tracing of user and Agent messages content.
911

1012
### Bugs Fixed
1113

sdk/ai/azure-ai-agents/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,11 +1616,15 @@ enable_telemetry(destination=sys.stdout)
16161616
```
16171617

16181618
### Enabling content recording
1619-
Content recording controles whether message contents and tool call related details, such as parameters and return values, are captured with the traces.
1620-
To enable content recording set the `AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED` environment variable value to `true`. If the environment variable is not set, then the value will default to `false`.
16211619

1620+
Content recording controls whether message contents and tool call related details, such as parameters and return values, are captured with the traces. This data may include sensitive user information.
16221621

1623-
**Important:** The `AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED` environment variable only controls content recording for built-in agent traces. When you use the `@trace_function` decorator on your own functions, all parameters and return values are always traced.
1622+
To enable content recording set the `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` environment variable to `true`. This environment variable is defined
1623+
by [OpenTelemetry](https://opentelemetry.io/), and all new applications are encouraged to use it when content recording is required. For legacy reasons, content recordings will also be enabled if `AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED` environment variable is set to `true`.
1624+
1625+
If neither environment variable is set, content recording defaults to `false`. If either variable is set to `false`, content recording will be disabled, regardless of the other's value.
1626+
1627+
**Important:** The environment variables only control content recording for built-in agent traces. When you use the `@trace_function` decorator on your own functions, all parameters and return values are always traced.
16241628

16251629
### How to trace your own functions
16261630

sdk/ai/azure-ai-agents/azure/ai/agents/telemetry/_ai_agents_instrumentor.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,16 @@ def instrument(self, enable_content_recording: Optional[bool] = None) -> None:
127127
:param enable_content_recording: Whether content recording is enabled as part
128128
of the traces or not. Content in this context refers to chat message content
129129
and function call tool related function names, function parameter names and
130-
values. True will enable content recording, False will disable it. If no value
131-
is provided, then the value read from environment variable
132-
AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED is used. If the environment variable
133-
is not found, then the value will default to False. Please note that successive calls
130+
values. `True` will enable content recording, `False` will disable it. If no value
131+
is provided, then the value read from environment variables
132+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT and
133+
AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED is used. The latter is supported
134+
for legacy reasons. Applications are encouraged to use the former, which is defined by OTEL.
135+
If the environment variable is not found, then the value will default to `False`.
136+
Please note that successive calls
134137
to instrument will always apply the content recording value provided with the most
135138
recent call to instrument (including applying the environment variable if no value is
136-
provided and defaulting to false if the environment variable is not found), even if
139+
provided and defaulting to `False` if the environment variable is not found), even if
137140
instrument was already previously called without uninstrument being called in between
138141
the instrument calls.
139142
:type enable_content_recording: bool, optional
@@ -187,18 +190,47 @@ def instrument(self, enable_content_recording: Optional[bool] = None):
187190
Enable trace instrumentation for AI Agents.
188191
189192
:param enable_content_recording: Whether content recording is enabled as part
190-
of the traces or not. Content in this context refers to chat message content
191-
and function call tool related function names, function parameter names and
192-
values. True will enable content recording, False will disable it. If no value
193-
is provided, then the value read from environment variable
194-
AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED is used. If the environment variable
195-
is not found, then the value will default to False.
196-
193+
of the traces or not. Content in this context refers to chat message content
194+
and function call tool related function names, function parameter names and
195+
values. `True` will enable content recording, `False` will disable it. If no value
196+
is provided, then the value read from environment variables
197+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT and
198+
AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED is used. The latter is supported
199+
for legacy reasons. Applications are encouraged to use the former, which is defined by OTEL.
200+
If the environment variable is not found, then the value will default to `False`.
201+
Please note that successive calls
202+
to instrument will always apply the content recording value provided with the most
203+
recent call to instrument (including applying the environment variable if no value is
204+
provided and defaulting to `False` if the environment variable is not found), even if
205+
instrument was already previously called without uninstrument being called in between
206+
the instrument calls.
197207
:type enable_content_recording: bool, optional
208+
198209
"""
199210
if enable_content_recording is None:
200-
var_value = os.environ.get("AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED")
211+
212+
# AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED was introduced before the standard
213+
# OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT was defined by OTEL. Support both
214+
# of them moving forward, but only document the standard one.
215+
var_value_new = os.environ.get("OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT")
216+
var_value_old = os.environ.get("AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED")
217+
var_value: Optional[str] = None
218+
219+
if var_value_new and var_value_old and var_value_new != var_value_old:
220+
logger.error(
221+
"Environment variables OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT "
222+
"and AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED are both set, and "
223+
"their values differ. Message content recording in this run will be disabled "
224+
"as a result. Please set OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT only."
225+
)
226+
var_value = None
227+
elif var_value_new:
228+
var_value = var_value_new
229+
elif var_value_old:
230+
var_value = var_value_old
231+
201232
enable_content_recording = self._str_to_bool(var_value)
233+
202234
if not self.is_instrumented():
203235
self._instrument_agents(enable_content_recording)
204236
else:

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_async_with_azure_monitor_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Set these environment variables with your own values:
2121
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
2222
page of your Azure AI Foundry portal.
23-
2) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
23+
2) OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat
2424
messages, which may contain personal data. False by default.
2525
3) APPLICATIONINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
2626
This is used to send telemetry data to Azure Monitor. You can also get the connection string programmatically

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_async_with_console_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Set these environment variables with your own values:
2626
* PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
2727
page of your Azure AI Foundry portal.
28-
* AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
28+
* OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat
2929
messages, which may contain personal data. False by default.
3030
"""
3131
import asyncio

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_with_azure_monitor_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
page of your Azure AI Foundry portal.
2323
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
2424
the "Models + endpoints" tab in your Azure AI Foundry project.
25-
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
25+
3) OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat
2626
messages, which may contain personal data. False by default.
2727
4) APPLICATIONINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
2828
This is used to send telemetry data to Azure Monitor. You can also get the connection string programmatically

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_with_console_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
page of your Azure AI Foundry portal.
2727
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
2828
the "Models + endpoints" tab in your Azure AI Foundry project.
29-
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
29+
3) OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat
3030
messages, which may contain personal data. False by default.
3131
"""
3232

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_with_console_tracing_custom_attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
page of your Azure AI Foundry portal.
2828
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
2929
the "Models + endpoints" tab in your Azure AI Foundry project.
30-
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
30+
3) OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat
3131
messages, which may contain personal data. False by default.
3232
"""
3333

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_stream_eventhandler_with_azure_monitor_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
page of your Azure AI Foundry portal.
2323
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
2424
the "Models + endpoints" tab in your Azure AI Foundry project.
25-
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
25+
3) OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat
2626
messages, which may contain personal data. False by default.
2727
4) APPLICATIONINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
2828
This is used to send telemetry data to Azure Monitor. You can also get the connection string programmatically

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_stream_eventhandler_with_console_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
page of your Azure AI Foundry portal.
2727
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
2828
the "Models + endpoints" tab in your Azure AI Foundry project.
29-
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
29+
3) OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat
3030
messages, which may contain personal data. False by default.
3131
"""
3232

0 commit comments

Comments
 (0)