Skip to content

Commit 0be0cb4

Browse files
committed
refactor: replace manual REST endpoint instrumentation with go-gin-prometheus
Replace manual timing instrumentation in individual REST endpoints with go-gin-prometheus middleware for automatic metrics coverage across all HTTP endpoints. Changes: - Add go-gin-prometheus dependency to provide standard HTTP metrics - Integrate Prometheus middleware in router for automatic instrumentation - Remove manual timing code from features, KVM displays, and power endpoints - Remove unused kvmAPIRequestSeconds metric and RecordAPIRequest function - Clean up unused time and devices package imports Benefits: - Automatic metrics for ALL REST endpoints (not just 3) - Standard HTTP metrics (duration, status codes, request/response sizes) - Less repetitive code and improved maintainability - Keeps all custom KVM connection and data flow metrics Addresses reviewer feedback in PR #761
1 parent dff5365 commit 0be0cb4

7 files changed

Lines changed: 8 additions & 38 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ require (
5959
github.com/ryanuber/go-glob v1.0.0 // indirect
6060
github.com/woodsbury/decimal128 v1.4.0 // indirect
6161
github.com/zalando/go-keyring v0.2.6 // indirect
62+
github.com/zsais/go-gin-prometheus v1.0.2 // indirect
6263
go.yaml.in/yaml/v2 v2.4.2 // indirect
6364
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
6465
golang.org/x/oauth2 v0.30.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ github.com/woodsbury/decimal128 v1.4.0 h1:xJATj7lLu4f2oObouMt2tgGiElE5gO6mSWUjQs
269269
github.com/woodsbury/decimal128 v1.4.0/go.mod h1:BP46FUrVjVhdTbKT+XuQh2xfQaGki9LMIRJSFuh6THU=
270270
github.com/zalando/go-keyring v0.2.6 h1:r7Yc3+H+Ux0+M72zacZoItR3UDxeWfKTcabvkI8ua9s=
271271
github.com/zalando/go-keyring v0.2.6/go.mod h1:2TCrxYrbUNYfNS/Kgy/LSrkSQzZ5UPVH85RwfczwvcI=
272+
github.com/zsais/go-gin-prometheus v1.0.2 h1:3asLqrFltMdItpgr/OS4hYc8pLq3HzMa5T1gYuXBIZ0=
273+
github.com/zsais/go-gin-prometheus v1.0.2/go.mod h1:iKBYSOHzvGfe2FyGSOC8JSwUA0MITdnYzI6v+aAbw1Q=
272274
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
273275
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
274276
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0 h1:F7Jx+6hwnZ41NSFTO5q4LYDtJRXBf2PD0rNBkeB/lus=

internal/controller/httpapi/router.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/gin-gonic/gin"
88
"github.com/prometheus/client_golang/prometheus/promhttp"
9+
ginprometheus "github.com/zsais/go-gin-prometheus"
910

1011
"github.com/device-management-toolkit/console/config"
1112
v1 "github.com/device-management-toolkit/console/internal/controller/httpapi/v1"
@@ -21,6 +22,10 @@ func NewRouter(handler *gin.Engine, l logger.Interface, t usecase.Usecases, cfg
2122
handler.Use(gin.Logger())
2223
handler.Use(gin.Recovery())
2324

25+
// Add Prometheus middleware for automatic HTTP metrics
26+
p := ginprometheus.NewPrometheus("gin")
27+
p.Use(handler)
28+
2429
// Initialize Fuego adapter
2530
fuegoAdapter := openapi.NewFuegoAdapter(t, l)
2631
fuegoAdapter.RegisterRoutes()

internal/controller/httpapi/v1/features.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package v1
22

33
import (
44
"net/http"
5-
"time"
65

76
"github.com/gin-gonic/gin"
87

98
dto "github.com/device-management-toolkit/console/internal/entity/dto/v1"
10-
"github.com/device-management-toolkit/console/internal/usecase/devices"
119
)
1210

1311
func (r *deviceManagementRoutes) getVersion(c *gin.Context) {
@@ -25,12 +23,6 @@ func (r *deviceManagementRoutes) getVersion(c *gin.Context) {
2523
}
2624

2725
func (r *deviceManagementRoutes) getFeatures(c *gin.Context) {
28-
start := time.Now()
29-
30-
defer func() {
31-
devices.RecordAPIRequest(time.Since(start), "amt_features")
32-
}()
33-
3426
guid := c.Param("guid")
3527

3628
features, _, err := r.d.GetFeatures(c.Request.Context(), guid)

internal/controller/httpapi/v1/kvm.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@ package v1
22

33
import (
44
"net/http"
5-
"time"
65

76
"github.com/gin-gonic/gin"
87

98
"github.com/device-management-toolkit/console/internal/entity/dto/v1"
10-
"github.com/device-management-toolkit/console/internal/usecase/devices"
119
)
1210

1311
// getKVMDisplays returns current IPS_ScreenSettingData for the device
1412
func (r *deviceManagementRoutes) getKVMDisplays(c *gin.Context) {
15-
start := time.Now()
16-
17-
defer func() {
18-
devices.RecordAPIRequest(time.Since(start), "kvm_displays")
19-
}()
20-
2113
guid := c.Param("guid")
2214

2315
settings, err := r.d.GetKVMScreenSettings(c.Request.Context(), guid)

internal/controller/httpapi/v1/power.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@ package v1
22

33
import (
44
"net/http"
5-
"time"
65

76
"github.com/gin-gonic/gin"
87

98
"github.com/device-management-toolkit/console/internal/entity/dto/v1"
10-
"github.com/device-management-toolkit/console/internal/usecase/devices"
119
)
1210

1311
func (r *deviceManagementRoutes) getPowerState(c *gin.Context) {
14-
start := time.Now()
15-
16-
defer func() {
17-
devices.RecordAPIRequest(time.Since(start), "power_state")
18-
}()
19-
2012
guid := c.Param("guid")
2113

2214
state, err := r.d.GetPowerState(c.Request.Context(), guid)

internal/usecase/devices/metrics.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,6 @@ var (
138138
},
139139
[]string{"mode"},
140140
)
141-
142-
kvmAPIRequestSeconds = promauto.NewHistogramVec(
143-
prometheus.HistogramOpts{
144-
Name: "kvm_api_request_seconds",
145-
Help: "Time for API requests during KVM connection setup (KVM_TIMING)",
146-
Buckets: []float64{0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2},
147-
},
148-
[]string{"endpoint"},
149-
)
150141
)
151142

152143
// RecordWebsocketUpgrade records the WebSocket upgrade duration metric.
@@ -164,11 +155,6 @@ func RecordConsentCodeWait(duration time.Duration, mode string) {
164155
kvmConsentCodeWaitSeconds.WithLabelValues(mode).Observe(duration.Seconds())
165156
}
166157

167-
// RecordAPIRequest records the API request duration metric.
168-
func RecordAPIRequest(duration time.Duration, endpoint string) {
169-
kvmAPIRequestSeconds.WithLabelValues(endpoint).Observe(duration.Seconds())
170-
}
171-
172158
// RecordDeviceLookup records the device lookup duration metric.
173159
func RecordDeviceLookup(duration time.Duration) {
174160
kvmDeviceLookupSeconds.Observe(duration.Seconds())

0 commit comments

Comments
 (0)