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

Add artifacts related metrics

parent 32b3fa9f
......@@ -9,12 +9,37 @@ import (
"os/exec"
"syscall"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/upload"
"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 {
TempPath string
metadataFile string
......@@ -73,6 +98,11 @@ func (a *artifactsUploadProcessor) Cleanup() {
func UploadArtifacts(myAPI *api.API, h http.Handler) http.Handler {
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 == "" {
helper.Fail500(w, r, fmt.Errorf("UploadArtifacts: TempPath is empty"))
return
......
......@@ -9,12 +9,33 @@ package sendfile
import (
"log"
"net/http"
"regexp"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
)
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 {
rw http.ResponseWriter
status int
......@@ -22,6 +43,11 @@ type sendFileResponseWriter struct {
req *http.Request
}
func init() {
prometheus.MustRegister(sendFileRequests)
prometheus.MustRegister(sendFileBytes)
}
func SendFile(h http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
s := &sendFileResponseWriter{
......@@ -84,9 +110,26 @@ func sendFileFromDisk(w http.ResponseWriter, r *http.Request, file string) {
}
defer content.Close()
countSendFileMetrics(fi.Size(), r)
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() {
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