Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Overlay #1

Merged
merged 5 commits into from
Sep 7, 2020
Merged

Add Overlay #1

merged 5 commits into from
Sep 7, 2020

Conversation

efritz
Copy link
Member

@efritz efritz commented Sep 1, 2020

This PR introduced the Overlay function, which allows a service container to be wrapped in a set of additional, immutable services. This works with both Get and Inject functions, allowing a service to be temporarily replaced in the base container.

This will be useful when specific loggers or context-specific services should be injected in all downstream handlers for a certain process, task, or request.

@sonarcloud
Copy link

sonarcloud bot commented Sep 1, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@aphistic
Copy link
Contributor

aphistic commented Sep 1, 2020

I like the idea, it can be used for more than just the logging I'd initially mentioned! How would you see this being used inside a bootstrapper method, such as https://github.com/aphistic/softcopy/blob/master/cmd/softcopy-server/main.go#L27 ? Would it become an additional nacelle.WithInitializerServiceContainer or nacelle.WithProcessServiceContainer? If I wanted to add additional fields to the logger configured by nacelle as the default, how would I do that? The ServiceContainer isn't initialized by the time the Bootstrapper function is called, is it? If so, would I just get the logger from the existing container and then add that as an overlay to the initializer/process?

This is what I'm imagining it would look like, is it right?

func (runner nacelle.ProcessContainer, container nacelle.ServiceContainer) error {
	// This or some nicer method to move this out of the bootstrapper
	rawLogger, err := container.Get("logger")
	if err != nil {
		return err
	}
	logger, ok := rawLogger.(nacelle.Logger)
	if !ok {
		return fmt.Errorf("logger isn't a nacelle logger")
	}

	runner.RegisterInitializer(
		api.NewInitializer(),
		nacelle.WithInitializerName("api"),
		nacelle.WithInitializerOverlay(nacelle.Overlay(
			"logger": logger.WithFields(
				"service": "api",
			),
		),)
	)
	
	runner.RegisterProcess(
		apiserver.NewProcess(),
		nacelle.WithProcessName("apiserver"),
		nacelle.WithProcessOverlay(nacelle.Overlay(
			"logger": logger.WithFields(
				"service": "apiserver",
			),
		),)
	)
}

@efritz
Copy link
Member Author

efritz commented Sep 1, 2020

My idea was to have the go-nacelle/process create a shim service and logger instance with any additional fields you've applied to an initializer or process. So really the setup methods would be something like:

func (runner nacelle.ProcessContainer, container nacelle.ServiceContainer) error {
	runner.RegisterInitializer(
		api.NewInitializer(),
		nacelle.WithInitializerName("api"),
		nacelle.WithLogFields(map[string]string{
			"service": "api",
		}),
	)
	
	runner.RegisterProcess(
		apiserver.NewProcess(),
		nacelle.WithProcessName("apiserver"),
		nacelle.WithLogFields(map[string]string{
			"service": "apiserver",
		}),
	)
}

Names subject to change, of course!

@efritz
Copy link
Member Author

efritz commented Sep 1, 2020

And if you have an initializer/process name, this could be done automatically (but would still allow for additional fields to be applied).

@aphistic
Copy link
Contributor

aphistic commented Sep 1, 2020

Ahh, gotcha. I didn't notice this was in the service repo at first!

@efritz efritz merged commit 30846cd into master Sep 7, 2020
@efritz efritz deleted the overlay-services branch September 7, 2020 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants