@@ -3,6 +3,7 @@ package main
33import (
44 "net/http"
55
6+ "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
67 "gorm.io/gorm"
78)
89
@@ -18,7 +19,7 @@ type ServerConfig struct {
1819 dbAutoMigrate bool
1920}
2021
21- func NewServer (serverConfig ServerConfig ) (* http.ServeMux , error ) {
22+ func NewServer (serverConfig ServerConfig ) (http.Handler , error ) {
2223
2324 if serverConfig .dbAutoMigrate {
2425 err := serverConfig .db .AutoMigrate (& his {}, & rec {})
@@ -41,47 +42,56 @@ func NewServer(serverConfig ServerConfig) (*http.ServeMux, error) {
4142 recController := recController {store : newGormRecStore (serverConfig .db )}
4243 currentController := currentController {store : newInMemoryCurrentStore ()}
4344
45+ // handleFunc is a replacement for mux.HandleFunc that adds route data to the metrics.
46+ handleFunc := func (mux * http.ServeMux , pattern string , handlerFunc func (http.ResponseWriter , * http.Request )) {
47+ handler := otelhttp .WithRouteTag (pattern , http .HandlerFunc (handlerFunc ))
48+ mux .Handle (pattern , handler )
49+ }
50+
4451 // All are deprecated. Instead use /api/*
45- server . HandleFunc ( "GET /auth/token" , authController .getAuthToken )
46- tokenAuth . HandleFunc ( "GET /his/{pointId}" , hisController .getHis ) // Deprecated
47- tokenAuth . HandleFunc ( "POST /his/{pointId}" , hisController .postHis ) // Deprecated
48- tokenAuth . HandleFunc ( "DELETE /his/{pointId}" , hisController .deleteHis ) // Deprecated
49- tokenAuth . HandleFunc ( "GET /recs" , recController .getRecs )
50- tokenAuth . HandleFunc ( "POST /recs" , recController .postRecs )
51- tokenAuth . HandleFunc ( "GET /recs/tag/siteMeter" , recController .getRecsByTag ) // Deprecated. Use /recs?tag=""
52- tokenAuth . HandleFunc ( "GET /recs/{id}" , recController .getRec )
53- tokenAuth . HandleFunc ( "PUT /recs/{id}" , recController .putRec )
54- tokenAuth . HandleFunc ( "DELETE /recs/{id}" , recController .deleteRec )
55- tokenAuth . HandleFunc ( "GET /recs/{pointId}/history" , hisController .getHis )
56- tokenAuth . HandleFunc ( "POST /recs/{pointId}/history" , hisController .postHis )
57- tokenAuth . HandleFunc ( "DELETE /recs/{pointId}/history" , hisController .deleteHis )
58- tokenAuth . HandleFunc ( "GET /recs/{pointId}/current" , currentController .getCurrent )
59- tokenAuth . HandleFunc ( "POST /recs/{pointId}/current" , currentController .postCurrent )
52+ handleFunc ( server , "GET /auth/token" , authController .getAuthToken )
53+ handleFunc ( tokenAuth , "GET /his/{pointId}" , hisController .getHis ) // Deprecated
54+ handleFunc ( tokenAuth , "POST /his/{pointId}" , hisController .postHis ) // Deprecated
55+ handleFunc ( tokenAuth , "DELETE /his/{pointId}" , hisController .deleteHis ) // Deprecated
56+ handleFunc ( tokenAuth , "GET /recs" , recController .getRecs )
57+ handleFunc ( tokenAuth , "POST /recs" , recController .postRecs )
58+ handleFunc ( tokenAuth , "GET /recs/tag/siteMeter" , recController .getRecsByTag ) // Deprecated. Use /recs?tag=""
59+ handleFunc ( tokenAuth , "GET /recs/{id}" , recController .getRec )
60+ handleFunc ( tokenAuth , "PUT /recs/{id}" , recController .putRec )
61+ handleFunc ( tokenAuth , "DELETE /recs/{id}" , recController .deleteRec )
62+ handleFunc ( tokenAuth , "GET /recs/{pointId}/history" , hisController .getHis )
63+ handleFunc ( tokenAuth , "POST /recs/{pointId}/history" , hisController .postHis )
64+ handleFunc ( tokenAuth , "DELETE /recs/{pointId}/history" , hisController .deleteHis )
65+ handleFunc ( tokenAuth , "GET /recs/{pointId}/current" , currentController .getCurrent )
66+ handleFunc ( tokenAuth , "POST /recs/{pointId}/current" , currentController .postCurrent )
6067 server .Handle ("/his/" , tokenAuthMiddleware (serverConfig .jwtSecret , tokenAuth ))
6168 server .Handle ("/recs" , tokenAuthMiddleware (serverConfig .jwtSecret , tokenAuth ))
6269 server .Handle ("/recs/" , tokenAuthMiddleware (serverConfig .jwtSecret , tokenAuth ))
6370
64- server . HandleFunc ( "GET /api/auth/token" , authController .getAuthToken )
65- tokenAuth . HandleFunc ( "GET /api/his/{pointId}" , hisController .getHis ) // Deprecated
66- tokenAuth . HandleFunc ( "POST /api/his/{pointId}" , hisController .postHis ) // Deprecated
67- tokenAuth . HandleFunc ( "DELETE /api/his/{pointId}" , hisController .deleteHis ) // Deprecated
68- tokenAuth . HandleFunc ( "GET /api/recs" , recController .getRecs )
69- tokenAuth . HandleFunc ( "POST /api/recs" , recController .postRecs )
70- tokenAuth . HandleFunc ( "GET /api/recs/tag/siteMeter" , recController .getRecsByTag ) // Deprecated. Use /recs?tag=""
71- tokenAuth . HandleFunc ( "GET /api/recs/{id}" , recController .getRec )
72- tokenAuth . HandleFunc ( "PUT /api/recs/{id}" , recController .putRec )
73- tokenAuth . HandleFunc ( "DELETE /api/recs/{id}" , recController .deleteRec )
74- tokenAuth . HandleFunc ( "GET /api/recs/{pointId}/history" , hisController .getHis )
75- tokenAuth . HandleFunc ( "POST /api/recs/{pointId}/history" , hisController .postHis )
76- tokenAuth . HandleFunc ( "DELETE /api/recs/{pointId}/history" , hisController .deleteHis )
77- tokenAuth . HandleFunc ( "GET /api/recs/{pointId}/current" , currentController .getCurrent )
78- tokenAuth . HandleFunc ( "POST /api/recs/{pointId}/current" , currentController .postCurrent )
71+ handleFunc ( server , "GET /api/auth/token" , authController .getAuthToken )
72+ handleFunc ( tokenAuth , "GET /api/his/{pointId}" , hisController .getHis ) // Deprecated
73+ handleFunc ( tokenAuth , "POST /api/his/{pointId}" , hisController .postHis ) // Deprecated
74+ handleFunc ( tokenAuth , "DELETE /api/his/{pointId}" , hisController .deleteHis ) // Deprecated
75+ handleFunc ( tokenAuth , "GET /api/recs" , recController .getRecs )
76+ handleFunc ( tokenAuth , "POST /api/recs" , recController .postRecs )
77+ handleFunc ( tokenAuth , "GET /api/recs/tag/siteMeter" , recController .getRecsByTag ) // Deprecated. Use /recs?tag=""
78+ handleFunc ( tokenAuth , "GET /api/recs/{id}" , recController .getRec )
79+ handleFunc ( tokenAuth , "PUT /api/recs/{id}" , recController .putRec )
80+ handleFunc ( tokenAuth , "DELETE /api/recs/{id}" , recController .deleteRec )
81+ handleFunc ( tokenAuth , "GET /api/recs/{pointId}/history" , hisController .getHis )
82+ handleFunc ( tokenAuth , "POST /api/recs/{pointId}/history" , hisController .postHis )
83+ handleFunc ( tokenAuth , "DELETE /api/recs/{pointId}/history" , hisController .deleteHis )
84+ handleFunc ( tokenAuth , "GET /api/recs/{pointId}/current" , currentController .getCurrent )
85+ handleFunc ( tokenAuth , "POST /api/recs/{pointId}/current" , currentController .postCurrent )
7986 server .Handle ("/api/his/" , tokenAuthMiddleware (serverConfig .jwtSecret , tokenAuth ))
8087 server .Handle ("/api/recs" , tokenAuthMiddleware (serverConfig .jwtSecret , tokenAuth ))
8188 server .Handle ("/api/recs/" , tokenAuthMiddleware (serverConfig .jwtSecret , tokenAuth ))
8289
8390 // Catch all others with public files. Not found fallback is app index for browser router.
8491 server .Handle ("/app/" , fileServerWithFallback (http .Dir ("./public" ), "./public/app/index.html" ))
8592
86- return server , nil
93+ // Observability
94+ observed := otelhttp .NewHandler (server , "/" )
95+
96+ return observed , nil
8797}
0 commit comments