-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtelemetry-custom-attributes.py
More file actions
177 lines (143 loc) · 5.09 KB
/
telemetry-custom-attributes.py
File metadata and controls
177 lines (143 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# -------------------------------------------------------------------------
"""
FILE: telemetry-custom-attributes.py
DESCRIPTION:
This sample demonstrates how to add custom attributes to Voice Live
OpenTelemetry spans using a custom SpanProcessor. This is useful for
correlating Voice Live traces with application-specific context such
as session IDs, user IDs, or request identifiers.
The custom SpanProcessor code (marked with region tags) can be copied
into any existing Voice Live application.
USAGE:
python telemetry-custom-attributes.py
Set the environment variables with your own values before running:
1) AZURE_VOICELIVE_ENDPOINT - The Azure VoiceLive endpoint
2) AZURE_VOICELIVE_API_KEY - The Azure VoiceLive API key
REQUIREMENTS:
- azure-ai-voicelive
- opentelemetry-sdk
- azure-core-tracing-opentelemetry
"""
import asyncio
import os
from typing import cast
from azure.core.settings import settings
settings.tracing_implementation = "opentelemetry"
from opentelemetry import trace
from opentelemetry.sdk.trace import (
TracerProvider,
SpanProcessor,
ReadableSpan,
Span,
)
from opentelemetry.sdk.trace.export import (
SimpleSpanProcessor,
ConsoleSpanExporter,
)
from azure.ai.voicelive.telemetry import VoiceLiveInstrumentor
# <custom_span_processor>
class CustomAttributeSpanProcessor(SpanProcessor):
"""Add application-specific attributes to every span."""
def on_start(self, span: Span, parent_context=None):
# Add a session identifier to all spans.
span.set_attribute(
"app.session_id", "my-session-123"
)
# Tag send spans with extra context.
if span.name and span.name.startswith("send"):
span.set_attribute(
"app.send.context", "user-interaction"
)
# Tag receive spans with a priority level.
if span.name and span.name.startswith("recv"):
span.set_attribute(
"app.recv.priority", "normal"
)
def on_end(self, span: ReadableSpan):
pass
# </custom_span_processor>
# Set up tracing with the custom processor.
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(
SimpleSpanProcessor(ConsoleSpanExporter())
)
trace.set_tracer_provider(tracer_provider)
os.environ.setdefault(
"AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING", "true"
)
VoiceLiveInstrumentor().instrument()
# <add_custom_processor>
# Register the custom processor with the global provider.
provider = cast(TracerProvider, trace.get_tracer_provider())
provider.add_span_processor(CustomAttributeSpanProcessor())
# </add_custom_processor>
from azure.core.credentials import AzureKeyCredential
from azure.ai.voicelive.aio import connect
from azure.ai.voicelive.models import (
InputTextContentPart,
Modality,
OutputAudioFormat,
RequestSession,
ServerEventType,
ServerVad,
UserMessageItem,
)
tracer = trace.get_tracer(__name__)
async def main() -> None:
endpoint = os.environ["AZURE_VOICELIVE_ENDPOINT"]
api_key = os.environ["AZURE_VOICELIVE_API_KEY"]
model = os.environ.get(
"AZURE_VOICELIVE_MODEL", "gpt-realtime"
)
credential = AzureKeyCredential(api_key)
with tracer.start_as_current_span(
"telemetry-custom-attributes"
):
async with connect(
endpoint=endpoint,
credential=credential,
model=model,
) as connection:
print(f"Connected to VoiceLive at {endpoint}")
session_config = RequestSession(
modalities=[Modality.TEXT],
instructions=(
"You are a helpful assistant. "
"Say hello briefly."
),
turn_detection=ServerVad(
threshold=0.5,
prefix_padding_ms=300,
silence_duration_ms=500,
),
output_audio_format=OutputAudioFormat.PCM16,
)
await connection.session.update(
session=session_config
)
print("Session configured.")
await connection.conversation.item.create(
item=UserMessageItem(
content=[
InputTextContentPart(
text="Hello, tell me a joke"
)
]
)
)
await connection.response.create()
print("User message sent.")
async for event in connection:
event_type = getattr(event, "type", None)
print(f"Received event: {event_type}")
if event_type == ServerEventType.RESPONSE_DONE:
print("Response complete.")
break
print("Connection closed.")
tracer_provider.force_flush()
tracer_provider.shutdown()
if __name__ == "__main__":
asyncio.run(main())