Skip to content

Commit 2e4891a

Browse files
Set internal version once on init, to avoid data race (#114)
1 parent ad2c25c commit 2e4891a

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

traces/traces.go

+13-17
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,34 @@ import (
1414

1515
const instrumentationPkg = "github.com/osquery/osquery-go"
1616

17-
var internalVersion string
17+
var (
18+
internalVersion string // provides the instrumentation version for attribute `otel.scope.version`
19+
tracerProvider trace.TracerProvider
20+
)
1821

19-
// osqueryGoVersion returns the version of osquery-go, to be used to set the instrumentation
20-
// version `otel.scope.version`. It looks through build info to determine the current version
21-
// of the osquery-go package. Once determined, it saves the version to avoid performing this
22-
// work again. If the version cannot be determined, the version defaults to the current version,
23-
// which is hardcoded.
24-
func osqueryGoVersion() string {
25-
if internalVersion != "" {
26-
return internalVersion
27-
}
22+
// init sets `internalVersion` and a default tracer provider.
23+
func init() {
24+
// By default, use the global tracer provider, which is a no-op provider.
25+
tracerProvider = otel.GetTracerProvider()
26+
27+
// Look through build info to determine the current version of the osquery-go package.
2828
if info, ok := debug.ReadBuildInfo(); ok {
2929
for _, dep := range info.Deps {
3030
if dep == nil {
3131
continue
3232
}
3333
if dep.Path == instrumentationPkg {
3434
internalVersion = dep.Version
35-
return internalVersion
35+
return
3636
}
3737
}
3838
}
3939

40-
// Couldn't get the version from runtime build info -- save and return 0.0.0,
40+
// Couldn't get the version from runtime build info -- save 0.0.0,
4141
// which is the current osquery-go version.
4242
internalVersion = "0.0.0"
43-
return internalVersion
4443
}
4544

46-
// By default, use the global tracer provider, which is a no-op provider.
47-
var tracerProvider = otel.GetTracerProvider()
48-
4945
// SetTracerProvider allows consuming libraries to set a custom/non-global tracer provider.
5046
func SetTracerProvider(tp trace.TracerProvider) {
5147
tracerProvider = tp
@@ -56,7 +52,7 @@ func SetTracerProvider(tp trace.TracerProvider) {
5652
// not supported by `StartSpan` below -- i.e., any `SpanStartOption` besides
5753
// `WithAttributes`.
5854
func OsqueryGoTracer() trace.Tracer {
59-
return tracerProvider.Tracer(instrumentationPkg, trace.WithInstrumentationVersion(osqueryGoVersion()))
55+
return tracerProvider.Tracer(instrumentationPkg, trace.WithInstrumentationVersion(internalVersion))
6056
}
6157

6258
// StartSpan is a wrapper around trace.Tracer.Start that simplifies passing in span attributes.

traces/traces_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package traces
2+
3+
import (
4+
"context"
5+
"sync"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestTraceInit(t *testing.T) {
12+
t.Parallel()
13+
14+
// Start several spans in quick succession to confirm there's no data race on setting `internalVersion`
15+
var wg sync.WaitGroup
16+
for i := 0; i < 5; i += 1 {
17+
wg.Add(1)
18+
go func() {
19+
defer wg.Done()
20+
_, span := StartSpan(context.TODO(), "TestSpan")
21+
span.End()
22+
}()
23+
}
24+
25+
wg.Wait()
26+
assert.NotEmpty(t, internalVersion, "internal version should have been set")
27+
}

0 commit comments

Comments
 (0)