-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed error by interface error supported
- Loading branch information
Showing
3 changed files
with
72 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package middlewares | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
metricsprometheus "github.com/kubitre/go_api_infra/metrics_prometheus" | ||
) | ||
|
||
func AddGoldenMetrics(recorder metricsprometheus.MetricRecorder, method string, handler http.HandlerFunc) http.Handler { | ||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
wi := &responseWriterInterceptor{ | ||
statusCode: http.StatusOK, | ||
ResponseWriter: writer, | ||
} | ||
|
||
timeStart := time.Now() | ||
defer func() { | ||
recorder.ObserveHTTPRequestDuration(request.Context(), metricsprometheus.HTTPReqProperties{ | ||
Method: method, | ||
Code: strconv.Itoa(wi.statusCode), | ||
}, time.Since(timeStart)) | ||
|
||
recorder.ObserveHTTPResponseSize(request.Context(), metricsprometheus.HTTPReqProperties{ | ||
Method: method, | ||
Code: strconv.Itoa(wi.statusCode), | ||
}, wi.sizeResponse) | ||
}() | ||
|
||
handler.ServeHTTP(wi, request) | ||
}) | ||
} | ||
|
||
type responseWriterInterceptor struct { | ||
http.ResponseWriter | ||
statusCode int | ||
sizeResponse int64 | ||
} | ||
|
||
func (w *responseWriterInterceptor) WriteHeader(statusCode int) { | ||
w.statusCode = statusCode | ||
w.ResponseWriter.WriteHeader(statusCode) | ||
} | ||
|
||
func (w *responseWriterInterceptor) Write(bts []byte) (int, error) { | ||
size, err := w.ResponseWriter.Write(bts) | ||
w.sizeResponse = int64(size) | ||
return size, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters