Skip to content

Commit

Permalink
Add env var equivalent to the WithGlobal InstrumentationOption (open-…
Browse files Browse the repository at this point in the history
…telemetry#849)

* Add env var equivalent to the WithGlobal InstrumentationOption

* Changelog
  • Loading branch information
RonFed authored May 23, 2024
1 parent 6968f4f commit f9ad92d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
- Support `google.golang.org/grpc` `1.65.0-dev`. ([#827](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/827))
- Support `google.golang.org/grpc` `1.64.0`. ([#843](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/843))
- `WithLoadedIndicator` `InstrumentationOption` to configure an Instrumentation to notify the caller once all the eBPF probes are loaded. ([#848](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/848))
- Add env var equivalent to the WithGlobal InstrumentationOption. ([#849](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/849))
- Support `go.opentelemetry.io/otel@v1.27.0`. ([#850](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/850))

### Fixed
Expand Down
17 changes: 16 additions & 1 deletion instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -54,6 +55,9 @@ const (
// envTracesExportersKey is the key for the environment variable value
// containing what OpenTelemetry trace exporter to use.
envTracesExportersKey = "OTEL_TRACES_EXPORTER"
// envOtelGlobalImplKey is the key for the environment variable value enabling to opt-in for the
// OpenTelemetry global implementation. It should be a boolean value.
envOtelGlobalImplKey = "OTEL_GO_AUTO_GLOBAL"
)

// Instrumentation manages and controls all OpenTelemetry Go
Expand Down Expand Up @@ -342,9 +346,10 @@ var lookupEnv = os.LookupEnv
// - OTEL_GO_AUTO_TARGET_EXE: sets the target binary
// - OTEL_SERVICE_NAME (or OTEL_RESOURCE_ATTRIBUTES): sets the service name
// - OTEL_TRACES_EXPORTER: sets the trace exporter
// - OTEL_GO_AUTO_GLOBAL: enables the OpenTelemetry global implementation
//
// This option may conflict with [WithTarget], [WithPID], [WithTraceExporter],
// and [WithServiceName] if their respective environment variable is defined.
// [WithServiceName] and [WithGlobal] if their respective environment variable is defined.
// If more than one of these options are used, the last one provided to an
// [Instrumentation] will be used.
//
Expand Down Expand Up @@ -372,6 +377,12 @@ func WithEnv() InstrumentationOption {
c.serviceName = name
c.additionalResAttrs = append(c.additionalResAttrs, attrs...)
}
if val, ok := lookupEnv(envOtelGlobalImplKey); ok {
boolVal, err := strconv.ParseBool(val)
if err == nil {
c.globalImpl = boolVal
}
}
return c, err
})
}
Expand Down Expand Up @@ -448,6 +459,10 @@ func WithSampler(sampler trace.Sampler) InstrumentationOption {
// The OpenTelemetry default global implementation is left unchanged (i.e. it
// remains a no-op implementation) if this options is not used.
//
// If OTEL_GO_AUTO_GLOBAL is defined, this option will conflict with
// [WithEnv]. If both are used, the last one provided to an [Instrumentation]
// will be used.
//
// [OpenTelemetry default global implementation]: https://pkg.go.dev/go.opentelemetry.io/otel
func WithGlobal() InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
Expand Down

0 comments on commit f9ad92d

Please sign in to comment.