Skip to content

Commit 0fb8a76

Browse files
committed
fix: metrics cleanup
1 parent 2dcc71b commit 0fb8a76

File tree

3 files changed

+101
-71
lines changed

3 files changed

+101
-71
lines changed

common/metric/metric.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ type MetricService interface {
1717
}
1818

1919
const (
20-
MetricProxyRequest = "proxy_request"
21-
MetricMirrorRequest = "mirror_request"
22-
MetricPanicRecovered = "panic_recovered"
23-
MetricProxyActiveConnections = "proxy_active_connections"
24-
MetricMirrorActiveConnections = "mirror_active_connections"
20+
// Core proxy/mirror operation metrics
21+
MetricProxy = "proxy" // Base metric for proxy operations
22+
MetricMirror = "mirror" // Base metric for mirror operations
23+
24+
// Connection tracking metrics
25+
MetricProxyConnections = "proxy_connections" // For active proxy connections
26+
MetricMirrorConnections = "mirror_connections" // For active mirror connections
27+
28+
// System metrics
29+
MetricPanics = "panics" // For system panic tracking
2530
)

controllers/proxy_controller.go

+84-65
Original file line numberDiff line numberDiff line change
@@ -190,28 +190,87 @@ func (_this *proxyController) processRequest(
190190
func (_this *proxyController) sendRequest(reqCtx requestContext) {
191191
req := reqCtx.request
192192
reqType := reqCtx.reqType
193+
startTime := time.Now()
193194

194195
// Set metric name based on request type
195-
metricName := metric.MetricProxyRequest
196+
metricName := metric.MetricProxy
196197
connsCounter := _this.proxyActiveConns
197-
198198
if reqType == mirrorRequest {
199-
metricName = metric.MetricMirrorRequest
199+
metricName = metric.MetricMirror
200200
connsCounter = _this.mirrorActiveConns
201201
}
202202

203+
// Track connections
203204
atomic.AddInt64(connsCounter, 1)
204205
_this.recordActiveConnections(reqType)
205-
206-
// Make the request
207-
resp, err := _this.client.Do(req)
208-
209-
// Ensure we always decrement the counter when the function exits
210206
defer func() {
211207
atomic.AddInt64(connsCounter, -1)
212208
_this.recordActiveConnections(reqType)
213209
}()
214210

211+
// Always record metrics and log response
212+
var resp *http.Response
213+
var err error
214+
var respBody []byte
215+
defer func() {
216+
statusCode := http.StatusBadGateway // Default error status
217+
statusClass := "5xx"
218+
latency := time.Since(startTime)
219+
220+
if err == nil {
221+
statusCode = resp.StatusCode
222+
statusClass = fmt.Sprintf("%dxx", resp.StatusCode/100)
223+
}
224+
225+
// Record all metrics
226+
_ = _this.metrics.RecordRequest(
227+
_this.ctx,
228+
metricName,
229+
req.Method,
230+
req.URL.Path,
231+
attribute.String("method", req.Method),
232+
)
233+
_ = _this.metrics.RecordRequest(
234+
_this.ctx,
235+
metricName,
236+
req.Method,
237+
req.URL.Path,
238+
attribute.String("status_class", statusClass),
239+
attribute.Int("status_code", statusCode),
240+
attribute.String("method", req.Method),
241+
)
242+
_ = _this.metrics.RecordDuration(
243+
_this.ctx,
244+
metricName,
245+
latency,
246+
attribute.String("method", req.Method),
247+
attribute.String("path", req.URL.Path),
248+
attribute.Int("status_code", statusCode),
249+
)
250+
251+
// Log response or error
252+
if err != nil {
253+
_this.logger.Errorw(fmt.Sprintf("%s error", reqType),
254+
"error", err,
255+
"method", req.Method,
256+
"url", req.URL.String(),
257+
"headers", req.Header,
258+
"trace_id", reqCtx.traceID,
259+
"latency", latency,
260+
)
261+
} else {
262+
_this.logger.Debugw(fmt.Sprintf("%s response", reqType),
263+
"method", req.Method,
264+
"url", req.URL.String(),
265+
"status", statusCode,
266+
"content_length", resp.ContentLength,
267+
"headers", resp.Header,
268+
"trace_id", reqCtx.traceID,
269+
"latency", latency,
270+
)
271+
}
272+
}()
273+
215274
// Log outbound request
216275
_this.logger.Debugw(fmt.Sprintf("%s request", reqType),
217276
"method", req.Method,
@@ -220,83 +279,43 @@ func (_this *proxyController) sendRequest(reqCtx requestContext) {
220279
"trace_id", reqCtx.traceID,
221280
)
222281

282+
// Make the request
283+
resp, err = _this.client.Do(req)
223284
if err != nil {
224-
_this.logger.Errorw(fmt.Sprintf("%s error", reqType),
225-
"error", err,
226-
"method", req.Method,
227-
"url", req.URL.String(),
228-
"headers", req.Header,
229-
"trace_id", reqCtx.traceID,
230-
"latency", time.Since(reqCtx.startTime),
231-
)
232285
if reqType == proxyRequest {
233286
reqCtx.ginContext.JSON(http.StatusBadGateway, gin.H{"error": "proxy error"})
234287
}
235288
return
236289
}
237-
// Ignore error since we are closing the body anyway
238-
defer func() { _ = resp.Body.Close() }()
290+
defer resp.Body.Close()
239291

240-
// Log response
241-
_this.logger.Debugw(fmt.Sprintf("%s response", reqType),
242-
"method", req.Method,
243-
"url", req.URL.String(),
244-
"status", resp.StatusCode,
245-
"content length", resp.ContentLength,
246-
"headers", resp.Header,
247-
"trace_id", reqCtx.traceID,
248-
"latency", time.Since(reqCtx.startTime),
249-
)
250-
251-
// Record status metrics with the appropriate prefix
252-
statusClass := fmt.Sprintf("%dxx", resp.StatusCode/100)
253-
_ = _this.metrics.RecordRequest(
254-
_this.ctx,
255-
fmt.Sprintf("%s_status", metricName),
256-
statusClass,
257-
req.URL.Path,
258-
attribute.String("status_class", statusClass),
259-
attribute.Int("status_code", resp.StatusCode),
260-
attribute.String("method", req.Method),
261-
)
292+
// For mirror requests, we're done here
293+
if reqType == mirrorRequest {
294+
return
295+
}
262296

263-
respBody, err := io.ReadAll(resp.Body)
297+
respBody, err = io.ReadAll(resp.Body)
264298
if err != nil {
265-
_this.logger.Errorw(fmt.Sprintf("failed to read %s response", reqType),
266-
"error", err,
267-
"trace_id", reqCtx.traceID,
268-
)
269-
if reqType == proxyRequest {
270-
reqCtx.ginContext.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read response"})
271-
}
299+
reqCtx.ginContext.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read response"})
272300
return
273301
}
274302

275-
if reqType == proxyRequest {
276-
for k, vv := range resp.Header {
277-
for _, v := range vv {
278-
reqCtx.ginContext.Header(k, v)
279-
}
303+
for k, vv := range resp.Header {
304+
for _, v := range vv {
305+
reqCtx.ginContext.Header(k, v)
280306
}
281-
reqCtx.ginContext.Header("X-Proxied-By", config.ServiceName)
282-
reqCtx.ginContext.Header("X-Trace-ID", reqCtx.traceID)
283-
reqCtx.ginContext.Data(resp.StatusCode, resp.Header.Get("Content-Type"), respBody)
284307
}
285-
286-
// Record duration with the same metric name prefix
287-
_ = _this.metrics.RecordDuration(_this.ctx, metricName, time.Since(reqCtx.startTime),
288-
attribute.String("method", req.Method),
289-
attribute.String("path", req.URL.Path),
290-
attribute.Int("status_code", resp.StatusCode),
291-
)
308+
reqCtx.ginContext.Header("X-Proxied-By", config.ServiceName)
309+
reqCtx.ginContext.Header("X-Trace-ID", reqCtx.traceID)
310+
reqCtx.ginContext.Data(resp.StatusCode, resp.Header.Get("Content-Type"), respBody)
292311
}
293312

294313
func (_this *proxyController) recordActiveConnections(reqType requestType) {
295-
metricName := metric.MetricProxyActiveConnections
314+
metricName := metric.MetricProxyConnections
296315
connsCounter := _this.proxyActiveConns
297316

298317
if reqType == mirrorRequest {
299-
metricName = metric.MetricMirrorActiveConnections
318+
metricName = metric.MetricMirrorConnections
300319
connsCounter = _this.mirrorActiveConns
301320
}
302321

server/server.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,13 @@ func (_this serverImpl) panicHandler() gin.HandlerFunc {
193193
attribute.String("error", fmt.Sprintf("%v", err)),
194194
}
195195

196-
if recordErr := _this.metricService.RecordRequest(_this.ctx, metric.MetricPanicRecovered, c.Request.Method, c.Request.URL.Path, attrs...); recordErr != nil {
196+
if recordErr := _this.metricService.RecordRequest(
197+
_this.ctx,
198+
metric.MetricPanics,
199+
c.Request.Method,
200+
c.Request.URL.Path,
201+
attrs...,
202+
); recordErr != nil {
197203
_this.logger.Errorw("failed to record panic metric", "error", recordErr)
198204
}
199205

0 commit comments

Comments
 (0)