Skip to content

Commit 307d362

Browse files
authored
fix(server): capture metrics for custom handlers (#111)
1 parent 3ea04a7 commit 307d362

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

server/api/api.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66

77
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
8+
"github.com/newrelic/go-agent/v3/newrelic"
89
"github.com/odpf/stencil/server/domain"
910
stencilv1beta1 "github.com/odpf/stencil/server/odpf/stencil/v1beta1"
1011
"google.golang.org/grpc/health/grpc_health_v1"
@@ -31,13 +32,13 @@ func NewAPI(namespace domain.NamespaceService, schema domain.SchemaService, sear
3132
}
3233

3334
// RegisterSchemaHandlers registers HTTP handlers for schema download
34-
func (a *API) RegisterSchemaHandlers(mux *runtime.ServeMux) {
35+
func (a *API) RegisterSchemaHandlers(mux *runtime.ServeMux, app *newrelic.Application) {
3536
mux.HandlePath("GET", "/ping", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
3637
fmt.Fprint(w, "pong")
3738
})
38-
mux.HandlePath("GET", "/v1beta1/namespaces/{namespace}/schemas/{name}/versions/{version}", handleSchemaResponse(mux, a.HTTPGetSchema))
39-
mux.HandlePath("GET", "/v1beta1/namespaces/{namespace}/schemas/{name}", handleSchemaResponse(mux, a.HTTPLatestSchema))
40-
mux.HandlePath("POST", "/v1beta1/namespaces/{namespace}/schemas/{name}", wrapHandler(mux, a.HTTPUpload))
39+
mux.HandlePath(wrapHandler(app, "GET", "/v1beta1/namespaces/{namespace}/schemas/{name}/versions/{version}", handleSchemaResponse(mux, a.HTTPGetSchema)))
40+
mux.HandlePath(wrapHandler(app, "GET", "/v1beta1/namespaces/{namespace}/schemas/{name}", handleSchemaResponse(mux, a.HTTPLatestSchema)))
41+
mux.HandlePath(wrapHandler(app, "POST", "/v1beta1/namespaces/{namespace}/schemas/{name}", wrapErrHandler(mux, a.HTTPUpload)))
4142
}
4243

4344
func handleSchemaResponse(mux *runtime.ServeMux, getSchemaFn getSchemaData) runtime.HandlerFunc {
@@ -58,7 +59,7 @@ func handleSchemaResponse(mux *runtime.ServeMux, getSchemaFn getSchemaData) runt
5859
}
5960
}
6061

61-
func wrapHandler(mux *runtime.ServeMux, handler errHandleFunc) runtime.HandlerFunc {
62+
func wrapErrHandler(mux *runtime.ServeMux, handler errHandleFunc) runtime.HandlerFunc {
6263
return func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
6364
err := handler(w, r, pathParams)
6465
if err != nil {
@@ -68,3 +69,17 @@ func wrapHandler(mux *runtime.ServeMux, handler errHandleFunc) runtime.HandlerFu
6869
}
6970
}
7071
}
72+
73+
func wrapHandler(app *newrelic.Application, method, pattern string, handler runtime.HandlerFunc) (string, string, runtime.HandlerFunc) {
74+
if app == nil {
75+
return method, pattern, handler
76+
}
77+
return method, pattern, func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
78+
txn := app.StartTransaction(method + " " + pattern)
79+
defer txn.End()
80+
w = txn.SetWebResponse(w)
81+
txn.SetWebRequestHTTP(r)
82+
r = newrelic.RequestWithTransactionContext(r, txn)
83+
handler(w, r, pathParams)
84+
}
85+
}

server/api/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ func setup() (*mocks.NamespaceService, *mocks.SchemaService, *mocks.SearchServic
1212
searchService := &mocks.SearchService{}
1313
mux := runtime.NewServeMux()
1414
v1beta1 := api.NewAPI(nsService, schemaService, searchService)
15-
v1beta1.RegisterSchemaHandlers(mux)
15+
v1beta1.RegisterSchemaHandlers(mux, nil)
1616
return nsService, schemaService, searchService, mux, v1beta1
1717
}

server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func Start(cfg config.Config) {
8080
if err != nil {
8181
log.Fatalln("Failed to dial server:", err)
8282
}
83-
api.RegisterSchemaHandlers(mux)
83+
api.RegisterSchemaHandlers(mux, nr)
8484

8585
if err = stencilv1beta1.RegisterStencilServiceHandler(ctx, mux, conn); err != nil {
8686
log.Fatalln("Failed to register stencil service handler:", err)

0 commit comments

Comments
 (0)