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

Commit

Permalink
feat: trace context traceparent header support
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias authored and lidel committed Apr 11, 2023
1 parent d87c915 commit 445f487
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 42 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ Caveats:
- Learn more at [Project Rhea (decentralized IPFS gateway)](https://pl-strflt.notion.site/Project-Rhea-decentralized-IPFS-gateway-3d5906e7a0d84bea800d5920005dfea6)
- Functional gaps facilitated by temporary delegation to legacy Kubo RPC (`/api/v0`) at `https://node[0-3].delegate.ipfs.io` infra (already used by js-ipfs).

### How to use tracing?

For tracing configuration, please check: https://github.com/ipfs/boxo/blob/main/docs/tracing.md - this includes
how to generate a `traceparent` header in order to be able to easily identify specific requests.

## Contributing

Expand Down
5 changes: 3 additions & 2 deletions blockstore_caboose.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/filecoin-saturn/caboose"
blockstore "github.com/ipfs/boxo/blockstore"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

const (
Expand Down Expand Up @@ -61,7 +62,7 @@ func newCabooseBlockStore(orchestrator, loggingEndpoint string, cdns *cachedDNS)

saturnRetrievalClient := &http.Client{
Timeout: caboose.DefaultSaturnCarRequestTimeout,
Transport: &customTransport{
Transport: otelhttp.NewTransport(&customTransport{
RoundTripper: &http.Transport{
// Increasing concurrency defaults from http.DefaultTransport
MaxIdleConns: 1000,
Expand All @@ -84,7 +85,7 @@ func newCabooseBlockStore(orchestrator, loggingEndpoint string, cdns *cachedDNS)
// ServerName: "strn.pl",
},
},
},
}),
}

return caboose.NewCaboose(&caboose.Config{
Expand Down
34 changes: 13 additions & 21 deletions blockstore_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"log"
"math/rand"
"net/http"
"net/url"
"time"

"github.com/ipfs/bifrost-gateway/lib"
blockstore "github.com/ipfs/boxo/blockstore"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

// Blockstore backed by a verifiable gateway. This is vendor-agnostic proxy interface,
Expand All @@ -37,18 +37,14 @@ type proxyBlockStore struct {
}

func (ps *proxyBlockStore) Fetch(ctx context.Context, path string, cb lib.DataCallback) error {
u, err := url.Parse(fmt.Sprintf("%s%s", ps.getRandomGatewayURL(), path))
urlStr := fmt.Sprintf("%s%s", ps.getRandomGatewayURL(), path)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return err
}
goLog.Debugw("car fetch", "url", u)
resp, err := ps.httpClient.Do(&http.Request{
Method: http.MethodGet,
URL: u,
Header: http.Header{
"Accept": []string{"application/vnd.ipld.car"},
},
})
goLog.Debugw("car fetch", "url", req.URL)
req.Header.Set("Accept", "application/vnd.ipld.car")
resp, err := ps.httpClient.Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -79,7 +75,7 @@ func newProxyBlockStore(gatewayURL []string, cdns *cachedDNS) blockstore.Blockst
gatewayURL: gatewayURL,
httpClient: &http.Client{
Timeout: GetBlockTimeout,
Transport: &customTransport{
Transport: otelhttp.NewTransport(&customTransport{
// Roundtripper with increased defaults than http.Transport such that retrieving
// multiple blocks from a single gateway concurrently is fast.
RoundTripper: &http.Transport{
Expand All @@ -89,7 +85,7 @@ func newProxyBlockStore(gatewayURL []string, cdns *cachedDNS) blockstore.Blockst
IdleConnTimeout: 90 * time.Second,
DialContext: cdns.dialWithCachedDNS,
},
},
}),
},
// Enables block validation by default. Important since we are
// proxying block requests to an untrusted gateway.
Expand All @@ -99,18 +95,14 @@ func newProxyBlockStore(gatewayURL []string, cdns *cachedDNS) blockstore.Blockst
}

func (ps *proxyBlockStore) fetch(ctx context.Context, c cid.Cid) (blocks.Block, error) {
u, err := url.Parse(fmt.Sprintf("%s/ipfs/%s?format=raw", ps.getRandomGatewayURL(), c))
urlStr := fmt.Sprintf("%s/ipfs/%s?format=raw", ps.getRandomGatewayURL(), c)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return nil, err
}
goLog.Debugw("raw fetch", "url", u)
resp, err := ps.httpClient.Do(&http.Request{
Method: http.MethodGet,
URL: u,
Header: http.Header{
"Accept": []string{"application/vnd.ipld.raw"},
},
})
goLog.Debugw("raw fetch", "url", req.URL)
req.Header.Set("Accept", "application/vnd.ipld.raw")
resp, err := ps.httpClient.Do(req)
if err != nil {
return nil, err
}
Expand Down
29 changes: 25 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/filecoin-saturn/caboose v0.0.0-20230407094233-65275d010784
github.com/gogo/protobuf v1.3.2
github.com/hashicorp/golang-lru/v2 v2.0.1
github.com/ipfs/boxo v0.8.1-0.20230406132331-999d939e84a0
github.com/ipfs/boxo v0.8.1-0.20230411232920-5d6c73c8e35e
github.com/ipfs/go-block-format v0.1.2
github.com/ipfs/go-cid v0.4.0
github.com/ipfs/go-ipld-format v0.4.0
Expand All @@ -25,6 +25,11 @@ require (
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417
github.com/spf13/cobra v1.6.1
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
go.opentelemetry.io/otel/trace v1.14.0
go.uber.org/atomic v1.10.0
go.uber.org/multierr v1.9.0
go.uber.org/zap v1.24.0
Expand All @@ -34,6 +39,7 @@ 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 @@ -45,7 +51,7 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/felixge/httpsnoop v1.0.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/gabriel-vasile/mimetype v1.4.1 // indirect
Expand All @@ -59,6 +65,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 @@ -113,6 +120,7 @@ require (
github.com/onsi/ginkgo/v2 v2.5.1 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20211123151946-c2389c3cb60a // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
Expand All @@ -138,8 +146,19 @@ require (
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // 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.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/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/proto/otlp v0.19.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/fx v1.18.2 // indirect
golang.org/x/crypto v0.6.0 // indirect
Expand All @@ -151,6 +170,8 @@ require (
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.5.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 445f487

Please sign in to comment.