File size: 19,346 Bytes
3ef5d3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# Monitoring & Logging - BDR Agent Factory

## Overview

Comprehensive monitoring and logging infrastructure for tracking AI capability performance, detecting issues, and ensuring compliance.

---

## Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Monitoring Stack                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚   Metrics    β”‚  β”‚     Logs     β”‚  β”‚    Traces    β”‚     β”‚
β”‚  β”‚  (Prometheus)β”‚  β”‚ (Elasticsearch)β”‚ β”‚   (Jaeger)   β”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β”‚         β”‚                  β”‚                  β”‚             β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β”‚
β”‚                            β”‚                                β”‚
β”‚                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚                   β”‚    Grafana      β”‚                       β”‚
β”‚                   β”‚   Dashboards    β”‚                       β”‚
β”‚                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                       β”‚
β”‚                            β”‚                                β”‚
β”‚                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚                   β”‚  Alert Manager  β”‚                       β”‚
β”‚                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## 1. Metrics Collection

### Prometheus Configuration

```yaml
# prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'bdr-agent-factory'
    static_configs:
      - targets: ['localhost:8000']
    metrics_path: '/metrics'
    
  - job_name: 'capability-services'
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      - source_labels: [__meta_kubernetes_pod_label_app]
        regex: capability-.*
        action: keep
```

### Key Metrics

#### Request Metrics
```python
from prometheus_client import Counter, Histogram, Gauge

# Request counter
request_count = Counter(
    'capability_requests_total',
    'Total number of capability requests',
    ['capability_id', 'status', 'decision_type']
)

# Request duration
request_duration = Histogram(
    'capability_request_duration_seconds',
    'Request duration in seconds',
    ['capability_id'],
    buckets=[0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]
)

# Active requests
active_requests = Gauge(
    'capability_active_requests',
    'Number of active requests',
    ['capability_id']
)

# Error rate
error_count = Counter(
    'capability_errors_total',
    'Total number of errors',
    ['capability_id', 'error_type']
)
```

#### Model Performance Metrics
```python
# Model accuracy
model_accuracy = Gauge(
    'model_accuracy',
    'Model accuracy score',
    ['capability_id', 'model_version']
)

# Prediction confidence
prediction_confidence = Histogram(
    'prediction_confidence',
    'Prediction confidence scores',
    ['capability_id'],
    buckets=[0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99, 1.0]
)

# Model inference time
inference_time = Histogram(
    'model_inference_duration_seconds',
    'Model inference duration',
    ['capability_id', 'model_version']
)
```

#### Business Metrics
```python
# Claims processed
claims_processed = Counter(
    'claims_processed_total',
    'Total claims processed',
    ['system_id', 'decision_type']
)

# Fraud detected
fraud_detected = Counter(
    'fraud_cases_detected_total',
    'Total fraud cases detected',
    ['risk_level']
)

# Compliance violations
compliance_violations = Counter(
    'compliance_violations_total',
    'Total compliance violations',
    ['framework', 'violation_type']
)
```

### Metrics Instrumentation

```python
from prometheus_client import start_http_server
import time

class CapabilityMetrics:
    def __init__(self):
        self.request_count = request_count
        self.request_duration = request_duration
        self.active_requests = active_requests
        self.error_count = error_count
    
    def track_request(self, capability_id, func):
        """Decorator to track capability requests"""
        def wrapper(*args, **kwargs):
            self.active_requests.labels(capability_id=capability_id).inc()
            
            start_time = time.time()
            try:
                result = func(*args, **kwargs)
                status = 'success'
                decision_type = result.get('decision_type', 'unknown')
                return result
            except Exception as e:
                status = 'error'
                decision_type = 'error'
                self.error_count.labels(
                    capability_id=capability_id,
                    error_type=type(e).__name__
                ).inc()
                raise
            finally:
                duration = time.time() - start_time
                self.request_duration.labels(
                    capability_id=capability_id
                ).observe(duration)
                
                self.request_count.labels(
                    capability_id=capability_id,
                    status=status,
                    decision_type=decision_type
                ).inc()
                
                self.active_requests.labels(
                    capability_id=capability_id
                ).dec()
        
        return wrapper

# Start metrics server
start_http_server(8001)
```

---

## 2. Logging

### Structured Logging Configuration

```python
import logging
import json
from datetime import datetime

class StructuredLogger:
    def __init__(self, name):
        self.logger = logging.getLogger(name)
        self.logger.setLevel(logging.INFO)
        
        # JSON formatter
        handler = logging.StreamHandler()
        handler.setFormatter(self.JSONFormatter())
        self.logger.addHandler(handler)
    
    class JSONFormatter(logging.Formatter):
        def format(self, record):
            log_data = {
                'timestamp': datetime.utcnow().isoformat(),
                'level': record.levelname,
                'logger': record.name,
                'message': record.getMessage(),
                'module': record.module,
                'function': record.funcName,
                'line': record.lineno
            }
            
            # Add extra fields
            if hasattr(record, 'capability_id'):
                log_data['capability_id'] = record.capability_id
            if hasattr(record, 'request_id'):
                log_data['request_id'] = record.request_id
            if hasattr(record, 'user_id'):
                log_data['user_id'] = record.user_id
            if hasattr(record, 'system_id'):
                log_data['system_id'] = record.system_id
            
            return json.dumps(log_data)
    
    def info(self, message, **kwargs):
        self.logger.info(message, extra=kwargs)
    
    def warning(self, message, **kwargs):
        self.logger.warning(message, extra=kwargs)
    
    def error(self, message, **kwargs):
        self.logger.error(message, extra=kwargs)
    
    def debug(self, message, **kwargs):
        self.logger.debug(message, extra=kwargs)

# Usage
logger = StructuredLogger('bdr_agent_factory')

logger.info(
    'Capability invoked',
    capability_id='cap_text_classification',
    request_id='req_12345',
    user_id='user_789'
)
```

### Log Levels

- **DEBUG**: Detailed diagnostic information
- **INFO**: General informational messages
- **WARNING**: Warning messages for potentially harmful situations
- **ERROR**: Error events that might still allow the application to continue
- **CRITICAL**: Critical events that may cause the application to abort

### Log Categories

#### Application Logs
```python
logger.info('Application started', version='1.0.0')
logger.info('Capability registered', capability_id='cap_text_classification')
logger.warning('High memory usage detected', memory_usage_mb=8192)
```

#### Request Logs
```python
logger.info(
    'Request received',
    request_id='req_12345',
    capability_id='cap_text_classification',
    user_id='user_789',
    ip_address='192.168.1.1'
)

logger.info(
    'Request completed',
    request_id='req_12345',
    duration_ms=142,
    status='success'
)
```

#### Error Logs
```python
logger.error(
    'Capability invocation failed',
    request_id='req_12345',
    capability_id='cap_text_classification',
    error_type='ValidationError',
    error_message='Input text exceeds maximum length',
    stack_trace=traceback.format_exc()
)
```

#### Audit Logs
```python
logger.info(
    'Audit trail created',
    audit_id='audit_67890',
    request_id='req_12345',
    capability_id='cap_text_classification',
    user_id='user_789',
    decision_type='approve',
    compliance_flags={'gdpr': True, 'ifrs17': True}
)
```

#### Security Logs
```python
logger.warning(
    'Authentication failed',
    user_id='user_789',
    ip_address='192.168.1.1',
    reason='Invalid token'
)

logger.warning(
    'Rate limit exceeded',
    user_id='user_789',
    ip_address='192.168.1.1',
    requests_per_minute=150
)
```

### Elasticsearch Configuration

```yaml
# logstash.conf
input {
  file {
    path => "/var/log/bdr-agent-factory/*.log"
    codec => json
  }
}

filter {
  # Parse timestamp
  date {
    match => ["timestamp", "ISO8601"]
    target => "@timestamp"
  }
  
  # Add tags for different log types
  if [capability_id] {
    mutate {
      add_tag => ["capability_log"]
    }
  }
  
  if [audit_id] {
    mutate {
      add_tag => ["audit_log"]
    }
  }
}

output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "bdr-agent-factory-%{+YYYY.MM.dd}"
  }
}
```

---

## 3. Distributed Tracing

### Jaeger Configuration

```python
from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# Configure tracer
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

# Configure Jaeger exporter
jaeger_exporter = JaegerExporter(
    agent_host_name='localhost',
    agent_port=6831,
)

trace.get_tracer_provider().add_span_processor(
    BatchSpanProcessor(jaeger_exporter)
)

# Usage
with tracer.start_as_current_span('capability_invocation') as span:
    span.set_attribute('capability_id', 'cap_text_classification')
    span.set_attribute('request_id', 'req_12345')
    
    # Perform capability invocation
    result = invoke_capability()
    
    span.set_attribute('decision_type', result.decision_type)
    span.set_attribute('confidence', result.confidence)
```

---

## 4. Dashboards

### Grafana Dashboard Configuration

#### System Overview Dashboard

```json
{
  "dashboard": {
    "title": "BDR Agent Factory - System Overview",
    "panels": [
      {
        "title": "Request Rate",
        "targets": [
          {
            "expr": "rate(capability_requests_total[5m])"
          }
        ]
      },
      {
        "title": "Error Rate",
        "targets": [
          {
            "expr": "rate(capability_errors_total[5m])"
          }
        ]
      },
      {
        "title": "P95 Latency",
        "targets": [
          {
            "expr": "histogram_quantile(0.95, rate(capability_request_duration_seconds_bucket[5m]))"
          }
        ]
      },
      {
        "title": "Active Requests",
        "targets": [
          {
            "expr": "sum(capability_active_requests)"
          }
        ]
      }
    ]
  }
}
```

#### Capability Performance Dashboard

- **Request Volume by Capability**: Bar chart showing requests per capability
- **Latency Distribution**: Heatmap of latency percentiles
- **Error Rate by Capability**: Line chart of error rates
- **Model Accuracy**: Gauge showing current accuracy
- **Prediction Confidence**: Histogram of confidence scores

#### Compliance Dashboard

- **Compliance Rate**: Gauge showing overall compliance percentage
- **Violations by Framework**: Bar chart of violations per framework
- **Audit Trail Coverage**: Percentage of requests with audit trails
- **Data Retention Status**: Status of data retention policies

---

## 5. Alerting

### Alert Rules

```yaml
# prometheus-alerts.yml
groups:
  - name: capability_alerts
    interval: 30s
    rules:
      # High error rate
      - alert: HighErrorRate
        expr: |
          rate(capability_errors_total[5m]) > 0.05
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High error rate detected"
          description: "Error rate is {{ $value }} errors/sec for {{ $labels.capability_id }}"
      
      # High latency
      - alert: HighLatency
        expr: |
          histogram_quantile(0.95, rate(capability_request_duration_seconds_bucket[5m])) > 1.0
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High latency detected"
          description: "P95 latency is {{ $value }}s for {{ $labels.capability_id }}"
      
      # Low model accuracy
      - alert: LowModelAccuracy
        expr: |
          model_accuracy < 0.85
        for: 10m
        labels:
          severity: critical
        annotations:
          summary: "Model accuracy below threshold"
          description: "Model accuracy is {{ $value }} for {{ $labels.capability_id }}"
      
      # Compliance violation
      - alert: ComplianceViolation
        expr: |
          increase(compliance_violations_total[1h]) > 0
        labels:
          severity: critical
        annotations:
          summary: "Compliance violation detected"
          description: "{{ $value }} violations detected for {{ $labels.framework }}"
      
      # Service down
      - alert: ServiceDown
        expr: |
          up{job="bdr-agent-factory"} == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "Service is down"
          description: "BDR Agent Factory service is not responding"
```

### Alert Channels

```yaml
# alertmanager.yml
route:
  group_by: ['alertname', 'capability_id']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 12h
  receiver: 'default'
  routes:
    - match:
        severity: critical
      receiver: 'pagerduty'
    - match:
        severity: warning
      receiver: 'slack'

receivers:
  - name: 'default'
    email_configs:
      - to: 'ops@bdragentfactory.com'
  
  - name: 'slack'
    slack_configs:
      - api_url: 'https://hooks.slack.com/services/XXX'
        channel: '#alerts'
        title: 'BDR Agent Factory Alert'
  
  - name: 'pagerduty'
    pagerduty_configs:
      - service_key: 'XXX'
```

---

## 6. Log Retention

### Retention Policies

| Log Type | Retention Period | Storage |
|----------|------------------|----------|
| Application Logs | 30 days | Elasticsearch |
| Request Logs | 90 days | Elasticsearch |
| Audit Logs | 7 years | S3 + Elasticsearch |
| Error Logs | 1 year | Elasticsearch |
| Security Logs | 2 years | S3 + Elasticsearch |
| Metrics | 1 year | Prometheus |

### Elasticsearch Index Lifecycle Management

```json
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_size": "50GB",
            "max_age": "1d"
          }
        }
      },
      "warm": {
        "min_age": "7d",
        "actions": {
          "shrink": {
            "number_of_shards": 1
          },
          "forcemerge": {
            "max_num_segments": 1
          }
        }
      },
      "cold": {
        "min_age": "30d",
        "actions": {
          "freeze": {}
        }
      },
      "delete": {
        "min_age": "90d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}
```

---

## 7. Performance Monitoring

### SLA Monitoring

```python
class SLAMonitor:
    def __init__(self):
        self.sla_targets = {
            'availability': 0.999,  # 99.9% uptime
            'p95_latency_ms': 300,  # 300ms P95 latency
            'error_rate': 0.001,    # 0.1% error rate
        }
    
    def check_sla_compliance(self, time_window='24h'):
        """Check if SLAs are being met"""
        metrics = self.get_metrics(time_window)
        
        compliance = {
            'availability': metrics['uptime'] >= self.sla_targets['availability'],
            'latency': metrics['p95_latency_ms'] <= self.sla_targets['p95_latency_ms'],
            'error_rate': metrics['error_rate'] <= self.sla_targets['error_rate']
        }
        
        return all(compliance.values()), compliance
```

---

## 8. Troubleshooting

### Common Issues

#### High Latency
```bash
# Check P95 latency by capability
curl -G 'http://localhost:9090/api/v1/query' \
  --data-urlencode 'query=histogram_quantile(0.95, rate(capability_request_duration_seconds_bucket[5m]))'

# Check slow queries in logs
curl -X GET "localhost:9200/bdr-agent-factory-*/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "range": {
      "duration_ms": { "gte": 1000 }
    }
  },
  "sort": [{ "duration_ms": "desc" }]
}'
```

#### High Error Rate
```bash
# Check error distribution
curl -G 'http://localhost:9090/api/v1/query' \
  --data-urlencode 'query=sum by (error_type) (rate(capability_errors_total[5m]))'

# View recent errors
curl -X GET "localhost:9200/bdr-agent-factory-*/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": { "level": "ERROR" }
  },
  "sort": [{ "timestamp": "desc" }],
  "size": 100
}'
```

---

## 9. Best Practices

1. **Use structured logging** - Always log in JSON format
2. **Include context** - Add request_id, user_id, capability_id to all logs
3. **Monitor SLAs** - Track availability, latency, and error rates
4. **Set up alerts** - Configure alerts for critical metrics
5. **Retain audit logs** - Keep audit logs for compliance requirements
6. **Use distributed tracing** - Track requests across services
7. **Dashboard everything** - Create dashboards for all key metrics
8. **Regular reviews** - Review logs and metrics regularly
9. **Optimize queries** - Use efficient Elasticsearch queries
10. **Archive old data** - Move old logs to cold storage

---

## Support

For monitoring support:
- Documentation: https://docs.bdragentfactory.com/monitoring
- Email: ops@bdragentfactory.com