-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
862 lines (702 loc) · 20.1 KB
/
middleware.go
File metadata and controls
862 lines (702 loc) · 20.1 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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
package sdk
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/xraph/ai-sdk/llm"
logger "github.com/xraph/go-utils/log"
"github.com/xraph/go-utils/metrics"
)
// Middleware intercepts and processes AI requests/responses.
type Middleware interface {
// Name returns the middleware name.
Name() string
// ProcessRequest processes a request and optionally short-circuits with a response.
ProcessRequest(ctx context.Context, req *MiddlewareRequest, next MiddlewareHandler) (*MiddlewareResponse, error)
}
// MiddlewareHandler is the next handler in the chain.
type MiddlewareHandler func(ctx context.Context, req *MiddlewareRequest) (*MiddlewareResponse, error)
// MiddlewareRequest wraps a chat request with metadata.
type MiddlewareRequest struct {
ChatRequest llm.ChatRequest
Metadata map[string]any
StartTime time.Time
}
// MiddlewareResponse wraps a chat response with metadata.
type MiddlewareResponse struct {
ChatResponse llm.ChatResponse
Metadata map[string]any
Cached bool
CacheKey string
Duration time.Duration
}
// MiddlewareChain manages a chain of middleware.
type MiddlewareChain struct {
middlewares []Middleware
logger logger.Logger
metrics metrics.Metrics
}
// NewMiddlewareChain creates a new middleware chain.
func NewMiddlewareChain(logger logger.Logger, metrics metrics.Metrics) *MiddlewareChain {
return &MiddlewareChain{
middlewares: make([]Middleware, 0),
logger: logger,
metrics: metrics,
}
}
// Use adds a middleware to the chain.
func (c *MiddlewareChain) Use(m Middleware) *MiddlewareChain {
c.middlewares = append(c.middlewares, m)
return c
}
// Execute executes the middleware chain.
func (c *MiddlewareChain) Execute(ctx context.Context, req *MiddlewareRequest, final MiddlewareHandler) (*MiddlewareResponse, error) {
if req.Metadata == nil {
req.Metadata = make(map[string]any)
}
req.StartTime = time.Now()
// Build the chain from end to start
handler := final
for i := len(c.middlewares) - 1; i >= 0; i-- {
m := c.middlewares[i]
next := handler
handler = func(ctx context.Context, req *MiddlewareRequest) (*MiddlewareResponse, error) {
return m.ProcessRequest(ctx, req, next)
}
}
return handler(ctx, req)
}
// Len returns the number of middleware in the chain.
func (c *MiddlewareChain) Len() int {
return len(c.middlewares)
}
// LoggingMiddleware logs requests and responses.
type LoggingMiddleware struct {
logger logger.Logger
logRequest bool
logResponse bool
redactKeys []string
}
// LoggingConfig configures logging middleware.
type LoggingConfig struct {
LogRequest bool
LogResponse bool
RedactKeys []string // Keys to redact from logs
}
// NewLoggingMiddleware creates a logging middleware.
func NewLoggingMiddleware(logger logger.Logger, config LoggingConfig) *LoggingMiddleware {
return &LoggingMiddleware{
logger: logger,
logRequest: config.LogRequest,
logResponse: config.LogResponse,
redactKeys: config.RedactKeys,
}
}
// Name implements Middleware.
func (m *LoggingMiddleware) Name() string {
return "logging"
}
// ProcessRequest implements Middleware.
func (m *LoggingMiddleware) ProcessRequest(ctx context.Context, req *MiddlewareRequest, next MiddlewareHandler) (*MiddlewareResponse, error) {
startTime := time.Now()
// Log request
if m.logRequest && m.logger != nil {
m.logger.Info("AI request",
logger.String("provider", req.ChatRequest.Provider),
logger.String("model", req.ChatRequest.Model),
logger.Int("messages_count", len(req.ChatRequest.Messages)),
)
}
// Call next
resp, err := next(ctx, req)
duration := time.Since(startTime)
// Log response or error
if m.logger != nil {
if err != nil {
m.logger.Error("AI request failed",
logger.String("provider", req.ChatRequest.Provider),
logger.String("model", req.ChatRequest.Model),
logger.Duration("duration", duration),
logger.String("error", err.Error()),
)
} else if m.logResponse {
usage := "none"
if resp.ChatResponse.Usage != nil {
usage = fmt.Sprintf("%d tokens", resp.ChatResponse.Usage.TotalTokens)
}
m.logger.Info("AI response",
logger.String("provider", req.ChatRequest.Provider),
logger.String("model", req.ChatRequest.Model),
logger.Duration("duration", duration),
logger.Bool("cached", resp.Cached),
logger.String("usage", usage),
)
}
}
if resp != nil {
resp.Duration = duration
}
return resp, err
}
// CachingMiddleware caches responses.
type CachingMiddleware struct {
cache Cache
ttl time.Duration
keyFunc func(*MiddlewareRequest) string
skipCache func(*MiddlewareRequest) bool
}
// Cache interface for caching middleware.
type Cache interface {
Get(ctx context.Context, key string) ([]byte, bool)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
}
// CachingConfig configures caching middleware.
type CachingConfig struct {
Cache Cache
TTL time.Duration
KeyFunc func(*MiddlewareRequest) string
SkipCache func(*MiddlewareRequest) bool
}
// NewCachingMiddleware creates a caching middleware.
func NewCachingMiddleware(config CachingConfig) *CachingMiddleware {
keyFunc := config.KeyFunc
if keyFunc == nil {
keyFunc = DefaultCacheKeyFunc
}
return &CachingMiddleware{
cache: config.Cache,
ttl: config.TTL,
keyFunc: keyFunc,
skipCache: config.SkipCache,
}
}
// Name implements Middleware.
func (m *CachingMiddleware) Name() string {
return "caching"
}
// ProcessRequest implements Middleware.
func (m *CachingMiddleware) ProcessRequest(ctx context.Context, req *MiddlewareRequest, next MiddlewareHandler) (*MiddlewareResponse, error) {
// Check if caching should be skipped
if m.skipCache != nil && m.skipCache(req) {
return next(ctx, req)
}
// Generate cache key
key := m.keyFunc(req)
// Try cache lookup
if data, ok := m.cache.Get(ctx, key); ok {
var cached llm.ChatResponse
if err := json.Unmarshal(data, &cached); err == nil {
return &MiddlewareResponse{
ChatResponse: cached,
Cached: true,
CacheKey: key,
}, nil
}
}
// Call next
resp, err := next(ctx, req)
if err != nil {
return resp, err
}
// Cache response
if data, err := json.Marshal(resp.ChatResponse); err == nil {
_ = m.cache.Set(ctx, key, data, m.ttl)
}
resp.CacheKey = key
return resp, nil
}
// DefaultCacheKeyFunc generates a default cache key from the request.
func DefaultCacheKeyFunc(req *MiddlewareRequest) string {
hasher := sha256.New()
hasher.Write([]byte(req.ChatRequest.Provider))
hasher.Write([]byte(req.ChatRequest.Model))
for _, msg := range req.ChatRequest.Messages {
hasher.Write([]byte(msg.Role))
hasher.Write([]byte(msg.Content))
}
if req.ChatRequest.Temperature != nil {
_, _ = fmt.Fprintf(hasher, "%f", *req.ChatRequest.Temperature)
}
return hex.EncodeToString(hasher.Sum(nil))
}
// RateLimitMiddleware implements rate limiting.
type RateLimitMiddleware struct {
limiter MiddlewareRateLimiter
keyFunc func(*MiddlewareRequest) string
onRejected func(*MiddlewareRequest) error
}
// MiddlewareRateLimiter interface for rate limiting in middleware.
type MiddlewareRateLimiter interface {
Allow(key string) bool
AllowN(key string, n int) bool
Reset(key string)
}
// MiddlewareRateLimitConfig configures rate limiting middleware.
type MiddlewareRateLimitConfig struct {
Limiter MiddlewareRateLimiter
KeyFunc func(*MiddlewareRequest) string
OnRejected func(*MiddlewareRequest) error
}
// NewRateLimitMiddleware creates a rate limiting middleware.
func NewRateLimitMiddleware(config MiddlewareRateLimitConfig) *RateLimitMiddleware {
keyFunc := config.KeyFunc
if keyFunc == nil {
keyFunc = func(req *MiddlewareRequest) string {
return req.ChatRequest.Provider + ":" + req.ChatRequest.Model
}
}
return &RateLimitMiddleware{
limiter: config.Limiter,
keyFunc: keyFunc,
onRejected: config.OnRejected,
}
}
// Name implements Middleware.
func (m *RateLimitMiddleware) Name() string {
return "rate_limit"
}
// ProcessRequest implements Middleware.
func (m *RateLimitMiddleware) ProcessRequest(ctx context.Context, req *MiddlewareRequest, next MiddlewareHandler) (*MiddlewareResponse, error) {
key := m.keyFunc(req)
if !m.limiter.Allow(key) {
if m.onRejected != nil {
return nil, m.onRejected(req)
}
return nil, fmt.Errorf("rate limit exceeded for %s", key)
}
return next(ctx, req)
}
// TokenBucketLimiter implements token bucket rate limiting.
type TokenBucketLimiter struct {
buckets map[string]*tokenBucket
rate float64 // tokens per second
capacity int
mu sync.Mutex
}
type tokenBucket struct {
tokens float64
lastUpdate time.Time
}
// NewTokenBucketLimiter creates a token bucket rate limiter.
func NewTokenBucketLimiter(rate float64, capacity int) *TokenBucketLimiter {
return &TokenBucketLimiter{
buckets: make(map[string]*tokenBucket),
rate: rate,
capacity: capacity,
}
}
// Allow implements RateLimiter.
func (l *TokenBucketLimiter) Allow(key string) bool {
return l.AllowN(key, 1)
}
// AllowN implements RateLimiter.
func (l *TokenBucketLimiter) AllowN(key string, n int) bool {
l.mu.Lock()
defer l.mu.Unlock()
bucket, ok := l.buckets[key]
if !ok {
bucket = &tokenBucket{
tokens: float64(l.capacity),
lastUpdate: time.Now(),
}
l.buckets[key] = bucket
}
// Refill tokens based on elapsed time
now := time.Now()
elapsed := now.Sub(bucket.lastUpdate).Seconds()
bucket.tokens += elapsed * l.rate
if bucket.tokens > float64(l.capacity) {
bucket.tokens = float64(l.capacity)
}
bucket.lastUpdate = now
// Try to consume tokens
if bucket.tokens >= float64(n) {
bucket.tokens -= float64(n)
return true
}
return false
}
// Reset implements RateLimiter.
func (l *TokenBucketLimiter) Reset(key string) {
l.mu.Lock()
defer l.mu.Unlock()
delete(l.buckets, key)
}
// SlidingWindowLimiter implements sliding window rate limiting.
type SlidingWindowLimiter struct {
windows map[string]*slidingWindow
limit int
window time.Duration
mu sync.Mutex
}
type slidingWindow struct {
count int
windowStart time.Time
}
// NewSlidingWindowLimiter creates a sliding window rate limiter.
func NewSlidingWindowLimiter(limit int, window time.Duration) *SlidingWindowLimiter {
return &SlidingWindowLimiter{
windows: make(map[string]*slidingWindow),
limit: limit,
window: window,
}
}
// Allow implements RateLimiter.
func (l *SlidingWindowLimiter) Allow(key string) bool {
return l.AllowN(key, 1)
}
// AllowN implements RateLimiter.
func (l *SlidingWindowLimiter) AllowN(key string, n int) bool {
l.mu.Lock()
defer l.mu.Unlock()
now := time.Now()
w, ok := l.windows[key]
if !ok || now.Sub(w.windowStart) >= l.window {
// New window
l.windows[key] = &slidingWindow{
count: n,
windowStart: now,
}
return n <= l.limit
}
// Check if within limit
if w.count+n <= l.limit {
w.count += n
return true
}
return false
}
// Reset implements RateLimiter.
func (l *SlidingWindowLimiter) Reset(key string) {
l.mu.Lock()
defer l.mu.Unlock()
delete(l.windows, key)
}
// CostTrackingMiddleware tracks costs of AI requests.
type CostTrackingMiddleware struct {
costs map[string]float64 // model -> cost per 1K tokens
totalCost float64
costFunc func(req *MiddlewareRequest, resp *MiddlewareResponse) float64
onCostEvent func(model string, cost float64, totalCost float64)
mu sync.Mutex
}
// CostTrackingConfig configures cost tracking middleware.
type CostTrackingConfig struct {
Costs map[string]float64
CostFunc func(req *MiddlewareRequest, resp *MiddlewareResponse) float64
OnCostEvent func(model string, cost float64, totalCost float64)
}
// NewCostTrackingMiddleware creates a cost tracking middleware.
func NewCostTrackingMiddleware(config CostTrackingConfig) *CostTrackingMiddleware {
costs := config.Costs
if costs == nil {
costs = make(map[string]float64)
}
return &CostTrackingMiddleware{
costs: costs,
costFunc: config.CostFunc,
onCostEvent: config.OnCostEvent,
}
}
// SetModelCost sets the cost per 1K tokens for a model.
func (m *CostTrackingMiddleware) SetModelCost(model string, costPer1K float64) {
m.mu.Lock()
defer m.mu.Unlock()
m.costs[model] = costPer1K
}
// GetTotalCost returns the total accumulated cost.
func (m *CostTrackingMiddleware) GetTotalCost() float64 {
m.mu.Lock()
defer m.mu.Unlock()
return m.totalCost
}
// ResetCost resets the total cost to zero.
func (m *CostTrackingMiddleware) ResetCost() {
m.mu.Lock()
defer m.mu.Unlock()
m.totalCost = 0
}
// Name implements Middleware.
func (m *CostTrackingMiddleware) Name() string {
return "cost_tracking"
}
// ProcessRequest implements Middleware.
func (m *CostTrackingMiddleware) ProcessRequest(ctx context.Context, req *MiddlewareRequest, next MiddlewareHandler) (*MiddlewareResponse, error) {
resp, err := next(ctx, req)
if err != nil {
return resp, err
}
// Calculate cost
var cost float64
if m.costFunc != nil {
cost = m.costFunc(req, resp)
} else {
cost = m.defaultCostCalc(req, resp)
}
// Update total
m.mu.Lock()
m.totalCost += cost
totalCost := m.totalCost
m.mu.Unlock()
// Callback
if m.onCostEvent != nil {
m.onCostEvent(req.ChatRequest.Model, cost, totalCost)
}
// Add to metadata
if resp.Metadata == nil {
resp.Metadata = make(map[string]any)
}
resp.Metadata["cost"] = cost
resp.Metadata["total_cost"] = totalCost
return resp, nil
}
// defaultCostCalc calculates cost based on token usage.
func (m *CostTrackingMiddleware) defaultCostCalc(req *MiddlewareRequest, resp *MiddlewareResponse) float64 {
if resp.ChatResponse.Usage == nil {
return 0
}
m.mu.Lock()
costPer1K := m.costs[req.ChatRequest.Model]
m.mu.Unlock()
if costPer1K == 0 {
return 0
}
totalTokens := float64(resp.ChatResponse.Usage.TotalTokens)
return (totalTokens / 1000) * costPer1K
}
// RetryMiddleware implements retry logic with exponential backoff.
type RetryMiddleware struct {
maxRetries int
initialDelay time.Duration
maxDelay time.Duration
retryOn func(error) bool
logger logger.Logger
}
// MiddlewareRetryConfig configures retry middleware.
type MiddlewareRetryConfig struct {
MaxRetries int
InitialDelay time.Duration
MaxDelay time.Duration
RetryOn func(error) bool
Logger logger.Logger
}
// NewRetryMiddleware creates a retry middleware.
func NewRetryMiddleware(config MiddlewareRetryConfig) *RetryMiddleware {
if config.MaxRetries <= 0 {
config.MaxRetries = 3
}
if config.InitialDelay <= 0 {
config.InitialDelay = time.Second
}
if config.MaxDelay <= 0 {
config.MaxDelay = 30 * time.Second
}
if config.RetryOn == nil {
config.RetryOn = DefaultRetryCondition
}
return &RetryMiddleware{
maxRetries: config.MaxRetries,
initialDelay: config.InitialDelay,
maxDelay: config.MaxDelay,
retryOn: config.RetryOn,
logger: config.Logger,
}
}
// Name implements Middleware.
func (m *RetryMiddleware) Name() string {
return "retry"
}
// ProcessRequest implements Middleware.
func (m *RetryMiddleware) ProcessRequest(ctx context.Context, req *MiddlewareRequest, next MiddlewareHandler) (*MiddlewareResponse, error) {
var lastErr error
delay := m.initialDelay
for attempt := 0; attempt <= m.maxRetries; attempt++ {
if attempt > 0 {
// Wait before retry
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(delay):
}
// Exponential backoff
delay *= 2
if delay > m.maxDelay {
delay = m.maxDelay
}
if m.logger != nil {
m.logger.Debug("Retrying request",
logger.Int("attempt", attempt),
logger.Duration("delay", delay),
)
}
}
resp, err := next(ctx, req)
if err == nil {
if resp.Metadata == nil {
resp.Metadata = make(map[string]any)
}
resp.Metadata["retry_attempts"] = attempt
return resp, nil
}
lastErr = err
// Check if we should retry
if !m.retryOn(err) {
return nil, err
}
}
return nil, fmt.Errorf("max retries exceeded: %w", lastErr)
}
// DefaultRetryCondition returns true for retryable errors.
func DefaultRetryCondition(err error) bool {
// Retry on context deadline exceeded, temporary errors, etc.
if errors.Is(err, context.DeadlineExceeded) {
return true
}
// Add more conditions as needed
return false
}
// TimeoutMiddleware adds timeout to requests.
type TimeoutMiddleware struct {
timeout time.Duration
}
// NewTimeoutMiddleware creates a timeout middleware.
func NewTimeoutMiddleware(timeout time.Duration) *TimeoutMiddleware {
return &TimeoutMiddleware{timeout: timeout}
}
// Name implements Middleware.
func (m *TimeoutMiddleware) Name() string {
return "timeout"
}
// ProcessRequest implements Middleware.
func (m *TimeoutMiddleware) ProcessRequest(ctx context.Context, req *MiddlewareRequest, next MiddlewareHandler) (*MiddlewareResponse, error) {
ctx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel()
return next(ctx, req)
}
// InMemoryCache is a simple in-memory cache implementation.
type InMemoryCache struct {
data map[string]*cacheEntry
mu sync.RWMutex
}
type cacheEntry struct {
value []byte
expiresAt time.Time
}
// NewInMemoryCache creates a new in-memory cache.
func NewInMemoryCache() *InMemoryCache {
cache := &InMemoryCache{
data: make(map[string]*cacheEntry),
}
// Start cleanup goroutine
go cache.cleanup()
return cache
}
// Get implements Cache.
func (c *InMemoryCache) Get(ctx context.Context, key string) ([]byte, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
entry, ok := c.data[key]
if !ok {
return nil, false
}
if time.Now().After(entry.expiresAt) {
return nil, false
}
return entry.value, true
}
// Set implements Cache.
func (c *InMemoryCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
c.mu.Lock()
defer c.mu.Unlock()
c.data[key] = &cacheEntry{
value: value,
expiresAt: time.Now().Add(ttl),
}
return nil
}
// Delete implements Cache.
func (c *InMemoryCache) Delete(ctx context.Context, key string) error {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.data, key)
return nil
}
// cleanup periodically removes expired entries.
func (c *InMemoryCache) cleanup() {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for range ticker.C {
c.mu.Lock()
now := time.Now()
for key, entry := range c.data {
if now.After(entry.expiresAt) {
delete(c.data, key)
}
}
c.mu.Unlock()
}
}
// MetricsMiddleware collects metrics on requests.
type MetricsMiddleware struct {
metrics metrics.Metrics
prefix string
}
// NewMetricsMiddleware creates a metrics middleware.
func NewMetricsMiddleware(metrics metrics.Metrics, prefix string) *MetricsMiddleware {
if prefix == "" {
prefix = "forge.ai.sdk"
}
return &MetricsMiddleware{
metrics: metrics,
prefix: prefix,
}
}
// Name implements Middleware.
func (m *MetricsMiddleware) Name() string {
return "metrics"
}
// ProcessRequest implements Middleware.
func (m *MetricsMiddleware) ProcessRequest(ctx context.Context, req *MiddlewareRequest, next MiddlewareHandler) (*MiddlewareResponse, error) {
startTime := time.Now()
// Increment request counter
m.metrics.Counter(m.prefix+".requests",
metrics.WithLabel("provider", req.ChatRequest.Provider),
metrics.WithLabel("model", req.ChatRequest.Model),
).Inc()
resp, err := next(ctx, req)
duration := time.Since(startTime)
// Record duration
m.metrics.Histogram(m.prefix+".duration",
metrics.WithLabel("provider", req.ChatRequest.Provider),
metrics.WithLabel("model", req.ChatRequest.Model),
).Observe(duration.Seconds())
if err != nil {
// Increment error counter
m.metrics.Counter(m.prefix+".errors",
metrics.WithLabel("provider", req.ChatRequest.Provider),
metrics.WithLabel("model", req.ChatRequest.Model),
).Inc()
} else {
// Record token usage
if resp.ChatResponse.Usage != nil {
m.metrics.Histogram(m.prefix+".tokens",
metrics.WithLabel("provider", req.ChatRequest.Provider),
metrics.WithLabel("model", req.ChatRequest.Model),
).Observe(float64(resp.ChatResponse.Usage.TotalTokens))
}
// Record cache hits
if resp.Cached {
m.metrics.Counter(m.prefix+".cache_hits",
metrics.WithLabel("provider", req.ChatRequest.Provider),
metrics.WithLabel("model", req.ChatRequest.Model),
).Inc()
}
}
return resp, err
}