Commit 8e4cf676 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add artifacts related metrics

parent 32b3fa9f
...@@ -9,12 +9,37 @@ import ( ...@@ -9,12 +9,37 @@ import (
"os/exec" "os/exec"
"syscall" "syscall"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api" "gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper" "gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/upload" "gitlab.com/gitlab-org/gitlab-workhorse/internal/upload"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/zipartifacts" "gitlab.com/gitlab-org/gitlab-workhorse/internal/zipartifacts"
) )
var (
artifactsUploadRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_artifacts_upload_requests",
Help: "How many artifacts upload requests have been processed by gitlab-workhorse.",
},
nil,
)
artifactsUploadBytes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_artifacts_upload_bytes",
Help: "How many artifacts upload bytes have been send by gitlab-workhorse.",
},
nil,
)
)
func init() {
prometheus.MustRegister(artifactsUploadRequests)
prometheus.MustRegister(artifactsUploadBytes)
}
type artifactsUploadProcessor struct { type artifactsUploadProcessor struct {
TempPath string TempPath string
metadataFile string metadataFile string
...@@ -73,6 +98,11 @@ func (a *artifactsUploadProcessor) Cleanup() { ...@@ -73,6 +98,11 @@ func (a *artifactsUploadProcessor) Cleanup() {
func UploadArtifacts(myAPI *api.API, h http.Handler) http.Handler { func UploadArtifacts(myAPI *api.API, h http.Handler) http.Handler {
return myAPI.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) { return myAPI.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) {
artifactsUploadRequests.WithLabelValues().Inc()
defer func() {
artifactsUploadBytes.WithLabelValues().Add(float64(r.ContentLength))
}()
if a.TempPath == "" { if a.TempPath == "" {
helper.Fail500(w, r, fmt.Errorf("UploadArtifacts: TempPath is empty")) helper.Fail500(w, r, fmt.Errorf("UploadArtifacts: TempPath is empty"))
return return
......
...@@ -9,12 +9,33 @@ package sendfile ...@@ -9,12 +9,33 @@ package sendfile
import ( import (
"log" "log"
"net/http" "net/http"
"regexp"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper" "gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
) )
const sendFileResponseHeader = "X-Sendfile" const sendFileResponseHeader = "X-Sendfile"
var (
sendFileRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_sendfile_requests",
Help: "How many X-Sendfile requests have been processed by gitlab-workhorse, partitioned by sendfile type.",
},
[]string{"type"},
)
sendFileBytes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_sendfile_bytes",
Help: "How many X-Sendfile bytes have been send by gitlab-workhorse, partitioned by sendfile type.",
},
[]string{"type"},
)
)
type sendFileResponseWriter struct { type sendFileResponseWriter struct {
rw http.ResponseWriter rw http.ResponseWriter
status int status int
...@@ -22,6 +43,11 @@ type sendFileResponseWriter struct { ...@@ -22,6 +43,11 @@ type sendFileResponseWriter struct {
req *http.Request req *http.Request
} }
func init() {
prometheus.MustRegister(sendFileRequests)
prometheus.MustRegister(sendFileBytes)
}
func SendFile(h http.Handler) http.Handler { func SendFile(h http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
s := &sendFileResponseWriter{ s := &sendFileResponseWriter{
...@@ -84,9 +110,26 @@ func sendFileFromDisk(w http.ResponseWriter, r *http.Request, file string) { ...@@ -84,9 +110,26 @@ func sendFileFromDisk(w http.ResponseWriter, r *http.Request, file string) {
} }
defer content.Close() defer content.Close()
countSendFileMetrics(fi.Size(), r)
http.ServeContent(w, r, "", fi.ModTime(), content) http.ServeContent(w, r, "", fi.ModTime(), content)
} }
func countSendFileMetrics(size int64, r *http.Request) {
var requestType string
switch {
case regexp.MustCompile("builds/[0-9]+/artifacts").MatchString(r.RequestURI):
requestType = "artifacts"
default:
requestType = "other"
}
sendFileRequests.WithLabelValues(requestType).Inc()
defer func() {
sendFileBytes.WithLabelValues(requestType).Add(float64(size))
}()
}
func (s *sendFileResponseWriter) Flush() { func (s *sendFileResponseWriter) Flush() {
s.WriteHeader(http.StatusOK) s.WriteHeader(http.StatusOK)
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment