Skip to content

Commit 5bc77c9

Browse files
committed
Put back some metrics to monitor what happens on graph updates.
1 parent e0f4aad commit 5bc77c9

File tree

2 files changed

+112
-37
lines changed

2 files changed

+112
-37
lines changed

internal/handlers/handler_update_graph.go

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,69 @@
11
package handlers
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"fmt"
76
"io"
87
"net/http"
98

109
"github.com/clems4ever/go-graphkb/internal/client"
1110
"github.com/clems4ever/go-graphkb/internal/knowledge"
11+
"github.com/clems4ever/go-graphkb/internal/metrics"
1212
"github.com/clems4ever/go-graphkb/internal/sources"
13+
"github.com/prometheus/client_golang/prometheus"
1314
"golang.org/x/sync/semaphore"
1415
)
1516

16-
func handleUpdate(registry sources.Registry, fn func(source string, body io.Reader) error, sem *semaphore.Weighted) http.HandlerFunc {
17+
func handleUpdate(registry sources.Registry, fn func(source string, body io.Reader) error, sem *semaphore.Weighted, operationDescriptor string) http.HandlerFunc {
1718
return func(w http.ResponseWriter, r *http.Request) {
1819
ok, source, err := IsTokenValid(registry, r)
1920
if err != nil {
2021
ReplyWithInternalError(w, err)
2122
return
2223
}
2324

25+
promLabels := prometheus.Labels{
26+
"source": source,
27+
"operation": operationDescriptor,
28+
}
29+
2430
if !ok {
31+
metrics.GraphUpdateRequestsUnauthorizedCounter.
32+
With(promLabels).
33+
Inc()
2534
ReplyWithUnauthorized(w)
2635
return
2736
}
2837

2938
{
3039
ok = sem.TryAcquire(1)
3140
if !ok {
41+
metrics.GraphUpdateRequestsRateLimitedCounter.
42+
With(promLabels).
43+
Inc()
3244
ReplyWithTooManyRequests(w)
3345
return
3446
}
3547
defer sem.Release(1)
3648

49+
metrics.GraphUpdateRequestsReceivedCounter.
50+
With(promLabels).
51+
Inc()
52+
3753
if err = fn(source, r.Body); err != nil {
54+
metrics.GraphUpdateRequestsFailedCounter.
55+
With(promLabels).
56+
Inc()
3857
ReplyWithInternalError(w, err)
3958
return
4059
}
60+
61+
metrics.GraphUpdateRequestsSucceededCounter.
62+
With(promLabels).
63+
Inc()
4164
}
4265

43-
_, err = bytes.NewBufferString("Graph has been received and will be processed soon").WriteTo(w)
66+
_, err = fmt.Fprint(w, "Graph update has been processed")
4467
if err != nil {
4568
ReplyWithInternalError(w, err)
4669
return
@@ -57,9 +80,17 @@ func PutSchema(registry sources.Registry, graphUpdater *knowledge.GraphUpdater,
5780
}
5881

5982
// TODO(c.michaud): verify compatibility of the schema with graph updates
60-
graphUpdater.UpdateSchema(source, requestBody.Schema)
83+
err := graphUpdater.UpdateSchema(source, requestBody.Schema)
84+
if err != nil {
85+
return fmt.Errorf("Unable to update the schema: %v", err)
86+
}
87+
88+
labels := prometheus.Labels{"source": source}
89+
metrics.GraphUpdateSchemaUpdatedCounter.
90+
With(labels).
91+
Inc()
6192
return nil
62-
}, sem)
93+
}, sem, "update_schema")
6394
}
6495

6596
// PutAssets upsert several assets into the graph of the data source
@@ -75,8 +106,13 @@ func PutAssets(registry sources.Registry, graphUpdater *knowledge.GraphUpdater,
75106
if err != nil {
76107
return fmt.Errorf("Unable to insert assets: %v", err)
77108
}
109+
labels := prometheus.Labels{"source": source}
110+
metrics.GraphUpdateAssetsInsertedCounter.
111+
With(labels).
112+
Add(float64(len(requestBody.Assets)))
113+
78114
return nil
79-
}, sem)
115+
}, sem, "insert_assets")
80116
}
81117

82118
// PutRelations upsert multiple relations into the graph of the data source
@@ -92,8 +128,13 @@ func PutRelations(registry sources.Registry, graphUpdater *knowledge.GraphUpdate
92128
if err != nil {
93129
return fmt.Errorf("Unable to insert relation: %v", err)
94130
}
131+
132+
labels := prometheus.Labels{"source": source}
133+
metrics.GraphUpdateRelationsInsertedCounter.
134+
With(labels).
135+
Add(float64(len(requestBody.Relations)))
95136
return nil
96-
}, sem)
137+
}, sem, "insert_relations")
97138
}
98139

99140
// DeleteAssets delete multiple assets from the graph of the data source
@@ -109,8 +150,13 @@ func DeleteAssets(registry sources.Registry, graphUpdater *knowledge.GraphUpdate
109150
if err != nil {
110151
return fmt.Errorf("Unable to remove assets: %v", err)
111152
}
153+
154+
labels := prometheus.Labels{"source": source}
155+
metrics.GraphUpdateAssetsDeletedCounter.
156+
With(labels).
157+
Add(float64(len(requestBody.Assets)))
112158
return nil
113-
}, sem)
159+
}, sem, "delete_assets")
114160
}
115161

116162
// DeleteRelations remove multiple relations from the graph of the data source
@@ -126,6 +172,11 @@ func DeleteRelations(registry sources.Registry, graphUpdater *knowledge.GraphUpd
126172
if err != nil {
127173
return fmt.Errorf("Unable to remove relation: %v", err)
128174
}
175+
176+
labels := prometheus.Labels{"source": source}
177+
metrics.GraphUpdateRelationsDeletedCounter.
178+
With(labels).
179+
Add(float64(len(requestBody.Relations)))
129180
return nil
130-
}, sem)
181+
}, sem, "delete_relations")
131182
}

internal/metrics/metrics.go

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,66 @@ var StartTimeGauge = promauto.NewGauge(prometheus.GaugeOpts{
1111
Help: "The timestamp of the time when the app started",
1212
})
1313

14-
// ********************* GRAPH UPDATE ENQUEUING ******************
14+
// ********************* GRAPH UPDATE REQUESTS ******************
1515

16-
// GraphUpdateEnqueuingRequestsReceivedCounter reports the number of authorized updates request received by the webserver
17-
var GraphUpdateEnqueuingRequestsReceivedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
18-
Name: "go_graphkb_graph_update_enqueuing_requests_received_counter",
19-
Help: "The number of graph update enqueuing requests received by the web server but not yet processed",
20-
}, []string{"source"})
16+
// GraphUpdateRequestsReceivedCounter reports the number of authorized and not rate limited updates requests received by the webserver
17+
var GraphUpdateRequestsReceivedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
18+
Name: "go_graphkb_graph_update_requests_received_counter",
19+
Help: "The number of graph updates (insertion or removal of assets or relations) received. A request is considered received if authorization is valid and the request has not been rate limited",
20+
}, []string{"source", "operation"})
21+
22+
// GraphUpdateRequestsRateLimitedCounter reports the number of unauthorized updates requests received by the webserver
23+
var GraphUpdateRequestsRateLimitedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
24+
Name: "go_graphkb_graph_update_requests_rate_limited_counter",
25+
Help: "The number of graph updates which were rate limited",
26+
}, []string{"source", "operation"})
27+
28+
// GraphUpdateRequestsUnauthorizedCounter reports the number of unauthorized updates requests received by the webserver
29+
var GraphUpdateRequestsUnauthorizedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
30+
Name: "go_graphkb_graph_update_requests_unauthorized_counter",
31+
Help: "The number of graph updates which were unauthorized",
32+
}, []string{"source", "operation"})
2133

22-
// GraphUpdateEnqueuingRequestsFailedCounter reports the number of failed update requests
23-
var GraphUpdateEnqueuingRequestsFailedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
24-
Name: "go_graphkb_graph_update_enqueuing_requests_failed_counter",
25-
Help: "The number of failed graph update enqueuing requests",
26-
}, []string{"source", "status_code"})
34+
// GraphUpdateRequestsFailedCounter reports the number of failed update requests
35+
var GraphUpdateRequestsFailedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
36+
Name: "go_graphkb_graph_update_requests_failed_counter",
37+
Help: "The number of failed graph update",
38+
}, []string{"source", "operation"})
2739

28-
// GraphUpdateEnqueuingRequestsSucceededCounter reports the number of successful enqueuing requests
29-
var GraphUpdateEnqueuingRequestsSucceededCounter = promauto.NewCounterVec(prometheus.CounterOpts{
30-
Name: "go_graphkb_graph_update_enqueuing_requests_succeeded_counter",
31-
Help: "The number of successful graph update enqueuing requests",
40+
// GraphUpdateRequestsSucceededCounter reports the number of successful update requests
41+
var GraphUpdateRequestsSucceededCounter = promauto.NewCounterVec(prometheus.CounterOpts{
42+
Name: "go_graphkb_graph_update_requests_succeeded_counter",
43+
Help: "The number of succeeded graph update",
44+
}, []string{"source", "operation"})
45+
46+
// ********************* GRAPH UPDATE COUNTERS ******************
47+
48+
// GraphUpdateSchemaUpdatedCounter reports the number of schema updated since the start of the process
49+
var GraphUpdateSchemaUpdatedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
50+
Name: "go_graphkb_graph_update_schema_updated_counter",
51+
Help: "The number of assets inserted since the start of the process",
3252
}, []string{"source"})
3353

34-
// ********************* GRAPH PROCESSING *************************
54+
// GraphUpdateAssetsInsertedCounter reports the number of assets inserted since the start of the process
55+
var GraphUpdateAssetsInsertedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
56+
Name: "go_graphkb_graph_update_assets_inserted_counter",
57+
Help: "The number of assets inserted since the start of the process",
58+
}, []string{"source"})
3559

36-
// GraphUpdatesProcessingRequestedCounter reports the number of updates to be processed and imported into DB.
37-
var GraphUpdatesProcessingRequestedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
38-
Name: "go_graphkb_graph_updates_processing_requested_counter",
39-
Help: "The number of update requests to be processed and imported into DB",
60+
// GraphUpdateRelationsInsertedCounter reports the number of relations inserted since the start of the process
61+
var GraphUpdateRelationsInsertedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
62+
Name: "go_graphkb_graph_update_relations_inserted_counter",
63+
Help: "The number of relations inserted since the start of the process",
4064
}, []string{"source"})
4165

42-
// GraphUpdatesProcessingSucceededCounter reports the number of updates which have successfully been processed
43-
var GraphUpdatesProcessingSucceededCounter = promauto.NewCounterVec(prometheus.CounterOpts{
44-
Name: "go_graphkb_graph_updates_processing_succeeded_counter",
45-
Help: "The number of update requests processed successfully",
66+
// GraphUpdateAssetsDeletedCounter reports the number of assets inserted since the start of the process
67+
var GraphUpdateAssetsDeletedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
68+
Name: "go_graphkb_graph_update_assets_deleted_counter",
69+
Help: "The number of assets deleted since the start of the process",
4670
}, []string{"source"})
4771

48-
// GraphUpdatesProcessingFailedCounter reports the number of updates which failed to be processed
49-
var GraphUpdatesProcessingFailedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
50-
Name: "go_graphkb_graph_updates_failed_counter",
51-
Help: "The number of update requests which failed",
72+
// GraphUpdateRelationsDeletedCounter reports the number of relations deleted since the start of the process
73+
var GraphUpdateRelationsDeletedCounter = promauto.NewCounterVec(prometheus.CounterOpts{
74+
Name: "go_graphkb_graph_update_relations_deleted_counter",
75+
Help: "The number of relations deleted since the start of the process",
5276
}, []string{"source"})

0 commit comments

Comments
 (0)