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

cobrautil v2 #19

Merged
merged 8 commits into from
Oct 3, 2022
Merged

cobrautil v2 #19

merged 8 commits into from
Oct 3, 2022

Conversation

vroldanbet
Copy link
Contributor

@vroldanbet vroldanbet commented Sep 26, 2022

Closes #17

Main design choices

  • separates functionality by packages: this allows clients importing just the bits they need without bringing all the transitive module dependencies
  • extensible configuration API: Using a fluent-API we can add new options without breaking API or being forced to leave a trail of deprecated methods.

Zerolog

The main problems is v1 didn't offer any API to have cobrautil consumers adjust how the logger was configured. This is important as zerolog provides a vast API with many knobs to tweak.

  • a fluent API is offered so that cobrautil/zerolog can be extended with options as the need arises.
  • v1 assumed the consumer would use the zerolog/log global instance package. This forces the consuming application to use that global instance. The WithTarget option offers a callback when the logger is instantiated, so e.g. consumers can cache the instance somewhere else. It continues to default to that global instance.
  • v1 used zerolog global level. This is an unexpected side-effect the consumer may not want, specially given cobrautil is creating an instance of a zerolog Logger. The implementation now adjusts the level on the logger instance instead.
  • v1 methods are provided to offer an easy path of migration to v2.
  • WithAsync configures the logger to be non-blocking, implemented using diode buffered-ring. This is default by default to retain v1 behaviour.

See how it's used in https://github.com/authzed/spicedb/pull/844/files#diff-df0635588ca5d6a577487faacc185c3838a3fe2b321660c36372541797c54c74R59-R67

OTel

  • removes dependency on zerolog and replaces with go-log/logr
  • WithLogger makes it possible to set the global logger used by the OTel library
  • default logging level for messages issued by this package is debug

HTTP

  • extensible HTTP configuration:
    • WithLogger allows injecting a custom go-log/logr value
    • WithHandler allows injecting a http.Handler into the http.Server to be created
    • default logging level for messages issued by this package is debug instead of info. Can be tweaked with WithPreRunLevel
    • warn level logging for non-https configuration moves to preRunLevel. Insecure http is used behind load-balancers that do TLS offloading.

gRPC

  • WithLogger allows injecting a custom go-log/logr value
  • default logging level for messages issued by this package is debug instead of info. Can be tweaked with WithPreRunLevel
  • warn level logging for non-https configuration moves to preRunLevel. Insecure http is used behind load-balancers that do TLS offloading.

@vroldanbet vroldanbet marked this pull request as ready for review September 26, 2022 14:21
@vroldanbet vroldanbet marked this pull request as draft September 26, 2022 14:21
@vroldanbet vroldanbet force-pushed the cobrautil-v2 branch 3 times, most recently from 6e2b011 to 8bfdabf Compare September 26, 2022 15:55
@vroldanbet vroldanbet marked this pull request as ready for review September 26, 2022 16:35
this allows clients importing just the bits
they need without bringing all the transitive
module dependencies
the functionality is largely the same as v1.
The gist of this redesign is configurability: v1
didn't offer any API to have cobrautil consumer
adjust the logger was configured. This is important
as zerolog provides a vast API with many knobs to tweak.

- a new fluent API is offered so that cobrautil/zerolog can
  be extended with options as the need arises. It's used
  to implement two new options in the package
- v1 assumed the consumer would use the zerolog/log global
  instance package. This forces the consuming application to use
  that global instance. The WithTarget option offers a callback when
  the logger is instantiated, so e.g. consumers can cache the
  instance somewhere else
- v1 used zerolog global level. This is an unexpected side-effect
  the consumer may not want, specially given cobrautil is creating
  an instance of a zerolog Logger. The implementation now adjusts the
  level on the logger instance instead.
- singleton methods are provided to offer an easy path of migration
  to v2.
- WithAsync configures the logger to be non-blocking, implemented using
  diode buffered-ring. This is default by default to retain v1 behaviour.
behaviour should be the same as v1, but:
- removes dependency on zerolog and replaces
  with go-log/logr
- allows defining the logger used by otel
- since it's optional and has a sensible default
- also align preRunLevel in OTel with zerolog
- turned all optional arguments into a ConfigureFunc
- added WithHandler to provide a http.Handler to inject
  into ServerFromFlags
- added ListenFromFlags that instantiates the http.Server
- migrated go go-log/logr facade
- turned all optional arguments into a ConfigureFunc
- migrated go go-log/logr facade
- make sure messaging is consistent
- make use of same logging level across packages
- do not warn on insecure http/gRPC. Instead load additional key-value
- log scheme in http package
}

// CobraUtil carries the configuration for a otel CobraRunFunc
type CobraUtil struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd name this type config in each package instead of cobrautil. I think it makes sense to also make the type private, since all of the fields are also private. I'm pretty sure you can still return the type from functions, but folks will just be unable to construct it from a literal in other packages.

Picking up optgen might also be a good idea for this too: https://github.com/ecordell/optgen

@jzelinskie jzelinskie merged commit f7c2049 into jzelinskie:main Oct 3, 2022
@vroldanbet vroldanbet deleted the cobrautil-v2 branch October 4, 2022 07:40
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.

adopt go-logr
2 participants