66
77Example usage:
88 from common.telemetry.telemetry_helper import trace_operation
9-
9+
1010 @trace_operation("cosmosdb_query")
1111 async def query_items(self, query: str):
1212 # Your CosmosDB query here
1313 pass
1414"""
1515
16+ import asyncio
1617import functools
1718from contextlib import asynccontextmanager , contextmanager
18- from typing import Any , Optional
19+ from typing import Optional
1920
2021from opentelemetry import trace
2122from opentelemetry .trace import Status , StatusCode
@@ -28,11 +29,11 @@ def get_tracer(name: str = __name__):
2829
2930def trace_operation (operation_name : str , attributes : Optional [dict ] = None ):
3031 """Decorator to add telemetry span to a function or method.
31-
32+
3233 Args:
3334 operation_name: Name of the operation for the span
3435 attributes: Optional dictionary of attributes to add to the span
35-
36+
3637 Example:
3738 @trace_operation("batch_processing", {"service": "sql_agents"})
3839 async def process_batch(batch_id: str):
@@ -48,10 +49,10 @@ async def async_wrapper(*args, **kwargs):
4849 if attributes :
4950 for key , value in attributes .items ():
5051 span .set_attribute (key , str (value ))
51-
52+
5253 # Add function arguments as attributes (optional, for debugging)
5354 span .set_attribute ("function" , func .__name__ )
54-
55+
5556 try :
5657 result = await func (* args , ** kwargs )
5758 span .set_status (Status (StatusCode .OK ))
@@ -60,17 +61,17 @@ async def async_wrapper(*args, **kwargs):
6061 span .record_exception (e )
6162 span .set_status (Status (StatusCode .ERROR , str (e )))
6263 raise
63-
64+
6465 @functools .wraps (func )
6566 def sync_wrapper (* args , ** kwargs ):
6667 tracer = get_tracer (func .__module__ )
6768 with tracer .start_as_current_span (operation_name ) as span :
6869 if attributes :
6970 for key , value in attributes .items ():
7071 span .set_attribute (key , str (value ))
71-
72+
7273 span .set_attribute ("function" , func .__name__ )
73-
74+
7475 try :
7576 result = func (* args , ** kwargs )
7677 span .set_status (Status (StatusCode .OK ))
@@ -79,24 +80,24 @@ def sync_wrapper(*args, **kwargs):
7980 span .record_exception (e )
8081 span .set_status (Status (StatusCode .ERROR , str (e )))
8182 raise
82-
83+
8384 # Return appropriate wrapper based on function type
8485 if asyncio .iscoroutinefunction (func ):
8586 return async_wrapper
8687 else :
8788 return sync_wrapper
88-
89+
8990 return decorator
9091
9192
9293@asynccontextmanager
9394async def trace_context (operation_name : str , attributes : Optional [dict ] = None ):
9495 """Async context manager for adding telemetry span to a code block.
95-
96+
9697 Args:
9798 operation_name: Name of the operation for the span
9899 attributes: Optional dictionary of attributes to add to the span
99-
100+
100101 Example:
101102 async with trace_context("cosmosdb_batch_query", {"batch_id": batch_id}):
102103 results = await database.query_items(query)
@@ -107,7 +108,7 @@ async def trace_context(operation_name: str, attributes: Optional[dict] = None):
107108 if attributes :
108109 for key , value in attributes .items ():
109110 span .set_attribute (key , str (value ))
110-
111+
111112 try :
112113 yield span
113114 span .set_status (Status (StatusCode .OK ))
@@ -120,11 +121,11 @@ async def trace_context(operation_name: str, attributes: Optional[dict] = None):
120121@contextmanager
121122def trace_sync_context (operation_name : str , attributes : Optional [dict ] = None ):
122123 """Sync context manager for adding telemetry span to a code block.
123-
124+
124125 Args:
125126 operation_name: Name of the operation for the span
126127 attributes: Optional dictionary of attributes to add to the span
127-
128+
128129 Example:
129130 with trace_sync_context("blob_upload", {"file_name": file_name}):
130131 blob_client.upload_blob(data)
@@ -134,7 +135,7 @@ def trace_sync_context(operation_name: str, attributes: Optional[dict] = None):
134135 if attributes :
135136 for key , value in attributes .items ():
136137 span .set_attribute (key , str (value ))
137-
138+
138139 try :
139140 yield span
140141 span .set_status (Status (StatusCode .OK ))
@@ -146,17 +147,14 @@ def trace_sync_context(operation_name: str, attributes: Optional[dict] = None):
146147
147148def add_span_attributes (attributes : dict ):
148149 """Add attributes to the current span.
149-
150+
150151 Args:
151152 attributes: Dictionary of attributes to add
152-
153+
153154 Example:
154155 add_span_attributes({"user_id": user_id, "batch_id": batch_id})
155156 """
156157 span = trace .get_current_span ()
157158 if span and span .is_recording ():
158159 for key , value in attributes .items ():
159160 span .set_attribute (key , str (value ))
160-
161-
162- import asyncio
0 commit comments