Skip to content

Commit

Permalink
Merge pull request ipfs/go-blockservice#91 from iand/feat-tracing
Browse files Browse the repository at this point in the history
feat: add basic tracing

This commit was moved from ipfs/go-blockservice@058572e
  • Loading branch information
iand authored May 12, 2022
2 parents 00a68a6 + 435dbbc commit 2ad1dc9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
25 changes: 24 additions & 1 deletion blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
"io"
"sync"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-blockservice/internal"
cid "github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
Expand Down Expand Up @@ -129,6 +133,9 @@ func NewSession(ctx context.Context, bs BlockService) *Session {
// AddBlock adds a particular block to the service, Putting it into the datastore.
// TODO pass a context into this if the remote.HasBlock is going to remain here.
func (s *blockService) AddBlock(ctx context.Context, o blocks.Block) error {
ctx, span := internal.StartSpan(ctx, "blockService.AddBlock")
defer span.End()

c := o.Cid()
// hash security
err := verifcid.ValidateCid(c)
Expand Down Expand Up @@ -157,6 +164,9 @@ func (s *blockService) AddBlock(ctx context.Context, o blocks.Block) error {
}

func (s *blockService) AddBlocks(ctx context.Context, bs []blocks.Block) error {
ctx, span := internal.StartSpan(ctx, "blockService.AddBlocks")
defer span.End()

// hash security
for _, b := range bs {
err := verifcid.ValidateCid(b.Cid())
Expand Down Expand Up @@ -203,7 +213,8 @@ func (s *blockService) AddBlocks(ctx context.Context, bs []blocks.Block) error {
// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
func (s *blockService) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
logger.Debugf("BlockService GetBlock: '%s'", c)
ctx, span := internal.StartSpan(ctx, "blockService.GetBlock", trace.WithAttributes(attribute.Stringer("CID", c)))
defer span.End()

var f func() exchange.Fetcher
if s.exchange != nil {
Expand Down Expand Up @@ -250,6 +261,9 @@ func getBlock(ctx context.Context, c cid.Cid, bs blockstore.Blockstore, fget fun
// the returned channel.
// NB: No guarantees are made about order.
func (s *blockService) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block {
ctx, span := internal.StartSpan(ctx, "blockService.GetBlocks")
defer span.End()

var f func() exchange.Fetcher
if s.exchange != nil {
f = s.getExchange
Expand Down Expand Up @@ -324,6 +338,9 @@ func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, fget

// DeleteBlock deletes a block in the blockservice from the datastore
func (s *blockService) DeleteBlock(ctx context.Context, c cid.Cid) error {
ctx, span := internal.StartSpan(ctx, "blockService.DeleteBlock", trace.WithAttributes(attribute.Stringer("CID", c)))
defer span.End()

err := s.blockstore.DeleteBlock(ctx, c)
if err == nil {
logger.Debugf("BlockService.BlockDeleted %s", c)
Expand Down Expand Up @@ -357,6 +374,9 @@ func (s *Session) getSession() exchange.Fetcher {

// GetBlock gets a block in the context of a request session
func (s *Session) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
ctx, span := internal.StartSpan(ctx, "Session.GetBlock", trace.WithAttributes(attribute.Stringer("CID", c)))
defer span.End()

var f func() exchange.Fetcher
if s.sessEx != nil {
f = s.getSession
Expand All @@ -366,6 +386,9 @@ func (s *Session) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error)

// GetBlocks gets blocks in the context of a request session
func (s *Session) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block {
ctx, span := internal.StartSpan(ctx, "Session.GetBlocks")
defer span.End()

var f func() exchange.Fetcher
if s.sessEx != nil {
f = s.getSession
Expand Down
13 changes: 13 additions & 0 deletions blockservice/internal/tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package internal

import (
"context"
"fmt"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)

func StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return otel.Tracer("go-blockservice").Start(ctx, fmt.Sprintf("Blockservice.%s", name), opts...)
}

0 comments on commit 2ad1dc9

Please sign in to comment.