|
| 1 | +# Custom Instrumentation with Monocle |
| 2 | + |
| 3 | +This guide demonstrates how to use Monocle to instrument OpenAI and Vector DB interactions, collecting telemetry data to analyze and monitor their performance. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The example includes the following components: |
| 8 | + |
| 9 | +- **OpenAI Client (`openai_client.py`)**: A client for interacting with OpenAI's Chat API |
| 10 | +- **Vector Database (`vector_db.py`)**: An in-memory vector database with OpenAI embeddings |
| 11 | +- **Output Processors**: Configuration files that define how to extract and structure telemetry data |
| 12 | +- **Example script**: Shows how to instrument and run the application |
| 13 | + |
| 14 | +## Component Details |
| 15 | + |
| 16 | +### OpenAI Client |
| 17 | + |
| 18 | +`OpenAIClient` is a wrapper around the OpenAI API that provides methods for: |
| 19 | + |
| 20 | +- Making chat completion requests via the `chat()` method |
| 21 | +- Formatting messages for the API using `format_messages()` |
| 22 | +- Handling API responses and errors |
| 23 | + |
| 24 | +```python |
| 25 | +# Initialize client |
| 26 | +client = OpenAIClient() |
| 27 | + |
| 28 | +# Format messages and send to OpenAI |
| 29 | +messages = client.format_messages( |
| 30 | + system_prompts=["You are a helpful assistant."], |
| 31 | + user_prompts=["Tell me a joke about programming."] |
| 32 | +) |
| 33 | +response = client.chat(messages=messages, model="gpt-3.5-turbo") |
| 34 | +``` |
| 35 | + |
| 36 | +### Vector Database |
| 37 | + |
| 38 | +`InMemoryVectorDB` is a simple vector database implementation that: |
| 39 | + |
| 40 | +- Converts text to vector embeddings using OpenAI's embedding API |
| 41 | +- Stores vectors with associated metadata |
| 42 | +- Performs similarity searches using cosine similarity |
| 43 | + |
| 44 | +```python |
| 45 | +# Initialize vector database |
| 46 | +vector_db = InMemoryVectorDB() |
| 47 | + |
| 48 | +# Store documents |
| 49 | +vector_db.store_text("doc1", "Python is a programming language", {"source": "docs"}) |
| 50 | + |
| 51 | +# Search for similar documents |
| 52 | +results = vector_db.search_by_text("programming languages", top_k=2) |
| 53 | +``` |
| 54 | + |
| 55 | +## Instrumenting Your Code with Monocle |
| 56 | + |
| 57 | +### 1. Define Output Processors |
| 58 | + |
| 59 | +Output processors define what data to extract from your methods. Two examples are provided: |
| 60 | + |
| 61 | +#### Inference Output Processor |
| 62 | + |
| 63 | +`output_processor_inference.py` defines how to extract data from OpenAI chat completions: |
| 64 | + |
| 65 | +```python |
| 66 | +INFERENCE_OUTPUT_PROCESSOR = { |
| 67 | + "type": "inference", |
| 68 | + "attributes": [ |
| 69 | + [ |
| 70 | + # Entity attributes for the provider |
| 71 | + { |
| 72 | + "attribute": "type", |
| 73 | + "accessor": lambda arguments: "openai" |
| 74 | + }, |
| 75 | + { |
| 76 | + "attribute": "deployment", |
| 77 | + "accessor": lambda arguments: arguments['kwargs'].get('model', 'unknown') |
| 78 | + }, |
| 79 | + # More attributes... |
| 80 | + ] |
| 81 | + ], |
| 82 | + "events": [ |
| 83 | + { |
| 84 | + "name": "data.input", |
| 85 | + "attributes": [ |
| 86 | + { |
| 87 | + "attribute": "input", |
| 88 | + "accessor": lambda arguments: [ |
| 89 | + msg["content"] |
| 90 | + for msg in arguments['kwargs'].get('messages', []) |
| 91 | + ] if isinstance(arguments['kwargs'].get('messages'), list) else [] |
| 92 | + } |
| 93 | + ] |
| 94 | + }, |
| 95 | + # More events... |
| 96 | + ] |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +#### Vector DB Output Processor |
| 101 | + |
| 102 | +`output_processor_vector.py` defines how to extract data from vector database operations: |
| 103 | + |
| 104 | +```python |
| 105 | +VECTOR_OUTPUT_PROCESSOR = { |
| 106 | + "type": "retrieval", |
| 107 | + "attributes": [ |
| 108 | + [ |
| 109 | + # Vector store attributes |
| 110 | + { |
| 111 | + "attribute": "name", |
| 112 | + "accessor": lambda arguments: type(arguments["instance"]).__name__, |
| 113 | + }, |
| 114 | + # More attributes... |
| 115 | + ] |
| 116 | + ], |
| 117 | + "events": [ |
| 118 | + { |
| 119 | + "name": "data.input", |
| 120 | + "attributes": [ |
| 121 | + { |
| 122 | + "attribute": "input", |
| 123 | + "accessor": lambda arguments: arguments["args"][0] if arguments["args"] else None |
| 124 | + } |
| 125 | + ] |
| 126 | + }, |
| 127 | + # More events... |
| 128 | + ] |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### 2. Accessor Functions |
| 133 | + |
| 134 | +The key to instrumentation is the `accessor` function, which extracts data from method calls: |
| 135 | + |
| 136 | +- `arguments["instance"]`: The object instance (e.g., the OpenAIClient or InMemoryVectorDB) |
| 137 | +- `arguments["args"]`: Positional arguments passed to the method |
| 138 | +- `arguments["kwargs"]`: Keyword arguments passed to the method |
| 139 | +- `arguments["result"]`: The return value from the method call |
| 140 | + |
| 141 | +These give you access to all inputs, outputs, and context of the instrumented methods. |
| 142 | + |
| 143 | +### 3. Configure Instrumentation |
| 144 | + |
| 145 | +Set up Monocle's telemetry system with your output processors: |
| 146 | + |
| 147 | +```python |
| 148 | +from monocle_apptrace.instrumentation.common.wrapper_method import WrapperMethod |
| 149 | +from monocle_apptrace.instrumentation.common.instrumentor import setup_monocle_telemetry |
| 150 | + |
| 151 | +setup_monocle_telemetry( |
| 152 | + workflow_name="openai.app", |
| 153 | + wrapper_methods=[ |
| 154 | + WrapperMethod( |
| 155 | + package="openai_client", # Module name |
| 156 | + object_name="OpenAIClient", # Class name |
| 157 | + method="chat", # Method to instrument |
| 158 | + span_name="openai_client.chat", # Span name in telemetry |
| 159 | + output_processor=INFERENCE_OUTPUT_PROCESSOR |
| 160 | + ), |
| 161 | + # More method wrappers... |
| 162 | + ] |
| 163 | +) |
| 164 | +``` |
| 165 | + |
| 166 | +## Running the Example |
| 167 | + |
| 168 | +1. Ensure you have your **OpenAI API key** available: |
| 169 | + |
| 170 | +```bash |
| 171 | +export OPENAI_API_KEY=your_api_key_here |
| 172 | +``` |
| 173 | + |
| 174 | +2. Install the required packages: |
| 175 | + |
| 176 | +```bash |
| 177 | +pip install -r requirements.txt |
| 178 | +``` |
| 179 | + |
| 180 | +3. Run the example script: |
| 181 | + |
| 182 | +```bash |
| 183 | +python example.py |
| 184 | +# Or use the provided shell script |
| 185 | +./run_example.sh |
| 186 | +``` |
| 187 | + |
| 188 | +## Understanding the Telemetry Output |
| 189 | + |
| 190 | +Monocle generates JSON trace files in your directory with names like: |
| 191 | +`monocle_trace_openai.app_<trace_id>_<timestamp>.json` |
| 192 | + |
| 193 | +### Output Format |
| 194 | + |
| 195 | +The trace files contain structured telemetry data: |
| 196 | + |
| 197 | +```json |
| 198 | +{ |
| 199 | + "name": "openai_client.chat", |
| 200 | + "context": { /* trace context */ }, |
| 201 | + "attributes": { |
| 202 | + "entity.2.type": "openai", |
| 203 | + "entity.2.provider_name": "OpenAI", |
| 204 | + "entity.2.deployment": "gpt-3.5-turbo", |
| 205 | + "entity.2.inference_endpoint": "https://api.openai.com/v1", |
| 206 | + "entity.3.name": "gpt-3.5-turbo", |
| 207 | + "entity.3.type": "model.llm.gpt-3.5-turbo" |
| 208 | + }, |
| 209 | + "events": [ |
| 210 | + { |
| 211 | + "name": "data.input", |
| 212 | + "timestamp": "2025-02-27T10:36:49.985586Z", |
| 213 | + "attributes": { |
| 214 | + "input": [ |
| 215 | + "You are a helpful AI assistant.", |
| 216 | + "Tell me a short joke about programming." |
| 217 | + ] |
| 218 | + } |
| 219 | + }, |
| 220 | + { |
| 221 | + "name": "data.output", |
| 222 | + "attributes": { |
| 223 | + "response": "Why do programmers prefer dark mode? Because the light attracts bugs!" |
| 224 | + } |
| 225 | + }, |
| 226 | + { |
| 227 | + "name": "metadata", |
| 228 | + "attributes": { |
| 229 | + "prompt_tokens": 26, |
| 230 | + "completion_tokens": 14, |
| 231 | + "total_tokens": 40 |
| 232 | + } |
| 233 | + } |
| 234 | + ] |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +### Key Elements |
| 239 | + |
| 240 | +1. **Attributes**: Contains information about the instrumented entity: |
| 241 | + - Model name and type |
| 242 | + - Deployment details |
| 243 | + - API endpoints |
| 244 | + - Provider information |
| 245 | + |
| 246 | +2. **Events**: Contains captured events during the method execution: |
| 247 | + - `data.input`: The inputs provided to the method |
| 248 | + - `data.output`: The response or results from the method |
| 249 | + - `metadata`: Additional information like token usage |
| 250 | + |
| 251 | +## Customizing for Your Application |
| 252 | + |
| 253 | +To instrument your own code: |
| 254 | + |
| 255 | +1. Create output processors tailored to your methods |
| 256 | +2. Use accessor functions to extract the data you need |
| 257 | +3. Set up telemetry with your method wrappers |
| 258 | +4. Run your application and analyze the generated traces |
| 259 | + |
| 260 | +By customizing the output processors, you can collect exactly the telemetry data you need from any Python method. |
0 commit comments