Skip to content

Commit 6c15e88

Browse files
authored
updating tracing documentation in readme (#41953)
* updating tracing documentation in readme * fixing spelling error * change to restart build
1 parent 5fe0132 commit 6c15e88

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ To report an issue with the client library, or request additional features, plea
5353
- [Tracing](#tracing)
5454
- [Installation](#installation)
5555
- [How to enable tracing](#how-to-enable-tracing)
56+
- [Enabling content recording](#enabling-content-recording)
5657
- [How to trace your own functions](#how-to-trace-your-own-functions)
58+
- [Adding custom attributes to spans](#adding-custom-attributes-to-spans)
5759
- [Troubleshooting](#troubleshooting)
5860
- [Logging](#logging)
5961
- [Reporting issues](#reporting-issues)
@@ -1564,6 +1566,14 @@ from azure.ai.agents.telemetry import enable_telemetry
15641566

15651567
enable_telemetry(destination=sys.stdout)
15661568
```
1569+
1570+
### Enabling content recording
1571+
Content recording controles whether message contents and tool call related details, such as parameters and return values, are captured with the traces.
1572+
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`.
1573+
1574+
1575+
**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.
1576+
15671577
### How to trace your own functions
15681578

15691579
The decorator `trace_function` is provided for tracing your own function calls using OpenTelemetry. By default the function name is used as the name for the span. Alternatively you can provide the name for the span as a parameter to the decorator.
@@ -1579,6 +1589,44 @@ Object types are omitted, and the corresponding parameter is not traced.
15791589

15801590
The parameters are recorded in attributes `code.function.parameter.<parameter_name>` and the return value is recorder in attribute `code.function.return.value`
15811591

1592+
### Adding custom attributes to spans
1593+
1594+
Define your own span processor which adds your custom attributes:
1595+
1596+
<!-- SNIPPET:sample_agents_basics_with_console_tracing_custom_attributes.custom_attribute_span_processor -->
1597+
1598+
```python
1599+
class CustomAttributeSpanProcessor(SpanProcessor):
1600+
def __init__(self):
1601+
pass
1602+
1603+
def on_start(self, span: Span, parent_context=None):
1604+
# Add this attribute to all spans
1605+
span.set_attribute("trace_sample.sessionid", "123")
1606+
1607+
# Add another attribute only to create_message spans
1608+
if span.name == "create_message":
1609+
span.set_attribute("trace_sample.message.context", "abc")
1610+
1611+
def on_end(self, span: ReadableSpan):
1612+
# Clean-up logic can be added here if necessary
1613+
pass
1614+
```
1615+
1616+
<!-- END SNIPPET -->
1617+
1618+
Add the span processor to trace provider:
1619+
1620+
<!-- SNIPPET:sample_agents_basics_with_console_tracing_custom_attributes.add_custom_span_processor_to_tracer_provider -->
1621+
1622+
```python
1623+
provider = cast(TracerProvider, trace.get_tracer_provider())
1624+
provider.add_span_processor(CustomAttributeSpanProcessor())
1625+
```
1626+
1627+
<!-- END SNIPPET -->
1628+
1629+
15821630
## Troubleshooting
15831631

15841632
### Logging

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
# Define the custom span processor that is used for adding the custom
5050
# attributes to spans when they are started.
51+
# [START custom_attribute_span_processor]
5152
class CustomAttributeSpanProcessor(SpanProcessor):
5253
def __init__(self):
5354
pass
@@ -63,7 +64,7 @@ def on_start(self, span: Span, parent_context=None):
6364
def on_end(self, span: ReadableSpan):
6465
# Clean-up logic can be added here if necessary
6566
pass
66-
67+
# [END custom_attribute_span_processor]
6768

6869
# Setup tracing to console
6970
# Requires opentelemetry-sdk
@@ -81,8 +82,10 @@ def on_end(self, span: ReadableSpan):
8182
)
8283

8384
# Add the custom span processor to the global tracer provider
85+
# [START add_custom_span_processor_to_tracer_provider]
8486
provider = cast(TracerProvider, trace.get_tracer_provider())
8587
provider.add_span_processor(CustomAttributeSpanProcessor())
88+
# [END add_custom_span_processor_to_tracer_provider]
8689

8790
scenario = os.path.basename(__file__)
8891
tracer = trace.get_tracer(__name__)

0 commit comments

Comments
 (0)