Skip to content

Commit c2ae3fc

Browse files
committed
feat: add tracing sub-package based on Kubo
1 parent 9f4acdf commit c2ae3fc

15 files changed

Lines changed: 605 additions & 80 deletions

File tree

examples/gateway/car/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,25 @@ import (
1414
offline "github.com/ipfs/boxo/exchange/offline"
1515
"github.com/ipfs/boxo/gateway"
1616
carblockstore "github.com/ipfs/boxo/ipld/car/v2/blockstore"
17+
"github.com/ipfs/boxo/tracing"
1718
"github.com/ipfs/go-cid"
1819
)
1920

2021
func main() {
22+
ctx, cancel := context.WithCancel(context.Background())
23+
defer cancel()
24+
2125
carFilePtr := flag.String("c", "", "path to CAR file to back this gateway from")
2226
port := flag.Int("p", 8040, "port to run this gateway from")
2327
flag.Parse()
2428

2529
// Setups up tracing. This is optional and only required if the implementer
2630
// wants to be able to enable tracing.
27-
tp, err := common.SetupTracing("CAR Gateway Example")
31+
shutdown, err := tracing.SetupDefault(ctx, "CAR Gateway Example")
2832
if err != nil {
2933
log.Fatal(err)
3034
}
31-
ctx := context.Background()
32-
defer (func() { _ = tp.Shutdown(ctx) })()
35+
defer (func() { _ = shutdown(ctx) })()
3336

3437
// Sets up a block service based on the CAR file.
3538
blockService, roots, f, err := newBlockServiceFromCAR(*carFilePtr)

examples/gateway/common/tracing.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

examples/gateway/proxy/blockstore.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"io"
77
"net/http"
88

9-
"github.com/ipfs/boxo/examples/gateway/common"
109
"github.com/ipfs/boxo/exchange"
1110
blocks "github.com/ipfs/go-block-format"
1211
"github.com/ipfs/go-cid"
12+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1313
)
1414

1515
type proxyExchange struct {
@@ -19,7 +19,9 @@ type proxyExchange struct {
1919

2020
func newProxyExchange(gatewayURL string, client *http.Client) exchange.Interface {
2121
if client == nil {
22-
client = common.NewClient()
22+
client = &http.Client{
23+
Transport: otelhttp.NewTransport(http.DefaultTransport),
24+
}
2325
}
2426

2527
return &proxyExchange{

examples/gateway/proxy/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,26 @@ import (
1111
"github.com/ipfs/boxo/blockstore"
1212
"github.com/ipfs/boxo/examples/gateway/common"
1313
"github.com/ipfs/boxo/gateway"
14+
"github.com/ipfs/boxo/tracing"
1415
"github.com/ipfs/go-datastore"
1516
dssync "github.com/ipfs/go-datastore/sync"
1617
)
1718

1819
func main() {
20+
ctx, cancel := context.WithCancel(context.Background())
21+
defer cancel()
22+
1923
gatewayUrlPtr := flag.String("g", "", "gateway to proxy to")
2024
port := flag.Int("p", 8040, "port to run this gateway from")
2125
flag.Parse()
2226

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

3235
// Sets up a blockstore to hold the blocks we request from the gateway
3336
// Note: in a production environment you would likely want to choose a more efficient datastore implementation

examples/gateway/proxy/main_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"io"
77
"net/http"
88
"net/http/httptest"
9+
"os"
910
"strings"
1011
"testing"
1112

1213
"github.com/ipfs/boxo/blockservice"
1314
"github.com/ipfs/boxo/blockstore"
1415
"github.com/ipfs/boxo/examples/gateway/common"
1516
"github.com/ipfs/boxo/gateway"
17+
"github.com/ipfs/boxo/tracing"
1618
blocks "github.com/ipfs/go-block-format"
1719
"github.com/ipfs/go-datastore"
1820
dssync "github.com/ipfs/go-datastore/sync"
@@ -94,11 +96,14 @@ func TestTraceContext(t *testing.T) {
9496
traceFlags = "00"
9597
)
9698

97-
// Creating a trace provider and registering it will make OTel enable tracing.
98-
tp, err := common.SetupTracing("Proxy Test")
99+
ctx, cancel := context.WithCancel(context.Background())
100+
defer cancel()
101+
102+
os.Setenv("OTEL_TRACES_EXPORTER", "stdout")
103+
104+
shutdown, err := tracing.SetupDefault(ctx, "Proxy Test")
99105
assert.Nil(t, err)
100-
ctx := context.Background()
101-
t.Cleanup(func() { _ = tp.Shutdown(ctx) })
106+
defer (func() { _ = shutdown(ctx) })()
102107

103108
t.Run("Re-use Traceparent Trace ID Of Initial Request", func(t *testing.T) {
104109
rs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

examples/gateway/proxy/routing.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"strings"
99

1010
"github.com/gogo/protobuf/proto"
11-
"github.com/ipfs/boxo/examples/gateway/common"
1211
"github.com/ipfs/boxo/ipns"
1312
ipns_pb "github.com/ipfs/boxo/ipns/pb"
1413
ic "github.com/libp2p/go-libp2p/core/crypto"
1514
"github.com/libp2p/go-libp2p/core/peer"
1615
"github.com/libp2p/go-libp2p/core/routing"
16+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1717
)
1818

1919
type proxyRouting struct {
@@ -23,7 +23,9 @@ type proxyRouting struct {
2323

2424
func newProxyRouting(gatewayURL string, client *http.Client) routing.ValueStore {
2525
if client == nil {
26-
client = common.NewClient()
26+
client = &http.Client{
27+
Transport: otelhttp.NewTransport(http.DefaultTransport),
28+
}
2729
}
2830

2931
return &proxyRouting{

examples/go.mod

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ require (
1616
github.com/prometheus/client_golang v1.14.0
1717
github.com/stretchr/testify v1.8.2
1818
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.40.0
19-
go.opentelemetry.io/contrib/propagators/autoprop v0.40.0
20-
go.opentelemetry.io/otel v1.14.0
21-
go.opentelemetry.io/otel/sdk v1.14.0
2219
)
2320

2421
require (
2522
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
2623
github.com/benbjohnson/clock v1.3.0 // indirect
2724
github.com/beorn7/perks v1.0.1 // indirect
25+
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
2826
github.com/cespare/xxhash v1.1.0 // indirect
2927
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3028
github.com/containerd/cgroups v1.0.4 // indirect
@@ -50,6 +48,7 @@ require (
5048
github.com/google/gopacket v1.1.19 // indirect
5149
github.com/google/pprof v0.0.0-20221203041831-ce31453925ec // indirect
5250
github.com/google/uuid v1.3.0 // indirect
51+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
5352
github.com/hashicorp/errwrap v1.1.0 // indirect
5453
github.com/hashicorp/go-multierror v1.1.1 // indirect
5554
github.com/hashicorp/golang-lru v0.5.4 // indirect
@@ -108,6 +107,7 @@ require (
108107
github.com/onsi/ginkgo/v2 v2.5.1 // indirect
109108
github.com/opencontainers/runtime-spec v1.0.2 // indirect
110109
github.com/opentracing/opentracing-go v1.2.0 // indirect
110+
github.com/openzipkin/zipkin-go v0.4.1 // indirect
111111
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
112112
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
113113
github.com/pkg/errors v0.9.1 // indirect
@@ -130,12 +130,23 @@ require (
130130
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect
131131
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
132132
go.opencensus.io v0.24.0 // indirect
133+
go.opentelemetry.io/contrib/propagators/autoprop v0.40.0 // indirect
133134
go.opentelemetry.io/contrib/propagators/aws v1.15.0 // indirect
134135
go.opentelemetry.io/contrib/propagators/b3 v1.15.0 // indirect
135136
go.opentelemetry.io/contrib/propagators/jaeger v1.15.0 // indirect
136137
go.opentelemetry.io/contrib/propagators/ot v1.15.0 // indirect
138+
go.opentelemetry.io/otel v1.14.0 // indirect
139+
go.opentelemetry.io/otel/exporters/jaeger v1.14.0 // indirect
140+
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
141+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
142+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 // indirect
143+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.14.0 // indirect
144+
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.14.0 // indirect
145+
go.opentelemetry.io/otel/exporters/zipkin v1.14.0 // indirect
137146
go.opentelemetry.io/otel/metric v0.37.0 // indirect
147+
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
138148
go.opentelemetry.io/otel/trace v1.14.0 // indirect
149+
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
139150
go.uber.org/atomic v1.10.0 // indirect
140151
go.uber.org/dig v1.15.0 // indirect
141152
go.uber.org/fx v1.18.2 // indirect
@@ -150,6 +161,8 @@ require (
150161
golang.org/x/text v0.7.0 // indirect
151162
golang.org/x/tools v0.3.0 // indirect
152163
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
164+
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
165+
google.golang.org/grpc v1.53.0 // indirect
153166
google.golang.org/protobuf v1.28.1 // indirect
154167
gopkg.in/yaml.v3 v3.0.1 // indirect
155168
lukechampine.com/blake3 v1.1.7 // indirect

0 commit comments

Comments
 (0)