|
| 1 | +--- |
| 2 | +title: Examples |
| 3 | +description: "Examples of using span metrics to debug performance issues and monitor application behavior." |
| 4 | +sidebar_order: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +<Alert> |
| 8 | + |
| 9 | +These examples assume you have already <PlatformLink to="/tracing/">set up tracing</PlatformLink> in your application. |
| 10 | + |
| 11 | +</Alert> |
| 12 | + |
| 13 | +This guide provides practical examples of using span metrics to solve common monitoring and debugging challenges. Each example includes the context, implementation, and specific benefits of tracking these metrics. |
| 14 | + |
| 15 | +## File Upload and Processing Pipeline |
| 16 | + |
| 17 | +**Challenge:** Understanding bottlenecks and failures in multi-step file processing operations. |
| 18 | + |
| 19 | +**Solution:** Track the entire file processing pipeline with detailed metrics at each stage. |
| 20 | + |
| 21 | +```javascript |
| 22 | +Sentry.startSpan( |
| 23 | + { |
| 24 | + name: 'File Upload and Processing', |
| 25 | + op: 'file.process', |
| 26 | + attributes: { |
| 27 | + // File metadata for correlation and context |
| 28 | + 'file.size_bytes': 15728640, // 15MB |
| 29 | + 'file.type': 'image/jpeg', |
| 30 | + 'file.name': 'user-profile.jpg', |
| 31 | + |
| 32 | + // Track each processing step for pipeline visibility |
| 33 | + 'processing.steps_completed': ['resize', 'compress', 'metadata'], |
| 34 | + 'processing.output_size_bytes': 524288, // 512KB |
| 35 | + 'processing.compression_ratio': 0.033, |
| 36 | + |
| 37 | + // Upload performance metrics |
| 38 | + 'upload.chunk_size': 1048576, // 1MB chunks |
| 39 | + 'upload.chunks_completed': 15, |
| 40 | + 'upload.storage_provider': 's3', |
| 41 | + 'upload.cdn_propagation_ms': 1500, |
| 42 | + |
| 43 | + // Error tracking |
| 44 | + 'error.count': 0 |
| 45 | + } |
| 46 | + }, |
| 47 | + async () => { |
| 48 | + // Your file processing implementation |
| 49 | + } |
| 50 | +); |
| 51 | +``` |
| 52 | + |
| 53 | +**Benefits:** |
| 54 | +- Identify which processing steps are taking longest |
| 55 | +- Track upload performance across different file sizes |
| 56 | +- Monitor CDN propagation delays |
| 57 | +- Calculate processing efficiency through compression ratios |
| 58 | +- Detect partial failures in the pipeline |
| 59 | + |
| 60 | +## LLM Integration Monitoring |
| 61 | + |
| 62 | +**Challenge:** Managing costs and performance of LLM API integrations while ensuring optimal user experience. |
| 63 | + |
| 64 | +**Solution:** Comprehensive tracking of token usage, timing, and configuration metrics. |
| 65 | + |
| 66 | +```javascript |
| 67 | +Sentry.startSpan( |
| 68 | + { |
| 69 | + name: 'LLM Generation', |
| 70 | + op: 'ai.completion', |
| 71 | + attributes: { |
| 72 | + // Model configuration for context |
| 73 | + 'llm.model': 'gpt-4', |
| 74 | + 'llm.temperature': 0.7, |
| 75 | + 'llm.max_tokens': 2000, |
| 76 | + 'llm.stream_mode': true, |
| 77 | + |
| 78 | + // Token usage for cost monitoring |
| 79 | + 'llm.prompt_tokens': 425, |
| 80 | + 'llm.completion_tokens': 632, |
| 81 | + 'llm.total_tokens': 1057, |
| 82 | + |
| 83 | + // Performance metrics |
| 84 | + 'llm.time_to_first_token_ms': 245, |
| 85 | + 'llm.total_duration_ms': 3250, |
| 86 | + |
| 87 | + // Request outcome |
| 88 | + 'llm.request_status': 'success' |
| 89 | + } |
| 90 | + }, |
| 91 | + async () => { |
| 92 | + // Your LLM API implementation |
| 93 | + } |
| 94 | +); |
| 95 | +``` |
| 96 | + |
| 97 | +**Benefits:** |
| 98 | +- Monitor API costs through token usage |
| 99 | +- Track user experience metrics like time to first token |
| 100 | +- Identify optimal model configurations |
| 101 | +- Debug streaming vs non-streaming performance |
| 102 | +- Correlate model parameters with response quality |
| 103 | + |
| 104 | +## E-Commerce Transaction Flow |
| 105 | + |
| 106 | +**Challenge:** Understanding the complete purchase flow and identifying revenue-impacting issues. |
| 107 | + |
| 108 | +**Solution:** Track the entire checkout process with business and technical metrics. |
| 109 | + |
| 110 | +```javascript |
| 111 | +Sentry.startSpan( |
| 112 | + { |
| 113 | + name: 'Purchase Transaction', |
| 114 | + op: 'commerce.checkout', |
| 115 | + attributes: { |
| 116 | + // Cart analytics |
| 117 | + 'cart.item_count': 3, |
| 118 | + 'cart.total_amount': 159.99, |
| 119 | + 'cart.currency': 'USD', |
| 120 | + 'cart.items': ['SKU123', 'SKU456', 'SKU789'], |
| 121 | + |
| 122 | + // Payment processing |
| 123 | + 'payment.provider': 'stripe', |
| 124 | + 'payment.method': 'credit_card', |
| 125 | + 'payment.status': 'success', |
| 126 | + |
| 127 | + // Transaction metadata |
| 128 | + 'transaction.id': 'ord_123456789', |
| 129 | + 'customer.type': 'returning', |
| 130 | + |
| 131 | + // Fulfillment details |
| 132 | + 'shipping.method': 'express', |
| 133 | + |
| 134 | + // Promotion tracking |
| 135 | + 'promotion.code_applied': 'SUMMER23', |
| 136 | + 'promotion.discount_amount': 20.00 |
| 137 | + } |
| 138 | + }, |
| 139 | + async () => { |
| 140 | + // Your checkout process implementation |
| 141 | + } |
| 142 | +); |
| 143 | +``` |
| 144 | + |
| 145 | +**Benefits:** |
| 146 | +- Track conversion rates by customer type |
| 147 | +- Monitor payment provider performance |
| 148 | +- Analyze promotion effectiveness |
| 149 | +- Identify abandoned cart patterns |
| 150 | +- Debug payment processing issues |
| 151 | + |
| 152 | +## API Integration Health |
| 153 | + |
| 154 | +**Challenge:** Maintaining reliability and performance of critical API integrations. |
| 155 | + |
| 156 | +**Solution:** Detailed tracking of API request patterns and performance. |
| 157 | + |
| 158 | +```javascript |
| 159 | +Sentry.startSpan( |
| 160 | + { |
| 161 | + name: 'External API Call', |
| 162 | + op: 'http.client', |
| 163 | + attributes: { |
| 164 | + // Request context |
| 165 | + 'http.endpoint': '/api/users', |
| 166 | + 'http.method': 'POST', |
| 167 | + |
| 168 | + // Performance metrics |
| 169 | + 'http.response_time_ms': 200, |
| 170 | + 'http.response_size_bytes': 2048, |
| 171 | + |
| 172 | + // Reliability metrics |
| 173 | + 'http.retry_count': 0, |
| 174 | + 'http.cache_status': 'miss', |
| 175 | + |
| 176 | + // Request outcome |
| 177 | + 'http.status_code': 200, |
| 178 | + 'http.error_type': null |
| 179 | + } |
| 180 | + }, |
| 181 | + async () => { |
| 182 | + // Your API call implementation |
| 183 | + } |
| 184 | +); |
| 185 | +``` |
| 186 | + |
| 187 | +**Benefits:** |
| 188 | +- Monitor API endpoint performance |
| 189 | +- Track retry patterns |
| 190 | +- Optimize cache usage |
| 191 | +- Identify slow or failing endpoints |
| 192 | +- Debug integration issues |
| 193 | + |
| 194 | +For more information about implementing these examples effectively, see our <PlatformLink to="/tracing/span-metrics/">Span Metrics guide</PlatformLink> which includes detailed best practices and implementation guidelines. |
0 commit comments