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
4344func 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+ }
0 commit comments