@@ -14,38 +14,34 @@ import (
14
14
15
15
const instrumentationPkg = "github.com/osquery/osquery-go"
16
16
17
- var internalVersion string
17
+ var (
18
+ internalVersion string // provides the instrumentation version for attribute `otel.scope.version`
19
+ tracerProvider trace.TracerProvider
20
+ )
18
21
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.
28
28
if info , ok := debug .ReadBuildInfo (); ok {
29
29
for _ , dep := range info .Deps {
30
30
if dep == nil {
31
31
continue
32
32
}
33
33
if dep .Path == instrumentationPkg {
34
34
internalVersion = dep .Version
35
- return internalVersion
35
+ return
36
36
}
37
37
}
38
38
}
39
39
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,
41
41
// which is the current osquery-go version.
42
42
internalVersion = "0.0.0"
43
- return internalVersion
44
43
}
45
44
46
- // By default, use the global tracer provider, which is a no-op provider.
47
- var tracerProvider = otel .GetTracerProvider ()
48
-
49
45
// SetTracerProvider allows consuming libraries to set a custom/non-global tracer provider.
50
46
func SetTracerProvider (tp trace.TracerProvider ) {
51
47
tracerProvider = tp
@@ -56,7 +52,7 @@ func SetTracerProvider(tp trace.TracerProvider) {
56
52
// not supported by `StartSpan` below -- i.e., any `SpanStartOption` besides
57
53
// `WithAttributes`.
58
54
func OsqueryGoTracer () trace.Tracer {
59
- return tracerProvider .Tracer (instrumentationPkg , trace .WithInstrumentationVersion (osqueryGoVersion () ))
55
+ return tracerProvider .Tracer (instrumentationPkg , trace .WithInstrumentationVersion (internalVersion ))
60
56
}
61
57
62
58
// StartSpan is a wrapper around trace.Tracer.Start that simplifies passing in span attributes.
0 commit comments