implement metrics, logging and tracing
This commit is contained in:
+34
-10
@@ -1,5 +1,7 @@
|
||||
package metrics
|
||||
|
||||
import "github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
var (
|
||||
// Namespace defines the namespace for the defines metrics.
|
||||
Namespace = "ocis"
|
||||
@@ -10,23 +12,45 @@ var (
|
||||
|
||||
// Metrics defines the available metrics of this service.
|
||||
type Metrics struct {
|
||||
// Counter *prometheus.CounterVec
|
||||
Counter *prometheus.CounterVec
|
||||
Latency *prometheus.SummaryVec
|
||||
Duration *prometheus.HistogramVec
|
||||
}
|
||||
|
||||
// New initializes the available metrics.
|
||||
func New() *Metrics {
|
||||
m := &Metrics{
|
||||
// Counter: prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
// Namespace: Namespace,
|
||||
// Subsystem: Subsystem,
|
||||
// Name: "greet_total",
|
||||
// Help: "How many greeting requests processed",
|
||||
// }, []string{}),
|
||||
Counter: prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: Namespace,
|
||||
Subsystem: Subsystem,
|
||||
Name: "getthumbnail_total",
|
||||
Help: "How many GetThumbnail requests processed",
|
||||
}, []string{}),
|
||||
Latency: prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
||||
Namespace: Namespace,
|
||||
Subsystem: Subsystem,
|
||||
Name: "getthumbnail_latency_microseconds",
|
||||
Help: "GetThumbnail request latencies in microseconds",
|
||||
}, []string{}),
|
||||
Duration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: Namespace,
|
||||
Subsystem: Subsystem,
|
||||
Name: "getthumbnail_duration_seconds",
|
||||
Help: "GetThumbnail method requests time in seconds",
|
||||
}, []string{}),
|
||||
}
|
||||
|
||||
// prometheus.Register(
|
||||
// m.Counter,
|
||||
// )
|
||||
prometheus.Register(
|
||||
m.Counter,
|
||||
)
|
||||
|
||||
prometheus.Register(
|
||||
m.Latency,
|
||||
)
|
||||
|
||||
prometheus.Register(
|
||||
m.Duration,
|
||||
)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user