Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus Metrics: Duration for the watermarking/queueing #121

Merged
merged 3 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ services:
- env_files/exam_marker.env
networks:
- backend-internal
- backend
depends_on:
- db
- storage
Expand Down
1 change: 1 addition & 0 deletions exam_marker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ COPY server/graph ./server/graph
COPY server/lti_utils ./server/lti_utils

COPY exam_marker/exam_marker.go ./exam_marker/exam_marker.go
COPY exam_marker/prometheus ./exam_marker/prometheus
COPY utils ./utils
RUN go build -a -o exam_marker.runnable ./exam_marker/exam_marker.go

Expand Down
21 changes: 19 additions & 2 deletions exam_marker/exam_marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/exam_marker/prometheus"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/utils"
"github.com/adjust/rmq/v3"
render "github.com/brunsgaard/go-pdfium-render"
Expand All @@ -24,6 +26,7 @@ import (
"github.com/minio/minio-go/v7"
pdfcpu_api "github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
Expand All @@ -43,15 +46,13 @@ var (

type RMQConsumer struct {
name string
count int
before time.Time
MinIOClient *minio.Client
}

func NewRMQConsumer(minioClient *minio.Client, tag int) *RMQConsumer {
return &RMQConsumer{
name: fmt.Sprintf("consumer%d", tag),
count: 0,
before: time.Now(),
MinIOClient: minioClient,
}
Expand All @@ -67,6 +68,10 @@ func (consumer *RMQConsumer) Consume(delivery rmq.Delivery) {
log.Printf("%s working on task %q", consumer.name, task.ExamUUID)
executeMarkerTask(consumer.MinIOClient, task)

taskDuration := time.Since(task.SubmitTime)
prometheus.WatermarkingTimeHistogram.Observe(float64(taskDuration.Seconds()))
log.Printf("%s took %v to work on task %q", consumer.name, taskDuration.Seconds(), task.ExamUUID)

if err := delivery.Ack(); err != nil {
log.Println(err)
}
Expand Down Expand Up @@ -259,6 +264,18 @@ func main() {
}
}

// Expose the registered metrics via HTTP.
go func() {
port := "8081"
http.Handle("/metrics", promhttp.Handler())
fmt.Print(
"==========================================\n",
"Started the exam_marker metrics listening on Port "+port+"\n",
"==========================================\n",
)
log.Fatal(http.ListenAndServe(":"+port, nil))
}()

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT)
defer signal.Stop(signals)
Expand Down
14 changes: 14 additions & 0 deletions exam_marker/prometheus/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package prometheus

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
// WatermarkingTimeHistogram is a prometheus metric for the total amount of exams in the database
WatermarkingTimeHistogram = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "altklausur_ausleihe_watermarking_histogram",
Help: "histogram for the time it takes an exam to get marked",
})
)
10 changes: 9 additions & 1 deletion frontend/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ upstream backend-upstream {
server backend:8081;
}

upstream exam_marker-upstream {
server exam_marker:8081;
}

server {
listen 80;
listen [::]:80;
Expand Down Expand Up @@ -35,9 +39,13 @@ server {
proxy_pass http://backend-upstream/adminlogin;
}

location /metrics {
location /backend/metrics {
proxy_pass http://backend-upstream/metrics;
}

location /exam_marker/metrics {
proxy_pass http://exam_marker-upstream/metrics;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
Expand Down
1 change: 1 addition & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN go mod download

COPY server/graph ./server/graph
COPY server/lti_utils ./server/lti_utils
COPY server/prometheus ./server/prometheus
COPY server/server.go server/dummylogin.html ./server/
COPY utils ./utils

Expand Down
6 changes: 4 additions & 2 deletions server/graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/graph/generated"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/graph/model"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/lti_utils"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/prometheus"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/utils"
"github.com/dustin/go-humanize"
"github.com/gabriel-vasile/mimetype"
Expand Down Expand Up @@ -139,7 +140,7 @@ func (r *mutationResolver) CreateExam(ctx context.Context, input model.NewExam)
}

// update the TotalExams metric
utils.UpdateTotalExamsMetric(r.DB)
prometheus.UpdateTotalExamsMetric(r.DB)

return &exam, nil
}
Expand Down Expand Up @@ -193,6 +194,7 @@ func (r *mutationResolver) RequestMarkedExam(ctx context.Context, stringUUID str
UserID: userInfos.ID,
TextLeft: userInfos.PersonFamilyName + " - " + userInfos.PersonFamilyName,
TextDiagonal: userInfos.PersonPrimaryEmail,
SubmitTime: time.Now(),
},
)

Expand All @@ -205,7 +207,7 @@ func (r *mutationResolver) RequestMarkedExam(ctx context.Context, stringUUID str
return nil, err
}

utils.ExamsMarkedMetric.Inc()
prometheus.ExamsMarkedMetric.Inc()

return &stringUUID, nil
}
Expand Down
2 changes: 1 addition & 1 deletion utils/prometheus.go → server/prometheus/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package prometheus

import (
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/graph/model"
Expand Down
3 changes: 2 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/graph"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/graph/generated"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/lti_utils"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/prometheus"
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/utils"
chiprometheus "github.com/edjumacator/chi-prometheus"
"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -107,7 +108,7 @@ func main() {
})

// set the TotalExams metric initially
utils.UpdateTotalExamsMetric(db)
prometheus.UpdateTotalExamsMetric(db)

router.Handle("/metrics", promhttp.Handler())

Expand Down
2 changes: 2 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,11 @@ func GetExamCachePath(userID string, examUUID uuid.UUID) string {
return userID + "_" + examUUID.String()
}

// RMQMarkerTask models one of the tasks on the exam queue
type RMQMarkerTask struct {
ExamUUID uuid.UUID `json:"examuuid"`
UserID string `json:"userid"`
TextLeft string `json:"textleft"`
TextDiagonal string `json:"textdiagonal"`
SubmitTime time.Time `json:"submittime"`
}