Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
feat: open telemetry tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 12, 2023
1 parent 65275d0 commit 660fbe0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
14 changes: 14 additions & 0 deletions caboose.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
gateway "github.com/ipfs/boxo/gateway"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

type Config struct {
Expand Down Expand Up @@ -256,10 +258,16 @@ func (c *Caboose) Close() {

// Fetch allows fetching car archives by a path of the form `/ipfs/<cid>[/path/to/file]`
func (c *Caboose) Fetch(ctx context.Context, path string, cb DataCallback) error {
ctx, span := spanTrace(ctx, "Blockstore.Fetch", trace.WithAttributes(attribute.String("path", path)))
defer span.End()

return c.pool.fetchResourceWith(ctx, path, cb, c.getAffinity(ctx))
}

func (c *Caboose) Has(ctx context.Context, it cid.Cid) (bool, error) {
ctx, span := spanTrace(ctx, "Blockstore.Has", trace.WithAttributes(attribute.Stringer("cid", it)))
defer span.End()

blk, err := c.pool.fetchBlockWith(ctx, it, c.getAffinity(ctx))
if err != nil {
return false, err
Expand All @@ -268,6 +276,9 @@ func (c *Caboose) Has(ctx context.Context, it cid.Cid) (bool, error) {
}

func (c *Caboose) Get(ctx context.Context, it cid.Cid) (blocks.Block, error) {
ctx, span := spanTrace(ctx, "Blockstore.Get", trace.WithAttributes(attribute.Stringer("cid", it)))
defer span.End()

blk, err := c.pool.fetchBlockWith(ctx, it, c.getAffinity(ctx))
if err != nil {
return nil, err
Expand All @@ -277,6 +288,9 @@ func (c *Caboose) Get(ctx context.Context, it cid.Cid) (blocks.Block, error) {

// GetSize returns the CIDs mapped BlockSize
func (c *Caboose) GetSize(ctx context.Context, it cid.Cid) (int, error) {
ctx, span := spanTrace(ctx, "Blockstore.GetSize", trace.WithAttributes(attribute.Stringer("cid", it)))
defer span.End()

blk, err := c.pool.fetchBlockWith(ctx, it, c.getAffinity(ctx))
if err != nil {
return 0, err
Expand Down
5 changes: 5 additions & 0 deletions fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"

"github.com/influxdata/tdigest"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"github.com/google/uuid"
blocks "github.com/ipfs/go-block-format"
Expand Down Expand Up @@ -82,6 +84,9 @@ func (p *pool) fetchResource(ctx context.Context, from string, resource string,
return ce
}

ctx, span := spanTrace(ctx, "Pool.FetchResource", trace.WithAttributes(attribute.String("from", from), attribute.String("of", resource), attribute.String("mime", mime)))
defer span.End()

requestId := uuid.NewString()
goLogger.Debugw("doing fetch", "from", from, "of", resource, "mime", mime, "requestId", requestId)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ require (
github.com/stretchr/testify v1.8.2
github.com/tcnksm/go-httpstat v0.2.0
github.com/urfave/cli/v2 v2.24.2
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/trace v1.14.0
)

require (
Expand Down Expand Up @@ -100,8 +102,6 @@ require (
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
Expand Down
15 changes: 15 additions & 0 deletions tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package caboose

import (
"context"
"fmt"

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

var tracer = otel.Tracer("caboose")

func spanTrace(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return tracer.Start(ctx, fmt.Sprintf("Caboose.%s", spanName), opts...)
}

0 comments on commit 660fbe0

Please sign in to comment.