File size: 8,087 Bytes
bf9e111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e6b65c
 
 
 
bf9e111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Package logging provides request logging functionality for the CLI Proxy API server.
// This file contains metrics integration for the FileStreamingLogWriter.
package logging

import (
	"os"
	"path/filepath"
	"sync/atomic"
	"time"

	"github.com/router-for-me/CLIProxyAPI/v6/internal/domain/ports"
)

// MetricsEnabledFileStreamingLogWriter wraps FileStreamingLogWriter with metrics collection
type MetricsEnabledFileStreamingLogWriter struct {
	*FileStreamingLogWriter
	metrics      ports.MetricsService
	dropCount    atomic.Uint64
	startTime    time.Time
	bytesWritten atomic.Int64
}

// NewMetricsEnabledFileStreamingLogWriter creates a new metrics-enabled streaming log writer
func NewMetricsEnabledFileStreamingLogWriter(
	base *FileStreamingLogWriter,
	metrics ports.MetricsService,
) *MetricsEnabledFileStreamingLogWriter {
	if metrics == nil {
		metrics = &ports.NoOpMetricsService{}
	}

	writer := &MetricsEnabledFileStreamingLogWriter{
		FileStreamingLogWriter: base,
		metrics:                metrics,
		startTime:              time.Now(),
	}

	// Report initial queue depth
	if base != nil && base.chunkChan != nil {
		metrics.RecordLogQueueDepth(len(base.chunkChan))
	}

	return writer
}

// WriteChunkAsync writes a response chunk asynchronously with metrics tracking
func (w *MetricsEnabledFileStreamingLogWriter) WriteChunkAsync(chunk []byte) {
	if w.FileStreamingLogWriter == nil {
		return
	}

	// Track queue depth before write
	if w.chunkChan != nil {
		w.metrics.RecordLogQueueDepth(len(w.chunkChan))
	}

	// Try to write to channel
	select {
	case w.chunkChan <- chunk:
		// Success - record bytes
		w.bytesWritten.Add(int64(len(chunk)))
	default:
		// Channel full - record drop
		w.dropCount.Add(1)
		w.metrics.RecordLogDropCount(1)
	}

	// Update queue depth after write
	if w.chunkChan != nil {
		w.metrics.RecordLogQueueDepth(len(w.chunkChan))
	}
}

// WriteStatus writes the response status with metrics tracking
func (w *MetricsEnabledFileStreamingLogWriter) WriteStatus(status int, headers map[string][]string) error {
	if w.FileStreamingLogWriter == nil {
		return nil
	}

	return w.FileStreamingLogWriter.WriteStatus(status, headers)
}

// WriteAPIRequest writes the API request with metrics tracking
func (w *MetricsEnabledFileStreamingLogWriter) WriteAPIRequest(apiRequest []byte) error {
	if w.FileStreamingLogWriter == nil {
		return nil
	}

	if len(apiRequest) > 0 {
		w.bytesWritten.Add(int64(len(apiRequest)))
	}

	return w.FileStreamingLogWriter.WriteAPIRequest(apiRequest)
}

// WriteAPIResponse writes the API response with metrics tracking
func (w *MetricsEnabledFileStreamingLogWriter) WriteAPIResponse(apiResponse []byte) error {
	if w.FileStreamingLogWriter == nil {
		return nil
	}

	if len(apiResponse) > 0 {
		w.bytesWritten.Add(int64(len(apiResponse)))
	}

	return w.FileStreamingLogWriter.WriteAPIResponse(apiResponse)
}

// Close finalizes the log and records final metrics
func (w *MetricsEnabledFileStreamingLogWriter) Close() error {
	if w.FileStreamingLogWriter == nil {
		return nil
	}

	// Record processing latency
	duration := time.Since(w.startTime)
	w.metrics.RecordLogProcessingLatency(duration)

	// Record total bytes written
	w.metrics.RecordLogWrite(w.bytesWritten.Load())

	// Record any drops
	drops := w.dropCount.Load()
	if drops > 0 {
		w.metrics.RecordLogDropCount(drops)
	}

	// Report final queue depth (should be 0 or close to it)
	if w.chunkChan != nil {
		w.metrics.RecordLogQueueDepth(len(w.chunkChan))
	}

	return w.FileStreamingLogWriter.Close()
}

// GetDropCount returns the number of dropped chunks
func (w *MetricsEnabledFileStreamingLogWriter) GetDropCount() uint64 {
	return w.dropCount.Load()
}

// GetBytesWritten returns the total bytes written
func (w *MetricsEnabledFileStreamingLogWriter) GetBytesWritten() int64 {
	return w.bytesWritten.Load()
}

// GetProcessingDuration returns the processing duration
func (w *MetricsEnabledFileStreamingLogWriter) GetProcessingDuration() time.Duration {
	return time.Since(w.startTime)
}

// FileRequestLoggerWithMetrics wraps FileRequestLogger with metrics support
type FileRequestLoggerWithMetrics struct {
	*FileRequestLogger
	metrics ports.MetricsService
}

// NewFileRequestLoggerWithMetrics creates a new FileRequestLogger with metrics support
func NewFileRequestLoggerWithMetrics(
	base *FileRequestLogger,
	metrics ports.MetricsService,
) *FileRequestLoggerWithMetrics {
	if metrics == nil {
		metrics = &ports.NoOpMetricsService{}
	}

	return &FileRequestLoggerWithMetrics{
		FileRequestLogger: base,
		metrics:           metrics,
	}
}

// LogStreamingRequest creates a metrics-enabled streaming log writer
func (l *FileRequestLoggerWithMetrics) LogStreamingRequest(
	url, method string,
	headers map[string][]string,
	body []byte,
	requestID string,
) (StreamingLogWriter, error) {
	if !l.enabled {
		return &NoOpStreamingLogWriter{}, nil
	}

	// Create the base writer using the parent implementation
	baseWriter, err := l.createBaseStreamingWriter(url, method, headers, body, requestID)
	if err != nil {
		l.metrics.RecordLogError(err)
		return nil, err
	}

	// Wrap with metrics
	return NewMetricsEnabledFileStreamingLogWriter(baseWriter, l.metrics), nil
}

// createBaseStreamingWriter creates the underlying FileStreamingLogWriter
func (l *FileRequestLoggerWithMetrics) createBaseStreamingWriter(
	url, method string,
	headers map[string][]string,
	body []byte,
	requestID string,
) (*FileStreamingLogWriter, error) {
	// This mirrors the logic in FileRequestLogger.LogStreamingRequest
	if err := l.ensureLogsDir(); err != nil {
		return nil, err
	}

	filename := l.generateFilename(url, requestID)
	filePath := filepath.Join(l.logsDir, filename)

	requestHeaders := make(map[string][]string, len(headers))
	for key, values := range headers {
		headerValues := make([]string, len(values))
		copy(headerValues, values)
		requestHeaders[key] = headerValues
	}

	requestBodyPath, errTemp := l.writeRequestBodyTempFile(body)
	if errTemp != nil {
		return nil, errTemp
	}

	responseBodyFile, errCreate := os.CreateTemp(l.logsDir, "response-body-*.tmp")
	if errCreate != nil {
		_ = os.Remove(requestBodyPath)
		return nil, errCreate
	}
	responseBodyPath := responseBodyFile.Name()

	writer := &FileStreamingLogWriter{
		logFilePath:      filePath,
		url:              url,
		method:           method,
		timestamp:        time.Now(),
		requestHeaders:   requestHeaders,
		requestBodyPath:  requestBodyPath,
		responseBodyPath: responseBodyPath,
		responseBodyFile: responseBodyFile,
		chunkChan:        make(chan []byte, 100),
		closeChan:        make(chan struct{}),
		errorChan:        make(chan error, 1),
	}

	go writer.asyncWriter()

	return writer, nil
}

// GetMetrics returns the current metrics
func (l *FileRequestLoggerWithMetrics) GetMetrics() *ports.LogMetrics {
	if l.metrics != nil {
		return l.metrics.GetMetrics()
	}
	return &ports.LogMetrics{}
}

// MetricsCollectorAdapter provides a callback-based interface for log writers
type MetricsCollectorAdapter struct {
	metrics ports.MetricsService
}

// NewMetricsCollectorAdapter creates a new adapter for the given metrics service
func NewMetricsCollectorAdapter(metrics ports.MetricsService) *MetricsCollectorAdapter {
	if metrics == nil {
		metrics = &ports.NoOpMetricsService{}
	}
	return &MetricsCollectorAdapter{metrics: metrics}
}

// OnQueueDepthChanged records queue depth changes
func (a *MetricsCollectorAdapter) OnQueueDepthChanged(depth int) {
	a.metrics.RecordLogQueueDepth(depth)
}

// OnProcessingCompleted records processing completion
func (a *MetricsCollectorAdapter) OnProcessingCompleted(duration time.Duration, bytesProcessed int64, err error) {
	a.metrics.RecordLogProcessingLatency(duration)
	a.metrics.RecordLogWrite(bytesProcessed)
	if err != nil {
		a.metrics.RecordLogError(err)
	}
}

// OnItemsDropped records dropped items
func (a *MetricsCollectorAdapter) OnItemsDropped(count uint64) {
	a.metrics.RecordLogDropCount(count)
}

// Ensure MetricsCollectorAdapter implements the interface
var _ ports.MetricsCollector = (*MetricsCollectorAdapter)(nil)