use metadata.Metadata (#8)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit was merged in pull request #8.
This commit is contained in:
2020-11-18 16:50:41 +03:00
committed by GitHub
parent e0ef8b2953
commit daffa9e548
28 changed files with 119 additions and 84 deletions

View File

@@ -2,6 +2,8 @@ package metrics
import (
"time"
"github.com/unistack-org/micro/v3/metadata"
)
// NoopReporter is an noop implementation of Reporter:
@@ -25,17 +27,17 @@ func (r *noopReporter) Init(opts ...Option) error {
}
// Count implements the Reporter interface Count method:
func (r *noopReporter) Count(metricName string, value int64, tags Tags) error {
func (r *noopReporter) Count(metricName string, value int64, md metadata.Metadata) error {
return nil
}
// Gauge implements the Reporter interface Gauge method:
func (r *noopReporter) Gauge(metricName string, value float64, tags Tags) error {
func (r *noopReporter) Gauge(metricName string, value float64, md metadata.Metadata) error {
return nil
}
// Timing implements the Reporter interface Timing method:
func (r *noopReporter) Timing(metricName string, value time.Duration, tags Tags) error {
func (r *noopReporter) Timing(metricName string, value time.Duration, md metadata.Metadata) error {
return nil
}

View File

@@ -1,6 +1,9 @@
package metrics
import "github.com/unistack-org/micro/v3/logger"
import (
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/metadata"
)
var (
// The Prometheus metrics will be made available on this port:
@@ -18,7 +21,7 @@ type Option func(*Options)
type Options struct {
Address string
Path string
DefaultTags Tags
DefaultTags metadata.Metadata
TimingObjectives map[float64]float64
Logger logger.Logger
}
@@ -27,7 +30,7 @@ type Options struct {
func NewOptions(opt ...Option) Options {
opts := Options{
Address: defaultPrometheusListenAddress,
DefaultTags: make(Tags),
DefaultTags: metadata.New(2),
Path: defaultPath,
TimingObjectives: defaultTimingObjectives,
}
@@ -54,9 +57,9 @@ func Address(value string) Option {
}
// DefaultTags will be added to every metric:
func DefaultTags(value Tags) Option {
func DefaultTags(md metadata.Metadata) Option {
return func(o *Options) {
o.DefaultTags = value
o.DefaultTags = metadata.Copy(md)
}
}

View File

@@ -1,10 +1,11 @@
// Package metrics is for instrumentation and debugging
package metrics
import "time"
import (
"time"
// Tags is a map of fields to add to a metric:
type Tags map[string]string
"github.com/unistack-org/micro/v3/metadata"
)
var (
DefaultReporter Reporter = NewReporter()
@@ -13,8 +14,8 @@ var (
// Reporter is an interface for collecting and instrumenting metrics
type Reporter interface {
Init(...Option) error
Count(id string, value int64, tags Tags) error
Gauge(id string, value float64, tags Tags) error
Timing(id string, value time.Duration, tags Tags) error
Count(id string, value int64, md metadata.Metadata) error
Gauge(id string, value float64, md metadata.Metadata) error
Timing(id string, value time.Duration, md metadata.Metadata) error
Options() Options
}

View File

@@ -1,10 +1,10 @@
package wrapper
import (
"context"
"time"
"context"
"github.com/unistack-org/micro/v3/metadata"
"github.com/unistack-org/micro/v3/metrics"
"github.com/unistack-org/micro/v3/server"
)
@@ -26,9 +26,8 @@ func (w *Wrapper) HandlerFunc(handlerFunction server.HandlerFunc) server.Handler
return func(ctx context.Context, req server.Request, rsp interface{}) error {
// Build some tags to describe the call:
tags := metrics.Tags{
"method": req.Method(),
}
tags := metadata.New(2)
tags.Set("method", req.Method())
// Start the clock:
callTime := time.Now()