Compare commits

...

4 Commits

Author SHA1 Message Date
3f5b19497c meter: add Clone method
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-10-09 23:50:57 +03:00
37d937d7ae meter: add missing options
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-10-09 19:16:45 +03:00
7d68f2396e tracer: dont return noop from context
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-10-07 22:46:47 +03:00
0854a7ea72 micro: add simple test
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-10-07 20:59:27 +03:00
8 changed files with 81 additions and 16 deletions

View File

@@ -28,17 +28,31 @@ var (
// Meter is an interface for collecting and instrumenting metrics
type Meter interface {
// Name returns meter name
Name() string
// Init initialize meter
Init(opts ...Option) error
// Clone create meter copy with new options
Clone(opts ...Option) Meter
// Counter get or create counter
Counter(name string, labels ...string) Counter
// FloatCounter get or create float counter
FloatCounter(name string, labels ...string) FloatCounter
// Gauge get or create gauge
Gauge(name string, fn func() float64, labels ...string) Gauge
// Set create new meter metrics set
Set(opts ...Option) Meter
// Histogram get or create histogram
Histogram(name string, labels ...string) Histogram
// Summary get or create summary
Summary(name string, labels ...string) Summary
// SummaryExt get or create summary with spcified quantiles and window time
SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) Summary
// Write writes metrics to io.Writer
Write(w io.Writer, opts ...Option) error
// Options returns meter options
Options() Options
// String return meter type
String() string
}

View File

@@ -15,6 +15,15 @@ func NewMeter(opts ...Option) Meter {
return &noopMeter{opts: NewOptions(opts...)}
}
// Clone return old meter with new options
func (r *noopMeter) Clone(opts ...Option) Meter {
options := r.opts
for _, o := range opts {
o(&options)
}
return &noopMeter{opts: options}
}
func (r *noopMeter) Name() string {
return r.opts.Name
}

View File

@@ -51,6 +51,20 @@ func NewOptions(opt ...Option) Options {
return opts
}
// LabelPrefix sets the labels prefix
func LabelPrefix(pref string) Option {
return func(o *Options) {
o.LabelPrefix = pref
}
}
// MetricPrefix sets the metric prefix
func MetricPrefix(pref string) Option {
return func(o *Options) {
o.MetricPrefix = pref
}
}
// Context sets the metrics context
func Context(ctx context.Context) Option {
return func(o *Options) {

View File

@@ -392,8 +392,12 @@ type nameIface interface {
Name() string
}
func getNameIndex(n string, ifaces ...interface{}) int {
for idx, iface := range ifaces {
func getNameIndex(n string, ifaces interface{}) int {
values, ok := ifaces.([]interface{})
if !ok {
return 0
}
for idx, iface := range values {
if ifc, ok := iface.(nameIface); ok && ifc.Name() == n {
return idx
}

22
service_test.go Normal file
View File

@@ -0,0 +1,22 @@
package micro
import (
"testing"
)
type testItem struct {
name string
}
func (ti *testItem) Name() string {
return ti.name
}
func TestGetNameIndex(t *testing.T) {
item1 := &testItem{name: "first"}
item2 := &testItem{name: "second"}
items := []interface{}{item1, item2}
if idx := getNameIndex("second", items); idx != 1 {
t.Fatalf("getNameIndex func error, item not found")
}
}

View File

@@ -8,14 +8,14 @@ import (
type tracerKey struct{}
// FromContext returns a tracer from context
func FromContext(ctx context.Context) Tracer {
func FromContext(ctx context.Context) (Tracer, bool) {
if ctx == nil {
return DefaultTracer
return nil, false
}
if tracer, ok := ctx.Value(tracerKey{}).(Tracer); ok {
return tracer
return tracer, true
}
return DefaultTracer
return nil, false
}
// NewContext saves the tracer in the context
@@ -29,14 +29,14 @@ func NewContext(ctx context.Context, tracer Tracer) context.Context {
type spanKey struct{}
// SpanFromContext returns a span from context
func SpanFromContext(ctx context.Context) Span {
func SpanFromContext(ctx context.Context) (Span, bool) {
if ctx == nil {
return &noopSpan{}
return nil, false
}
if span, ok := ctx.Value(spanKey{}).(Span); ok {
return span
return span, true
}
return &noopSpan{}
return nil, false
}
// NewSpanContext saves the span in the context

View File

@@ -35,6 +35,7 @@ type noopSpan struct {
ctx context.Context
tracer Tracer
name string
labels []Label
}
func (s *noopSpan) Finish(opts ...SpanOption) {
@@ -56,6 +57,7 @@ func (s *noopSpan) SetName(name string) {
}
func (s *noopSpan) SetLabels(labels ...Label) {
s.labels = labels
}
// NewTracer returns new memory tracer

View File

@@ -38,26 +38,26 @@ type Label struct {
key string
}
func Any(k string, v interface{}) Label {
func LabelAny(k string, v interface{}) Label {
return Label{key: k, val: v}
}
func String(k string, v string) Label {
func LabelString(k string, v string) Label {
return Label{key: k, val: v}
}
func Int(k string, v int) Label {
func LabelInt(k string, v int) Label {
return Label{key: k, val: v}
}
func Int64(k string, v int64) Label {
func LabelInt64(k string, v int64) Label {
return Label{key: k, val: v}
}
func Float64(k string, v float64) Label {
func LabelFloat64(k string, v float64) Label {
return Label{key: k, val: v}
}
func Bool(k string, v bool) Label {
func LabelBool(k string, v bool) Label {
return Label{key: k, val: v}
}