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 Bookkeeping #35

Closed
wants to merge 12 commits into from
Closed

Conversation

kcajmagic
Copy link
Member

@kcajmagic kcajmagic commented Aug 15, 2018

  • Added Bookkeeper to handle logging for different routes.

Closes #34

@schmidtw
Copy link
Member

This looks good to me.

@@ -132,21 +145,6 @@ func NewPrimaryHandler(logger log.Logger, v *viper.Viper, registry xmetrics.Regi
logginghttp.SetLogger(
logger,
logginghttp.RequestInfo,

// custom logger func that extracts the intended destination of requests
func(kv []interface{}, request *http.Request) []interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you removing this block of code?

@schmidtw
Copy link
Member

@njharter can this be merged?

@johnabass
Copy link
Collaborator

We don't want to be doing bookkeeping this way, in any of our servers:

  • Bookkeeping should not be present in application layer code. This makes it hard to refactor later.
  • Bookkeeping is a cross-cutting concern, and should be done with an Alice-style decorator. There is already code to facilitate this in the logging and xcontext packages.
  • Logging decorations should transfer to every log message in an HTTP code path, not just the bookkeeping message.

@johnabass
Copy link
Collaborator

There are (3) basic steps to a better solution. The first (2) need only be done once, for all servers.

(1) Use xcontext.Populate to create an Alice-style decorator:

c := xcontext.Populate(0, logginghttp.SetLogger(
        logginghttp.Header("X-Webpa-Device-Name", "deviceName"),
        // any other go-kit request functions that you want go here, e.g. to pull data from a SAT
    ),
)

(2) Create a bookkeeping decorator, preferably in one place such as the logginghttp package:

b := func(next http.Handler) http.Handler {
    return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
        logging.GetLogger(request.Context()).Log(level.Key(), level.InfoValue(), logging.MessageKey(), "Bookkeeping")
    })
}

(3) Use both of the above decorators for any handlers in application code:

r := mux.NewRouter()

// decorate one handler
r.Handle("/foobar", alice.New(c, b).Then(myHandler))

// decorate the whole thing
alice.New(c, b).Then(r)

func doFanout(ctx context.Context, fanout *http.Request, message *wrp.Message) (context.Context, error) {
populateMessage(ctx, message)

bookkeepingLog(ctx, message)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't want this here. This is in application-layer code. We want the bookkeeping done as part of an Alice-style decorator. This decorator can go into the logginghttp package as a standard thing applications can add to their handler mappings.

return ctx, nil
}

func bookkeepingLog(ctx context.Context, message *wrp.Message) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function should not exist in application code. What you want is decoration:

package logginghttp

import (
    "github.com/Comcast/webpa-common/logging"
    "net/http"
)

func Bookkeeper(next http.Handler) http.Handler {
    return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
        // rely on another decorator, e.g. xcontext.Populate, to supply the fields
        logging.GetLogger(request.Context()).Log(logging.MessageKey(), "Bookkeeping")
        next.ServeHTTP(response, request)
    })
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Client code, such as in scytale, will then supply a contextual logger, again via decoration:

// imports omitted
c := xcontext.Populate(0,
    logginghttp.SetLogger(loggerFromViper, logginghttp.Header("X-Example", "example")),
)

// use 'c' to decorate handlers:
alice.New(c, /* other constructors).Then(myHandler)

@kcajmagic kcajmagic changed the title fix issue #34 Add Bookkeeping Nov 15, 2018

var message wrp.Message

switch request.Header.Get("Content-Type") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Decoding the message is not a very efficient way of accomplishing this. Also, we don't want to require bookkeeping to have a dependence on wrp. We should find a way to do this that doesn't require decoding, perhaps by inserting the wrp message from the fanout into the context.

@joe94 joe94 assigned joe94 and unassigned joe94 Sep 26, 2019
@johnabass johnabass added the Frozen Due to Age Issues that are considered too old and irrelevant to address label Nov 9, 2021
@johnabass johnabass closed this Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frozen Due to Age Issues that are considered too old and irrelevant to address
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scytale should log a Bookkeeping Entry like Tr1d1um
5 participants