rollrus icon indicating copy to clipboard operation
rollrus copied to clipboard

Add option to enable person context using special fields

Open montanaflynn opened this issue 4 years ago • 1 comments

Adds WithPersonFunc OptionFunc that enables using NewPersonContext from the following special fields:

  • user_id
  • user_name
  • user_email

With it enabled and using these fields you will see the Person populated in rollbar under the issue's People section:

Screen Shot 2020-07-22 at 22 30 22

And also all the errors grouped by the person:

Screen Shot 2020-07-22 at 22 29 57

Closes #46

montanaflynn avatar Jul 22 '20 15:07 montanaflynn

This feature would be really interesting. Right now I'm using the following workaround to dynamically fill the Person struct by using information stored in the Context:

type RollbarPersonInjectionWrapperHook struct {
	logrus.Hook
}

func (w *RollbarPersonInjectionWrapperHook) Fire(entry *logrus.Entry) error {
	if entry.Context != nil {
		// Retrieve the User from the Context. 
		// Replace with your own implementation.
		if user, ok := authentication.UserFromContext(entry.Context); ok {
			entry = dupEntry(entry)
			entry.Context = rollbar.NewPersonContext(entry.Context, &rollbar.Person{
				Id: fmt.Sprint(user.ID()),
				// other fields
			})
		}
	}

	return w.Hook.Fire(entry)
}

func dupEntry(entry *logrus.Entry) *logrus.Entry {
	newEntry := entry.Dup()
	newEntry.Message = entry.Message
	newEntry.Level = entry.Level
	return newEntry
}

// Usage:
func setupRollbar(logger *logrus.Logger, accessToken string, environment string) {
	logger.AddHook(&RollbarPersonInjectionWrapperHook{
		Hook: rollrus.NewHook(accessToken, environment)
	})
}

It would be nice to have a official way of doing so, along side with more extension points for similar use-cases.

Is this repo accepting new pull request?

vtfr avatar Mar 24 '23 18:03 vtfr