implement metrics, logging and tracing

This commit is contained in:
David Christofas
2020-03-16 16:55:10 +01:00
parent 35141c8e09
commit 06151de3f4
7 changed files with 110 additions and 291 deletions
+14 -1
View File
@@ -5,6 +5,7 @@ import (
"github.com/owncloud/ocis-thumbnails/pkg/metrics"
v0proto "github.com/owncloud/ocis-thumbnails/pkg/proto/v0"
"github.com/prometheus/client_golang/prometheus"
)
// NewInstrument returns a service that instruments metrics.
@@ -22,5 +23,17 @@ type instrument struct {
// GetThumbnail implements the ThumbnailServiceHandler interface.
func (i instrument) GetThumbnail(ctx context.Context, req *v0proto.GetRequest, rsp *v0proto.GetResponse) error {
return i.next.GetThumbnail(ctx, req, rsp)
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
us := v * 1000_000
i.metrics.Latency.WithLabelValues().Observe(us)
i.metrics.Duration.WithLabelValues().Observe(v)
}))
defer timer.ObserveDuration()
err := i.next.GetThumbnail(ctx, req, rsp)
if err != nil {
i.metrics.Counter.WithLabelValues().Inc()
}
return err
}
+18 -1
View File
@@ -2,6 +2,7 @@ package svc
import (
"context"
"time"
"github.com/owncloud/ocis-pkg/v2/log"
v0proto "github.com/owncloud/ocis-thumbnails/pkg/proto/v0"
@@ -22,5 +23,21 @@ type logging struct {
// GetThumbnail implements the ThumbnailServiceHandler interface.
func (l logging) GetThumbnail(ctx context.Context, req *v0proto.GetRequest, rsp *v0proto.GetResponse) error {
return l.next.GetThumbnail(ctx, req, rsp)
start := time.Now()
err := l.next.GetThumbnail(ctx, req, rsp)
logger := l.logger.With().
Str("method", "Thumbnails.GetThumbnail").
Dur("duration", time.Since(start)).
Logger()
if err != nil {
logger.Warn().
Err(err).
Msg("Failed to execute")
} else {
logger.Debug().
Msg("")
}
return err
}
+12
View File
@@ -4,6 +4,7 @@ import (
"context"
v0proto "github.com/owncloud/ocis-thumbnails/pkg/proto/v0"
"go.opencensus.io/trace"
)
// NewTracing returns a service that instruments traces.
@@ -19,5 +20,16 @@ type tracing struct {
// GetThumbnail implements the ThumbnailServiceHandler interface.
func (t tracing) GetThumbnail(ctx context.Context, req *v0proto.GetRequest, rsp *v0proto.GetResponse) error {
ctx, span := trace.StartSpan(ctx, "Thumbnails.GetThumbnail")
defer span.End()
span.Annotate([]trace.Attribute{
trace.StringAttribute("filepath", req.Filepath),
trace.StringAttribute("filetype", req.Filetype.String()),
trace.StringAttribute("etag", req.Etag),
trace.Int64Attribute("width", int64(req.Width)),
trace.Int64Attribute("height", int64(req.Height)),
}, "Execute Thumbnails.GetThumbnail handler")
return t.next.GetThumbnail(ctx, req, rsp)
}