Skip to content

Commit a684c84

Browse files
committed
Use auto-instrumentation SDK in global tracing
When the auto-instrumentation attaches to a process using the global TracerProvider, and there has not been a delegate set, create a span from the go.opentelemetry.io/auto/sdk package so the OTel Go auto-instrumentation can instrument the application by default.
1 parent 664a075 commit a684c84

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
module go.opentelemetry.io/otel
22

3-
go 1.22
3+
go 1.22.0
44

55
require (
66
github.com/go-logr/logr v1.4.2
77
github.com/go-logr/stdr v1.2.2
88
github.com/google/go-cmp v0.6.0
99
github.com/stretchr/testify v1.9.0
10+
go.opentelemetry.io/auto/sdk v0.1.0-alpha
1011
go.opentelemetry.io/otel/metric v1.31.0
1112
go.opentelemetry.io/otel/trace v1.31.0
1213
)
1314

1415
require (
1516
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/kr/text v0.2.0 // indirect
1618
github.com/pmezard/go-difflib v1.0.0 // indirect
1719
gopkg.in/yaml.v3 v3.0.1 // indirect
1820
)

go.sum

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
12
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
23
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
@@ -7,11 +8,20 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
78
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
89
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
910
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
12+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
13+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
14+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1015
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1116
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
18+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
1219
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1320
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
14-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
21+
go.opentelemetry.io/auto/sdk v0.1.0-alpha h1:4Ys9FFRxjOu0QRtAf4OHUHklBdbQ7mxWWZNBxF8YLI4=
22+
go.opentelemetry.io/auto/sdk v0.1.0-alpha/go.mod h1:yPESko1QR5kd8fkS+cMrHdxVj08l+RWtnuuBgvT1H7Q=
1523
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
24+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
25+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
1626
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1727
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/global/trace.go

+25
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"sync"
2626
"sync/atomic"
2727

28+
"go.opentelemetry.io/auto/sdk"
2829
"go.opentelemetry.io/otel/attribute"
2930
"go.opentelemetry.io/otel/codes"
3031
"go.opentelemetry.io/otel/trace"
@@ -139,6 +140,30 @@ func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanStart
139140
return delegate.(trace.Tracer).Start(ctx, name, opts...)
140141
}
141142

143+
return t.newSpan(ctx, autoInstEnabled, name, opts)
144+
}
145+
146+
// autoInstEnabled determines if the auto-instrumentation SDK span is returned
147+
// from the tracer when not backed by a delegate and auto-instrumentation has
148+
// attached to this process.
149+
//
150+
// The auto-instrumentation is expected to overwrite this value to true when it
151+
// attaches. By default, this will point to false and mean a tracer will return
152+
// a nonRecordingSpan by default.
153+
var autoInstEnabled *bool = new(bool)
154+
155+
func (t *tracer) newSpan(ctx context.Context, autoSpan *bool, name string, opts []trace.SpanStartOption) (context.Context, trace.Span) {
156+
// autoInstEnabled is passed to newSpan via the autoSpan parameter. This is
157+
// so the auto-instrumentation can define a uprobe for (*t).newSpan and be
158+
// provided with the address of the bool autoInstEnabled points to. It
159+
// needs to be a parameter so that pointer can be reliably determined, it
160+
// should not be read from the global.
161+
162+
if *autoSpan {
163+
tracer := sdk.GetTracerProvider().Tracer(t.name, t.opts...)
164+
return tracer.Start(ctx, name, opts...)
165+
}
166+
142167
s := nonRecordingSpan{sc: trace.SpanContextFromContext(ctx), tracer: t}
143168
ctx = trace.ContextWithSpan(ctx, s)
144169
return ctx, s

0 commit comments

Comments
 (0)