Skip to content

Commit

Permalink
Set internal version once on init, to avoid data race (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored Jul 7, 2023
1 parent ad2c25c commit 2e4891a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
30 changes: 13 additions & 17 deletions traces/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,34 @@ import (

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

var internalVersion string
var (
internalVersion string // provides the instrumentation version for attribute `otel.scope.version`
tracerProvider trace.TracerProvider
)

// osqueryGoVersion returns the version of osquery-go, to be used to set the instrumentation
// version `otel.scope.version`. It looks through build info to determine the current version
// of the osquery-go package. Once determined, it saves the version to avoid performing this
// work again. If the version cannot be determined, the version defaults to the current version,
// which is hardcoded.
func osqueryGoVersion() string {
if internalVersion != "" {
return internalVersion
}
// init sets `internalVersion` and a default tracer provider.
func init() {
// By default, use the global tracer provider, which is a no-op provider.
tracerProvider = otel.GetTracerProvider()

// Look through build info to determine the current version of the osquery-go package.
if info, ok := debug.ReadBuildInfo(); ok {
for _, dep := range info.Deps {
if dep == nil {
continue
}
if dep.Path == instrumentationPkg {
internalVersion = dep.Version
return internalVersion
return
}
}
}

// Couldn't get the version from runtime build info -- save and return 0.0.0,
// Couldn't get the version from runtime build info -- save 0.0.0,
// which is the current osquery-go version.
internalVersion = "0.0.0"
return internalVersion
}

// By default, use the global tracer provider, which is a no-op provider.
var tracerProvider = otel.GetTracerProvider()

// SetTracerProvider allows consuming libraries to set a custom/non-global tracer provider.
func SetTracerProvider(tp trace.TracerProvider) {
tracerProvider = tp
Expand All @@ -56,7 +52,7 @@ func SetTracerProvider(tp trace.TracerProvider) {
// not supported by `StartSpan` below -- i.e., any `SpanStartOption` besides
// `WithAttributes`.
func OsqueryGoTracer() trace.Tracer {
return tracerProvider.Tracer(instrumentationPkg, trace.WithInstrumentationVersion(osqueryGoVersion()))
return tracerProvider.Tracer(instrumentationPkg, trace.WithInstrumentationVersion(internalVersion))
}

// StartSpan is a wrapper around trace.Tracer.Start that simplifies passing in span attributes.
Expand Down
27 changes: 27 additions & 0 deletions traces/traces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package traces

import (
"context"
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTraceInit(t *testing.T) {
t.Parallel()

// Start several spans in quick succession to confirm there's no data race on setting `internalVersion`
var wg sync.WaitGroup
for i := 0; i < 5; i += 1 {
wg.Add(1)
go func() {
defer wg.Done()
_, span := StartSpan(context.TODO(), "TestSpan")
span.End()
}()
}

wg.Wait()
assert.NotEmpty(t, internalVersion, "internal version should have been set")
}

0 comments on commit 2e4891a

Please sign in to comment.