Skip to content

Commit

Permalink
chore: Scoping metrics by source
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Dec 11, 2021
1 parent 7c5c94d commit f3aee57
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 178 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
google.golang.org/protobuf v1.26.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
161 changes: 6 additions & 155 deletions go.sum

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions pkg/exas/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,34 @@ func (a App) AmqpHandler(message amqp.Delivery) error {

var item model.StorageItem
if err := json.Unmarshal(message.Body, &item); err != nil {
a.increaseMetric("exif", "invalid")
a.increaseMetric("amqp", "exif", "invalid")
return fmt.Errorf("unable to decode: %s", err)
}

if strings.Contains(item.Pathname, "..") {
a.increaseMetric("amqp", "exif", "invalid_path")
return errors.New("input path with dots is not allowed")
}

inputFilename := filepath.Join(a.workingDir, item.Pathname)

if info, err := os.Stat(inputFilename); err != nil || info.IsDir() {
a.increaseMetric("exif", "not_found")
a.increaseMetric("amqp", "exif", "not_found")
return fmt.Errorf("input `%s` doesn't exist or is a directory", item.Pathname)
}

exif, err := a.get(inputFilename)
if err != nil {
a.increaseMetric("exif", "error")
a.increaseMetric("amqp", "exif", "error")
return fmt.Errorf("unable to get exif: %s", err)
}

if err := a.amqpClient.PublishJSON(amqpResponse{Item: item, Exif: exif}, a.amqpExchange, a.amqpRoutingKey); err != nil {
a.increaseMetric("amqp", "exif", "publish_error")
return fmt.Errorf("unable to publish amqp message: %s", err)
}

a.increaseMetric("exif", "success")
a.increaseMetric("amqp", "exif", "success")

return nil
}
5 changes: 1 addition & 4 deletions pkg/exas/exas.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func New(config Config, geocodeApp geocode.App, prometheusRegisterer prometheus.
amqpExchange: strings.TrimSpace(*config.amqpExchange),
amqpRoutingKey: strings.TrimSpace(*config.amqpRoutingKey),

metric: prom.CounterVec(prometheusRegisterer, "exas", "", "item", "kind", "state"),
metric: prom.CounterVec(prometheusRegisterer, "exas", "", "item", "source", "kind", "state"),
}
}

Expand Down Expand Up @@ -131,11 +131,8 @@ func (a App) get(input string) (model.Exif, error) {
}

if a.geocodeApp.Enabled() {
a.increaseMetric("geocode", "request")

geocode, err := a.geocodeApp.GetGeocoding(exif)
if err != nil {
a.increaseMetric("geocode", "error")
return exif, fmt.Errorf("unable to append geocoding: %s", err)
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/exas/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@ func (a App) handleGet(w http.ResponseWriter, r *http.Request) {
inputFilename := r.URL.Path

if strings.Contains(inputFilename, "..") {
a.increaseMetric("http", "exif", "invalid_path")
httperror.BadRequest(w, errors.New("input path with dots is not allowed"))
return
}

inputFilename = filepath.Join(a.workingDir, inputFilename)

if info, err := os.Stat(inputFilename); err != nil || info.IsDir() {
a.increaseMetric("exif", "not_found")
a.increaseMetric("http", "exif", "not_found")
httperror.BadRequest(w, fmt.Errorf("input `%s` doesn't exist or is a directory", inputFilename))
return
}

exif, err := a.get(inputFilename)
if err != nil {
a.increaseMetric("exif", "error")
httperror.InternalServerError(w, err)
a.increaseMetric("http", "exif", "error")
return
}

a.increaseMetric("exif", "success")
httpjson.Write(w, http.StatusOK, exif)
a.increaseMetric("http", "exif", "success")
}
4 changes: 2 additions & 2 deletions pkg/exas/metric.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package exas

func (a App) increaseMetric(kind, state string) {
func (a App) increaseMetric(source, kind, state string) {
if a.metric == nil {
return
}

a.metric.WithLabelValues(kind, state).Inc()
a.metric.WithLabelValues(source, kind, state).Inc()
}
7 changes: 4 additions & 3 deletions pkg/exas/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (a App) handlePost(w http.ResponseWriter, r *http.Request) {
writer, err := os.OpenFile(inputName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
httperror.InternalServerError(w, err)
a.increaseMetric("http", "exif", "not_found")
return
}

Expand All @@ -32,19 +33,19 @@ func (a App) handlePost(w http.ResponseWriter, r *http.Request) {
}()

if err := loadFile(writer, r); err != nil {
a.increaseMetric("exif", "load_error")
httperror.InternalServerError(w, err)
a.increaseMetric("http", "exif", "load_error")
return
}

exif, err := a.get(inputName)
if err != nil {
a.increaseMetric("exif", "error")
a.increaseMetric("http", "exif", "error")
httperror.InternalServerError(w, err)
return
}

a.increaseMetric("exif", "success")
a.increaseMetric("http", "exif", "success")
httpjson.Write(w, http.StatusOK, exif)
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/geocode/geocode.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (a App) GetGeocoding(exif model.Exif) (model.Geocode, error) {
return geocode, nil
}

a.increaseMetric("good")
a.increaseMetric("success")

return geocode, nil
}
Expand Down Expand Up @@ -216,7 +216,5 @@ func (a App) increaseMetric(state string) {
return
}

a.metric.With(prometheus.Labels{
"state": state,
}).Inc()
a.metric.WithLabelValues(state).Inc()
}

0 comments on commit f3aee57

Please sign in to comment.