diff --git a/cmd/splunk-extension-wrapper/splunk-extension-wrapper.go b/cmd/splunk-extension-wrapper/splunk-extension-wrapper.go index aa1ffb1..1d52336 100644 --- a/cmd/splunk-extension-wrapper/splunk-extension-wrapper.go +++ b/cmd/splunk-extension-wrapper/splunk-extension-wrapper.go @@ -33,16 +33,32 @@ import ( // the correct value is set by the go linker (it's done during build using "ldflags") var gitVersion string +const enabledKey = "SPLUNK_EXTENSION_WRAPPER_ENABLED" +const extensionNameKey = "SPLUNK_EXTENSION_WRAPPER_NAME" + +func enabled() bool { + s := strings.ToLower(os.Getenv(enabledKey)) + return s != "0" && s != "false" +} + func main() { + enabled := enabled() + configuration := config.New() initLogging(&configuration) ossignal.Watch() - m := metrics.New() + // When we are running "disabled", don't actually try to emit metrics. + // A cleaner design for this would use an interface with a stub implementation, + // but 3 or 4 nil checks will do for now, since they're all in one file + var m *metrics.MetricEmitter = nil + if enabled { + m = metrics.New() + } - shutdownCondition := registerApiAndStartMainLoop(m, &configuration) + shutdownCondition := registerApiAndStartMainLoop(enabled, m, &configuration) if shutdownCondition.IsError() { log.SetOutput(os.Stderr) @@ -51,10 +67,12 @@ func main() { log.Println("shutdown reason:", shutdownCondition.Reason()) log.Println("shutdown message:", shutdownCondition.Message()) - m.Shutdown(shutdownCondition) + if m != nil { + m.Shutdown(shutdownCondition) + } } -func registerApiAndStartMainLoop(m *metrics.MetricEmitter, configuration *config.Configuration) (sc shutdown.Condition) { +func registerApiAndStartMainLoop(enabled bool, m *metrics.MetricEmitter, configuration *config.Configuration) (sc shutdown.Condition) { var api *extensionapi.RegisteredApi defer func() { @@ -67,7 +85,7 @@ func registerApiAndStartMainLoop(m *metrics.MetricEmitter, configuration *config } }() - api, sc = extensionapi.Register(extensionName(), configuration) + api, sc = extensionapi.Register(enabled, extensionName(), configuration) if sc == nil { sc = mainLoop(api, m, configuration) @@ -81,13 +99,17 @@ func registerApiAndStartMainLoop(m *metrics.MetricEmitter, configuration *config } func mainLoop(api *extensionapi.RegisteredApi, m *metrics.MetricEmitter, configuration *config.Configuration) (sc shutdown.Condition) { - m.SetFunction(api.FunctionName, api.FunctionVersion) + if m != nil { + m.SetFunction(api.FunctionName, api.FunctionVersion) + } var event *extensionapi.Event event, sc = api.NextEvent() for sc == nil { - sc = m.Invoked(event.InvokedFunctionArn, configuration.SplunkFailFast) + if m != nil { + sc = m.Invoked(event.InvokedFunctionArn, configuration.SplunkFailFast) + } if sc == nil { event, sc = api.NextEvent() } @@ -121,5 +143,9 @@ func initLogging(configuration *config.Configuration) { } func extensionName() string { - return path.Base(os.Args[0]) + name := os.Getenv(extensionNameKey) + if name == "" { + name = path.Base(os.Args[0]) + } + return name } diff --git a/internal/extensionapi/extensionapi.go b/internal/extensionapi/extensionapi.go index 450182c..80dc40a 100644 --- a/internal/extensionapi/extensionapi.go +++ b/internal/extensionapi/extensionapi.go @@ -53,11 +53,17 @@ type RegisteredApi struct { registerResponse } -func Register(name string, configuration *config.Configuration) (*RegisteredApi, shutdown.Condition) { - log.Println("Registering...") +func Register(enabled bool, name string, configuration *config.Configuration) (*RegisteredApi, shutdown.Condition) { + log.Println("Registering... " + name) + // extensions have to at least call Register and Next; they can't actually be "disabled" + // so if we are not enabled, at least subscribe to SHUTDOWN + events := []string{ shutdownType } + if enabled { + events = []string{ invokeType, shutdownType } + } rb, err := json.Marshal(map[string][]string{ - "events": {invokeType, shutdownType}}) + "events": events}) if err != nil { return nil, shutdown.Api(fmt.Sprintf("can't marshall body: %v", err))