Commit 006de07d authored by acud's avatar acud Committed by GitHub

fix tracing (#729)

parent 22d7e53b
......@@ -59,7 +59,6 @@ func (s *Service) chunksWorker() {
<-s.quit
cancel()
}()
sem := make(chan struct{}, 10)
inflight := make(map[string]struct{})
var mtx sync.Mutex
......
......@@ -20,6 +20,7 @@ import (
"github.com/ethersphere/bee/pkg/tags"
"github.com/ethersphere/bee/pkg/topology"
"github.com/ethersphere/bee/pkg/tracing"
"github.com/opentracing/opentracing-go"
)
const (
......@@ -62,6 +63,7 @@ func New(streamer p2p.Streamer, storer storage.Putter, closestPeerer topology.Cl
accounting: accounting,
pricer: pricer,
metrics: newMetrics(),
tracer: tracer,
}
return ps
}
......@@ -95,8 +97,7 @@ func (ps *PushSync) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream)
if err != nil {
return fmt.Errorf("chunk delivery from peer %s: %w", p.Address.String(), err)
}
span, _, ctx := ps.tracer.StartSpanFromContext(ctx, "pushsync-handler", ps.logger)
span = span.SetTag("address", chunk.Address().String())
span, _, ctx := ps.tracer.StartSpanFromContext(ctx, "pushsync-handler", ps.logger, opentracing.Tag{Key: "address", Value: chunk.Address().String()})
defer span.Finish()
// Select the closest peer to forward the chunk
......@@ -219,8 +220,7 @@ func (ps *PushSync) receiveReceipt(r protobuf.Reader) (receipt pb.Receipt, err e
// a receipt from that peer and returns error or nil based on the receiving and
// the validity of the receipt.
func (ps *PushSync) PushChunkToClosest(ctx context.Context, ch swarm.Chunk) (*Receipt, error) {
span, _, ctx := ps.tracer.StartSpanFromContext(ctx, "pushsync-push", ps.logger)
span = span.SetTag("address", ch.Address().String())
span, _, ctx := ps.tracer.StartSpanFromContext(ctx, "pushsync-push", ps.logger, opentracing.Tag{Key: "address", Value: ch.Address().String()})
defer span.Finish()
peer, err := ps.peerSuggester.ClosestPeer(ch.Address())
......
......@@ -18,6 +18,7 @@ import (
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/topology"
"github.com/ethersphere/bee/pkg/tracing"
"github.com/opentracing/opentracing-go"
"golang.org/x/sync/singleflight"
)
......@@ -84,8 +85,7 @@ func (s *Service) RetrieveChunk(ctx context.Context, addr swarm.Address) (swarm.
defer cancel()
v, err, _ := s.singleflight.Do(addr.String(), func() (interface{}, error) {
span, logger, ctx := s.tracer.StartSpanFromContext(ctx, "retrieve-chunk", s.logger)
span = span.SetTag("address", addr.String())
span, logger, ctx := s.tracer.StartSpanFromContext(ctx, "retrieve-chunk", s.logger, opentracing.Tag{Key: "address", Value: addr.String()})
defer span.Finish()
var skipPeers []swarm.Address
......@@ -238,8 +238,7 @@ func (s *Service) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) (e
if err := r.ReadMsg(&req); err != nil {
return fmt.Errorf("read request: %w peer %s", err, p.Address.String())
}
span, _, ctx := s.tracer.StartSpanFromContext(ctx, "handle-retrieve-chunk", s.logger)
span = span.SetTag("address", swarm.NewAddress(req.Addr).String())
span, _, ctx := s.tracer.StartSpanFromContext(ctx, "handle-retrieve-chunk", s.logger, opentracing.Tag{Key: "address", Value: swarm.NewAddress(req.Addr).String()})
defer span.Finish()
ctx = context.WithValue(ctx, requestSourceContextKey{}, p.Address.String())
......
......@@ -25,12 +25,13 @@ var (
// in p2p Headers or context.
ErrContextNotFound = errors.New("tracing context not found")
// contextKey is used to reference a tracing context span as context value.
contextKey = struct{}{}
// noopTracer is the tracer that does nothing to handle a nil Tracer usage.
noopTracer = &Tracer{tracer: new(opentracing.NoopTracer)}
)
// contextKey is used to reference a tracing context span as context value.
type contextKey struct{}
// LogField is the key in log message field that holds tracing id value.
const LogField = "traceid"
......@@ -160,13 +161,13 @@ func (t *Tracer) WithContextFromHeaders(ctx context.Context, headers p2p.Headers
// WithContext adds tracing span context to go context.
func WithContext(ctx context.Context, c opentracing.SpanContext) context.Context {
return context.WithValue(ctx, contextKey, c)
return context.WithValue(ctx, contextKey{}, c)
}
// FromContext return tracing span context from go context. If the tracing span
// context is not present in go context, nil is returned.
func FromContext(ctx context.Context) opentracing.SpanContext {
c, ok := ctx.Value(contextKey).(opentracing.SpanContext)
c, ok := ctx.Value(contextKey{}).(opentracing.SpanContext)
if !ok {
return nil
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment