Skip to content

Commit

Permalink
Disable config switch (#191)
Browse files Browse the repository at this point in the history
Add a config environment variable to disable the collection of metrics through this extension. Owing to the rules the lambda engine places on extensions, this isn't as simple as "just exit" - you still have to Register as an extension, and ask for at least the shutdown event. Additionally, add a config switch for "extension name" to make a complicated workaround for disabling the opentelemetry collector possible.

Co-authored-by: Pablo Collins <[email protected]>
  • Loading branch information
johnbley and pmcollins authored Sep 22, 2023
1 parent f78db8b commit fee0cd3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
42 changes: 34 additions & 8 deletions cmd/splunk-extension-wrapper/splunk-extension-wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
}
12 changes: 9 additions & 3 deletions internal/extensionapi/extensionapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit fee0cd3

Please sign in to comment.