Skip to content

Commit

Permalink
feat: add tracing sub-package based on Kubo
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 11, 2023
1 parent 9f4acdf commit 0ca7e90
Show file tree
Hide file tree
Showing 15 changed files with 605 additions and 80 deletions.
9 changes: 6 additions & 3 deletions examples/gateway/car/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ import (
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/gateway"
carblockstore "github.com/ipfs/boxo/ipld/car/v2/blockstore"
"github.com/ipfs/boxo/tracing"
"github.com/ipfs/go-cid"
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

carFilePtr := flag.String("c", "", "path to CAR file to back this gateway from")
port := flag.Int("p", 8040, "port to run this gateway from")
flag.Parse()

// Setups up tracing. This is optional and only required if the implementer
// wants to be able to enable tracing.
tp, err := common.SetupTracing("CAR Gateway Example")
shutdown, err := tracing.SetupDefault(ctx, "CAR Gateway Example")
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
defer (func() { _ = tp.Shutdown(ctx) })()
defer (func() { _ = shutdown(ctx) })()

// Sets up a block service based on the CAR file.
blockService, roots, f, err := newBlockServiceFromCAR(*carFilePtr)
Expand Down
59 changes: 0 additions & 59 deletions examples/gateway/common/tracing.go

This file was deleted.

6 changes: 4 additions & 2 deletions examples/gateway/proxy/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"io"
"net/http"

"github.com/ipfs/boxo/examples/gateway/common"
"github.com/ipfs/boxo/exchange"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

type proxyExchange struct {
Expand All @@ -19,7 +19,9 @@ type proxyExchange struct {

func newProxyExchange(gatewayURL string, client *http.Client) exchange.Interface {
if client == nil {
client = common.NewClient()
client = &http.Client{
Transport: otelhttp.NewTransport(http.DefaultTransport),
}
}

return &proxyExchange{
Expand Down
9 changes: 6 additions & 3 deletions examples/gateway/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@ import (
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/examples/gateway/common"
"github.com/ipfs/boxo/gateway"
"github.com/ipfs/boxo/tracing"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

gatewayUrlPtr := flag.String("g", "", "gateway to proxy to")
port := flag.Int("p", 8040, "port to run this gateway from")
flag.Parse()

// Setups up tracing. This is optional and only required if the implementer
// wants to be able to enable tracing.
tp, err := common.SetupTracing("Proxy Gateway Example")
shutdown, err := tracing.SetupDefault(ctx, "CAR Gateway Example")
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
defer (func() { _ = tp.Shutdown(ctx) })()
defer (func() { _ = shutdown(ctx) })()

// Sets up a blockstore to hold the blocks we request from the gateway
// Note: in a production environment you would likely want to choose a more efficient datastore implementation
Expand Down
13 changes: 9 additions & 4 deletions examples/gateway/proxy/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/examples/gateway/common"
"github.com/ipfs/boxo/gateway"
"github.com/ipfs/boxo/tracing"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
Expand Down Expand Up @@ -94,11 +96,14 @@ func TestTraceContext(t *testing.T) {
traceFlags = "00"
)

// Creating a trace provider and registering it will make OTel enable tracing.
tp, err := common.SetupTracing("Proxy Test")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

os.Setenv("OTEL_TRACES_EXPORTER", "stdout")

shutdown, err := tracing.SetupDefault(ctx, "Proxy Test")
assert.Nil(t, err)
ctx := context.Background()
t.Cleanup(func() { _ = tp.Shutdown(ctx) })
defer (func() { _ = shutdown(ctx) })()

t.Run("Re-use Traceparent Trace ID Of Initial Request", func(t *testing.T) {
rs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
6 changes: 4 additions & 2 deletions examples/gateway/proxy/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"strings"

"github.com/gogo/protobuf/proto"
"github.com/ipfs/boxo/examples/gateway/common"
"github.com/ipfs/boxo/ipns"
ipns_pb "github.com/ipfs/boxo/ipns/pb"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

type proxyRouting struct {
Expand All @@ -23,7 +23,9 @@ type proxyRouting struct {

func newProxyRouting(gatewayURL string, client *http.Client) routing.ValueStore {
if client == nil {
client = common.NewClient()
client = &http.Client{
Transport: otelhttp.NewTransport(http.DefaultTransport),
}
}

return &proxyRouting{
Expand Down
19 changes: 16 additions & 3 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ require (
github.com/prometheus/client_golang v1.14.0
github.com/stretchr/testify v1.8.2
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.40.0
go.opentelemetry.io/contrib/propagators/autoprop v0.40.0
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/sdk v1.14.0
)

require (
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
Expand All @@ -50,6 +48,7 @@ require (
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20221203041831-ce31453925ec // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
Expand Down Expand Up @@ -108,6 +107,7 @@ require (
github.com/onsi/ginkgo/v2 v2.5.1 // indirect
github.com/opencontainers/runtime-spec v1.0.2 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -130,12 +130,23 @@ require (
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/propagators/autoprop v0.40.0 // indirect
go.opentelemetry.io/contrib/propagators/aws v1.15.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.15.0 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.15.0 // indirect
go.opentelemetry.io/contrib/propagators/ot v1.15.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/zipkin v1.14.0 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/fx v1.18.2 // indirect
Expand All @@ -150,6 +161,8 @@ require (
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
Expand Down
Loading

0 comments on commit 0ca7e90

Please sign in to comment.