Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: unistack-org/micro
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.2.15
Choose a base ref
...
head repository: unistack-org/micro
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.2.16
Choose a head ref
  • 2 commits
  • 7 files changed
  • 1 contributor

Commits on Mar 3, 2021

  1. fixup domain in ListServices

    Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
    vtolstov committed Mar 3, 2021

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    a862562 View commit details
  2. tracer: finalize tracer implementation

    Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
    vtolstov committed Mar 3, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1f0482f View commit details
Showing with 477 additions and 191 deletions.
  1. +2 −2 register/memory.go
  2. +33 −28 tracer/context.go
  3. +0 −99 tracer/memory.go
  4. +69 −0 tracer/noop.go
  5. +10 −23 tracer/options.go
  6. +46 −39 tracer/tracer.go
  7. +317 −0 tracer/wrapper/wrapper.go
4 changes: 2 additions & 2 deletions register/memory.go
Original file line number Diff line number Diff line change
@@ -369,9 +369,9 @@ func (m *memory) ListServices(ctx context.Context, opts ...ListOption) ([]*Servi
// serialize the result, each version counts as an individual service
var result []*Service

for domain, service := range services {
for _, service := range services {
for _, version := range service {
result = append(result, recordToService(version, domain))
result = append(result, recordToService(version, options.Domain))
}
}

61 changes: 33 additions & 28 deletions tracer/context.go
Original file line number Diff line number Diff line change
@@ -3,41 +3,46 @@ package tracer

import (
"context"

"github.com/unistack-org/micro/v3/metadata"
)

const (
traceIDKey = "Micro-Trace-Id"
spanIDKey = "Micro-Span-Id"
)
type tracerKey struct{}

// FromContext returns a span from context
func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFound bool) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", "", false
// FromContext returns a tracer from context
func FromContext(ctx context.Context) Tracer {
if ctx == nil {
return DefaultTracer
}
if tracer, ok := ctx.Value(tracerKey{}).(Tracer); ok {
return tracer
}
traceID, traceOk := md.Get(traceIDKey)
microID, microOk := md.Get("Micro-Id")
if !traceOk && !microOk {
isFound = false
return
return DefaultTracer
}

// NewContext saves the tracer in the context
func NewContext(ctx context.Context, tracer Tracer) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, tracerKey{}, tracer)
}

type spanKey struct{}

// SpanFromContext returns a span from context
func SpanFromContext(ctx context.Context) Span {
if ctx == nil {
return &noopSpan{}
}
if !traceOk {
traceID = microID
if span, ok := ctx.Value(spanKey{}).(Span); ok {
return span
}
parentSpanID, ok = md.Get(spanIDKey)
return traceID, parentSpanID, ok
return &noopSpan{}
}

// NewContext saves the trace and span ids in the context
func NewContext(ctx context.Context, traceID, parentSpanID string) context.Context {
md, ok := metadata.FromContext(ctx)
if !ok {
md = metadata.New(2)
// NewSpanContext saves the span in the context
func NewSpanContext(ctx context.Context, span Span) context.Context {
if ctx == nil {
ctx = context.Background()
}
md.Set(traceIDKey, traceID)
md.Set(spanIDKey, parentSpanID)
return metadata.NewContext(ctx, md)
return context.WithValue(ctx, spanKey{}, span)
}
99 changes: 0 additions & 99 deletions tracer/memory.go

This file was deleted.

69 changes: 69 additions & 0 deletions tracer/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package tracer

import (
"context"
)

type noopTracer struct {
opts Options
}

func (t *noopTracer) Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span) {
span := &noopSpan{
name: name,
ctx: ctx,
tracer: t,
}
if span.ctx == nil {
span.ctx = context.Background()
}
return NewSpanContext(ctx, span), span
}

func (t *noopTracer) Init(opts ...Option) error {
for _, o := range opts {
o(&t.opts)
}
return nil
}

func (t *noopTracer) Name() string {
return t.opts.Name
}

type noopSpan struct {
name string
ctx context.Context
tracer Tracer
}

func (s *noopSpan) Finish(opts ...SpanOption) {

}

func (s *noopSpan) Context() context.Context {
return s.ctx
}

func (s *noopSpan) Tracer() Tracer {
return s.tracer
}

func (s *noopSpan) AddEvent(name string, opts ...EventOption) {

}

func (s *noopSpan) SetName(name string) {
s.name = name
}

func (s *noopSpan) SetLabels(labels ...Label) {

}

// NewTracer returns new memory tracer
func NewTracer(opts ...Option) Tracer {
return &noopTracer{
opts: NewOptions(opts...),
}
}
33 changes: 10 additions & 23 deletions tracer/options.go
Original file line number Diff line number Diff line change
@@ -2,39 +2,27 @@ package tracer

import "github.com/unistack-org/micro/v3/logger"

var (
// DefaultSize of the buffer
DefaultSize = 64
)
type SpanOptions struct {
}

type SpanOption func(o *SpanOptions)

type EventOptions struct {
}

type EventOption func(o *EventOptions)

// Options struct
type Options struct {
// Name of the tracer
Name string
// Logger is the logger for messages
Logger logger.Logger
// Size is the size of ring buffer
Size int
}

// Option func
type Option func(o *Options)

// ReadOptions struct
type ReadOptions struct {
// Trace id
Trace string
}

// ReadOption func
type ReadOption func(o *ReadOptions)

// ReadTrace read the given trace
func ReadTrace(t string) ReadOption {
return func(o *ReadOptions) {
o.Trace = t
}
}

// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {
@@ -46,7 +34,6 @@ func Logger(l logger.Logger) Option {
func NewOptions(opts ...Option) Options {
options := Options{
Logger: logger.DefaultLogger,
Size: DefaultSize,
}
for _, o := range opts {
o(&options)
Loading