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

Commit bf4b3cf

Browse files
authored
feat: add tracing (#30)
* feat: add tracing * make span names and attributes more consistent
1 parent ff68a74 commit bf4b3cf

File tree

10 files changed

+114
-3
lines changed

10 files changed

+114
-3
lines changed

base.go

+6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ func resolve(ctx context.Context, r resolver, name string, options opts.ResolveO
4040
}
4141

4242
func resolveAsync(ctx context.Context, r resolver, name string, options opts.ResolveOpts) <-chan Result {
43+
ctx, span := StartSpan(ctx, "ResolveAsync")
44+
defer span.End()
45+
4346
resCh := r.resolveOnceAsync(ctx, name, options)
4447
depth := options.Depth
4548
outCh := make(chan Result, 1)
4649

4750
go func() {
4851
defer close(outCh)
52+
ctx, span := StartSpan(ctx, "ResolveAsync.Worker")
53+
defer span.End()
54+
4955
var subCh <-chan Result
5056
var cancelSub context.CancelFunc
5157
defer func() {

dns.go

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
path "github.com/ipfs/go-path"
1212
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
1313
dns "github.com/miekg/dns"
14+
"go.opentelemetry.io/otel/attribute"
15+
"go.opentelemetry.io/otel/trace"
1416
)
1517

1618
// LookupTXTFunc is a function that lookups TXT record values.
@@ -30,11 +32,17 @@ func NewDNSResolver(lookup LookupTXTFunc) *DNSResolver {
3032

3133
// Resolve implements Resolver.
3234
func (r *DNSResolver) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) {
35+
ctx, span := StartSpan(ctx, "DNSResolver.Resolve")
36+
defer span.End()
37+
3338
return resolve(ctx, r, name, opts.ProcessOpts(options))
3439
}
3540

3641
// ResolveAsync implements Resolver.
3742
func (r *DNSResolver) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result {
43+
ctx, span := StartSpan(ctx, "DNSResolver.ResolveAsync")
44+
defer span.End()
45+
3846
return resolveAsync(ctx, r, name, opts.ProcessOpts(options))
3947
}
4048

@@ -47,6 +55,9 @@ type lookupRes struct {
4755
// TXT records for a given domain name should contain a b58
4856
// encoded multihash.
4957
func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
58+
ctx, span := StartSpan(ctx, "DNSResolver.ResolveOnceAsync")
59+
defer span.End()
60+
5061
var fqdn string
5162
out := make(chan onceResult, 1)
5263
segments := strings.SplitN(name, "/", 2)
@@ -80,6 +91,9 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
8091

8192
go func() {
8293
defer close(out)
94+
ctx, span := StartSpan(ctx, "DNSResolver.ResolveOnceAsync.Worker")
95+
defer span.End()
96+
8397
var rootResErr, subResErr error
8498
for {
8599
select {
@@ -131,6 +145,9 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
131145
}
132146

133147
func workDomain(ctx context.Context, r *DNSResolver, name string, res chan lookupRes) {
148+
ctx, span := StartSpan(ctx, "DNSResolver.WorkDomain", trace.WithAttributes(attribute.String("Name", name)))
149+
defer span.End()
150+
134151
defer close(res)
135152

136153
txt, err := r.lookupTXT(ctx, name)

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ require (
2424
github.com/multiformats/go-multiaddr-dns v0.3.1
2525
github.com/multiformats/go-multihash v0.0.15
2626
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc
27+
go.opentelemetry.io/otel v1.6.0
28+
go.opentelemetry.io/otel/trace v1.6.0
2729
)
2830

2931
go 1.16

go.sum

+13-2
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
187187
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
188188
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
189189
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
190+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
191+
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
192+
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
193+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
194+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
190195
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
191196
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
192197
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
@@ -246,8 +251,9 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
246251
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
247252
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
248253
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
249-
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
250254
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
255+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
256+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
251257
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
252258
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
253259
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -1104,8 +1110,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
11041110
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
11051111
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
11061112
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1107-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
11081113
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1114+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
1115+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11091116
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
11101117
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
11111118
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -1155,9 +1162,13 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
11551162
go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M=
11561163
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
11571164
go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo=
1165+
go.opentelemetry.io/otel v1.6.0 h1:YV6GkGe/Ag2PKsm4rjlqdSNs0w0A5ZzxeGkxhx1T+t4=
1166+
go.opentelemetry.io/otel v1.6.0/go.mod h1:bfJD2DZVw0LBxghOTlgnlI0CV3hLDu9XF/QKOUXMTQQ=
11581167
go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU=
11591168
go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw=
11601169
go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw=
1170+
go.opentelemetry.io/otel/trace v1.6.0 h1:NDzPermp9ISkhxIaJXjBTi2O60xOSHDHP/EezjOL2wo=
1171+
go.opentelemetry.io/otel/trace v1.6.0/go.mod h1:qs7BrU5cZ8dXQHBGxHMOxwME/27YH2qEp4/+tZLLwJE=
11611172
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
11621173
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
11631174
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=

namesys.go

+21
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
routing "github.com/libp2p/go-libp2p-core/routing"
3131
dns "github.com/miekg/dns"
3232
madns "github.com/multiformats/go-multiaddr-dns"
33+
"go.opentelemetry.io/otel/attribute"
34+
"go.opentelemetry.io/otel/trace"
3335
)
3436

3537
// mpns (a multi-protocol NameSystem) implements generic IPFS naming.
@@ -134,6 +136,9 @@ const DefaultResolverCacheTTL = time.Minute
134136

135137
// Resolve implements Resolver.
136138
func (ns *mpns) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) {
139+
ctx, span := StartSpan(ctx, "MPNS.Resolve", trace.WithAttributes(attribute.String("Name", name)))
140+
defer span.End()
141+
137142
if strings.HasPrefix(name, "/ipfs/") {
138143
return path.ParsePath(name)
139144
}
@@ -146,6 +151,9 @@ func (ns *mpns) Resolve(ctx context.Context, name string, options ...opts.Resolv
146151
}
147152

148153
func (ns *mpns) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result {
154+
ctx, span := StartSpan(ctx, "MPNS.ResolveAsync", trace.WithAttributes(attribute.String("Name", name)))
155+
defer span.End()
156+
149157
if strings.HasPrefix(name, "/ipfs/") {
150158
p, err := path.ParsePath(name)
151159
res := make(chan Result, 1)
@@ -167,6 +175,9 @@ func (ns *mpns) ResolveAsync(ctx context.Context, name string, options ...opts.R
167175

168176
// resolveOnce implements resolver.
169177
func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
178+
ctx, span := StartSpan(ctx, "MPNS.ResolveOnceAsync")
179+
defer span.End()
180+
170181
out := make(chan onceResult, 1)
171182

172183
if !strings.HasPrefix(name, ipnsPrefix) {
@@ -213,11 +224,14 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
213224
if len(segments) > 3 {
214225
p, err = path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
215226
}
227+
span.SetAttributes(attribute.Bool("CacheHit", true))
228+
span.RecordError(err)
216229

217230
out <- onceResult{value: p, err: err}
218231
close(out)
219232
return out
220233
}
234+
span.SetAttributes(attribute.Bool("CacheHit", false))
221235

222236
if err == nil {
223237
res = ns.ipnsResolver
@@ -273,18 +287,25 @@ func emitOnceResult(ctx context.Context, outCh chan<- onceResult, r onceResult)
273287

274288
// Publish implements Publisher
275289
func (ns *mpns) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
290+
ctx, span := StartSpan(ctx, "MPNS.Publish")
291+
defer span.End()
276292
return ns.PublishWithEOL(ctx, name, value, time.Now().Add(DefaultRecordEOL))
277293
}
278294

279295
func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.Path, eol time.Time) error {
296+
ctx, span := StartSpan(ctx, "MPNS.PublishWithEOL", trace.WithAttributes(attribute.String("Value", value.String())))
297+
defer span.End()
280298
id, err := peer.IDFromPrivateKey(name)
281299
if err != nil {
300+
span.RecordError(err)
282301
return err
283302
}
303+
span.SetAttributes(attribute.String("ID", id.String()))
284304
if err := ns.ipnsPublisher.PublishWithEOL(ctx, name, value, eol); err != nil {
285305
// Invalidate the cache. Publishing may _partially_ succeed but
286306
// still return an error.
287307
ns.cacheInvalidate(string(id))
308+
span.RecordError(err)
288309
return err
289310
}
290311
ttl := DefaultResolverCacheTTL

publisher.go

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
peer "github.com/libp2p/go-libp2p-core/peer"
1717
routing "github.com/libp2p/go-libp2p-core/routing"
1818
base32 "github.com/whyrusleeping/base32"
19+
"go.opentelemetry.io/otel/attribute"
20+
"go.opentelemetry.io/otel/trace"
1921
)
2022

2123
const ipnsPrefix = "/ipns/"
@@ -191,6 +193,9 @@ func (p *IpnsPublisher) updateRecord(ctx context.Context, k crypto.PrivKey, valu
191193
// PublishWithEOL is a temporary stand in for the ipns records implementation
192194
// see here for more details: https://github.com/ipfs/specs/tree/master/records
193195
func (p *IpnsPublisher) PublishWithEOL(ctx context.Context, k crypto.PrivKey, value path.Path, eol time.Time) error {
196+
ctx, span := StartSpan(ctx, "IpnsPublisher.PublishWithEOL", trace.WithAttributes(attribute.String("Value", value.String())))
197+
defer span.End()
198+
194199
record, err := p.updateRecord(ctx, k, value, eol)
195200
if err != nil {
196201
return err
@@ -216,6 +221,9 @@ func checkCtxTTL(ctx context.Context) (time.Duration, bool) {
216221
// keyed on the ID associated with the provided public key. The public key is
217222
// also made available to the routing system so that entries can be verified.
218223
func PutRecordToRouting(ctx context.Context, r routing.ValueStore, k crypto.PubKey, entry *pb.IpnsEntry) error {
224+
ctx, span := StartSpan(ctx, "PutRecordToRouting")
225+
defer span.End()
226+
219227
ctx, cancel := context.WithCancel(ctx)
220228
defer cancel()
221229

@@ -266,6 +274,9 @@ func waitOnErrChan(ctx context.Context, errs chan error) error {
266274
// PublishPublicKey stores the given public key in the ValueStore with the
267275
// given key.
268276
func PublishPublicKey(ctx context.Context, r routing.ValueStore, k string, pubk crypto.PubKey) error {
277+
ctx, span := StartSpan(ctx, "PublishPublicKey", trace.WithAttributes(attribute.String("Key", k)))
278+
defer span.End()
279+
269280
log.Debugf("Storing pubkey at: %s", k)
270281
pkbytes, err := crypto.MarshalPublicKey(pubk)
271282
if err != nil {
@@ -279,6 +290,9 @@ func PublishPublicKey(ctx context.Context, r routing.ValueStore, k string, pubk
279290
// PublishEntry stores the given IpnsEntry in the ValueStore with the given
280291
// ipnskey.
281292
func PublishEntry(ctx context.Context, r routing.ValueStore, ipnskey string, rec *pb.IpnsEntry) error {
293+
ctx, span := StartSpan(ctx, "PublishEntry", trace.WithAttributes(attribute.String("IPNSKey", ipnskey)))
294+
defer span.End()
295+
282296
data, err := proto.Marshal(rec)
283297
if err != nil {
284298
return err

republisher/repub.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
keystore "github.com/ipfs/go-ipfs-keystore"
1111
namesys "github.com/ipfs/go-namesys"
1212
path "github.com/ipfs/go-path"
13+
"go.opentelemetry.io/otel/attribute"
1314

1415
proto "github.com/gogo/protobuf/proto"
1516
ds "github.com/ipfs/go-datastore"
@@ -93,6 +94,8 @@ func (rp *Republisher) Run(proc goprocess.Process) {
9394
func (rp *Republisher) republishEntries(p goprocess.Process) error {
9495
ctx, cancel := context.WithCancel(gpctx.OnClosingContext(p))
9596
defer cancel()
97+
ctx, span := namesys.StartSpan(ctx, "Republisher.RepublishEntries")
98+
defer span.End()
9699

97100
// TODO: Use rp.ipns.ListPublished(). We can't currently *do* that
98101
// because:
@@ -125,8 +128,11 @@ func (rp *Republisher) republishEntries(p goprocess.Process) error {
125128
}
126129

127130
func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) error {
131+
ctx, span := namesys.StartSpan(ctx, "Republisher.RepublishEntry")
132+
defer span.End()
128133
id, err := peer.IDFromPrivateKey(priv)
129134
if err != nil {
135+
span.RecordError(err)
130136
return err
131137
}
132138

@@ -136,14 +142,17 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro
136142
e, err := rp.getLastIPNSEntry(ctx, id)
137143
if err != nil {
138144
if err == errNoEntry {
145+
span.SetAttributes(attribute.Bool("NoEntry", true))
139146
return nil
140147
}
148+
span.RecordError(err)
141149
return err
142150
}
143151

144152
p := path.Path(e.GetValue())
145153
prevEol, err := ipns.GetEOL(e)
146154
if err != nil {
155+
span.RecordError(err)
147156
return err
148157
}
149158

@@ -152,7 +161,9 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro
152161
if prevEol.After(eol) {
153162
eol = prevEol
154163
}
155-
return rp.ns.PublishWithEOL(ctx, priv, p, eol)
164+
err = rp.ns.PublishWithEOL(ctx, priv, p, eol)
165+
span.RecordError(err)
166+
return err
156167
}
157168

158169
func (rp *Republisher) getLastIPNSEntry(ctx context.Context, id peer.ID) (*pb.IpnsEntry, error) {

resolve/resolve.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strings"
88

99
"github.com/ipfs/go-path"
10+
"go.opentelemetry.io/otel/attribute"
11+
"go.opentelemetry.io/otel/trace"
1012

1113
"github.com/ipfs/go-namesys"
1214
)
@@ -18,6 +20,8 @@ var ErrNoNamesys = errors.New(
1820

1921
// ResolveIPNS resolves /ipns paths
2022
func ResolveIPNS(ctx context.Context, nsys namesys.NameSystem, p path.Path) (path.Path, error) {
23+
ctx, span := namesys.StartSpan(ctx, "ResolveIPNS", trace.WithAttributes(attribute.String("Path", p.String())))
24+
defer span.End()
2125
if strings.HasPrefix(p.String(), "/ipns/") {
2226
// TODO(cryptix): we should be able to query the local cache for the path
2327
if nsys == nil {

routing.go

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
routing "github.com/libp2p/go-libp2p-core/routing"
1717
dht "github.com/libp2p/go-libp2p-kad-dht"
1818
mh "github.com/multiformats/go-multihash"
19+
"go.opentelemetry.io/otel/attribute"
20+
"go.opentelemetry.io/otel/trace"
1921
)
2022

2123
var log = logging.Logger("namesys")
@@ -38,17 +40,24 @@ func NewIpnsResolver(route routing.ValueStore) *IpnsResolver {
3840

3941
// Resolve implements Resolver.
4042
func (r *IpnsResolver) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) {
43+
ctx, span := StartSpan(ctx, "IpnsResolver.Resolve", trace.WithAttributes(attribute.String("Name", name)))
44+
defer span.End()
4145
return resolve(ctx, r, name, opts.ProcessOpts(options))
4246
}
4347

4448
// ResolveAsync implements Resolver.
4549
func (r *IpnsResolver) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result {
50+
ctx, span := StartSpan(ctx, "IpnsResolver.ResolveAsync", trace.WithAttributes(attribute.String("Name", name)))
51+
defer span.End()
4652
return resolveAsync(ctx, r, name, opts.ProcessOpts(options))
4753
}
4854

4955
// resolveOnce implements resolver. Uses the IPFS routing system to
5056
// resolve SFS-like names.
5157
func (r *IpnsResolver) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
58+
ctx, span := StartSpan(ctx, "IpnsResolver.ResolveOnceAsync", trace.WithAttributes(attribute.String("Name", name)))
59+
defer span.End()
60+
5261
out := make(chan onceResult, 1)
5362
log.Debugf("RoutingResolver resolving %s", name)
5463
cancel := func() {}
@@ -86,6 +95,9 @@ func (r *IpnsResolver) resolveOnceAsync(ctx context.Context, name string, option
8695
go func() {
8796
defer cancel()
8897
defer close(out)
98+
ctx, span := StartSpan(ctx, "IpnsResolver.ResolveOnceAsync.Worker")
99+
defer span.End()
100+
89101
for {
90102
select {
91103
case val, ok := <-vals:

tracing.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package namesys
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"go.opentelemetry.io/otel"
8+
"go.opentelemetry.io/otel/trace"
9+
)
10+
11+
func StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
12+
return otel.Tracer("go-namesys").Start(ctx, fmt.Sprintf("Namesys.%s", name))
13+
}

0 commit comments

Comments
 (0)