pallet

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

pallet

Go Report Card Go

Pallet is a Stackdriver logging helper with request context grouping of messages.

Usage

For more details, see examples/.

// Example chi router
r := chi.NewRouter()

// Create new option sets
loggingOptions := pallet.NewOptionSet()
loggingOptions.SetLabels(map[string]string{
  "test": "test",
})

// Set the google project
loggingOptions.SetProject(os.Getenv("GOOGLE_CLOUD_PROJECT"))


// Pass options to the middleware
r.Use(pallet.DefaultMiddleware(loggingOptions))

This middleware will create a Logger and pass it via the r.Context(). You can access it within your request handler with the following:

logger := pallet.GetLoggerFromRequest(r)
logger.Logf(logging.Warning, "url: %+v", r.URL.Path)

Reference Materials:

The following packages, posts and projects have influenced this project:

Documentation

Overview

Package pallet offers a clean and simple stackdriver logging interface for google services.

Grouping Logs by Request

To group all the log entries written during a single HTTP request, create two Loggers, a "parent" and a "child," with different log IDs. Both should be in the same project, and have the same MonitoredResource type and labels.

- Parent entries must have HTTPRequest.Request populated. (Strictly speaking, only the URL is necessary.)

- A child entry's timestamp must be within the time interval covered by the parent request. (i.e., before the parent.Timestamp and after the parent.Timestamp - parent.HTTPRequest.Latency. This assumes the parent.Timestamp marks the end of the request.)

- The trace field must be populated in all of the entries and match exactly.

You should observe the child log entries grouped under the parent on the console. The parent entry will not inherit the severity of its children; you must update the parent severity yourself.

Index

Constants

View Source
const ContextCloudTrace contextKey = "cloudTraceID"

ContextCloudTrace is the context key for response data from routes to middleware.

View Source
const ContextLoggingClient contextKey = "palletLoggingClient"

ContextLoggingClient is the context key for response data from routes to middleware.

Variables

This section is empty.

Functions

func DefaultMiddleware

func DefaultMiddleware(options *Options) func(http.Handler) http.Handler

DefaultMiddleware sets the context variables for cloudtrace

Types

type ErrorInvalidConfiguration

type ErrorInvalidConfiguration struct {
	// contains filtered or unexported fields
}

ErrorInvalidConfiguration is thrown when a missing configuration option is required.

func (ErrorInvalidConfiguration) Error

Error returns a string, implementing the error interface.

type Logger

type Logger struct {
	Client            *logging.Client
	Logger            *logging.Logger
	MonitoredResource *monitoredres.MonitoredResource
	TraceID           string
}

Logger is a standard logging client struct.

func GetLoggerFromContext

func GetLoggerFromContext(ctx context.Context) *Logger

GetLoggerFromContext returns the XCloudTraceContent value from the context.

func GetLoggerFromRequest

func GetLoggerFromRequest(r *http.Request) *Logger

GetLoggerFromRequest returns the loggnig.Client from the context.

func NewLoggerForRequest

func NewLoggerForRequest(r *http.Request, options *Options, loggingOptions ...logging.LoggerOption) (*Logger, error)

NewLoggerForRequest is a helper method to create a new client

func (*Logger) Alert

func (l *Logger) Alert(v interface{})

Alert calls Log with alert severity.

func (*Logger) Alertf

func (l *Logger) Alertf(format string, v ...interface{})

Alertf calls Logf with alert severity.

func (*Logger) Close

func (l *Logger) Close() error

Close closes the Logger, ensuring all logs are flushed and closing the underlying Stackdriver Logging client.

func (*Logger) Critical

func (l *Logger) Critical(v interface{})

Critical calls Log with critical severity.

func (*Logger) Criticalf

func (l *Logger) Criticalf(format string, v ...interface{})

Criticalf calls Logf with critical severity.

func (*Logger) Debug

func (l *Logger) Debug(v interface{})

Debug calls Log with debug severity.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf calls Logf with debug severity.

func (*Logger) Emergency

func (l *Logger) Emergency(v interface{})

Emergency calls Log with emergency severity.

func (*Logger) Emergencyf

func (l *Logger) Emergencyf(format string, v ...interface{})

Emergencyf calls Logf with emergency severity.

func (*Logger) Error

func (l *Logger) Error(v interface{})

Error calls Log with error severity.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf calls Logf with error severity.

func (*Logger) Flush

func (l *Logger) Flush() error

Flush will flush the active logger.

func (*Logger) Info

func (l *Logger) Info(v interface{})

Info calls Log with info severity.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof calls Logf with info severity.

func (*Logger) Log

func (l *Logger) Log(severity logging.Severity, v interface{})

Log logs with the given severity. v must be either a string, or something that marshals via the encoding/json package to a JSON object (and not any other type of JSON value).

func (*Logger) Logf

func (l *Logger) Logf(severity logging.Severity, format string, v ...interface{})

Logf logs with the given severity. Remaining arguments are handled in the manner of fmt.Printf.

func (*Logger) Notice

func (l *Logger) Notice(v interface{})

Notice calls Log with notice severity.

func (*Logger) Noticef

func (l *Logger) Noticef(format string, v ...interface{})

Noticef calls Logf with notice severity.

func (*Logger) Warning

func (l *Logger) Warning(v interface{})

Warning calls Log with warning severity.

func (*Logger) Warningf

func (l *Logger) Warningf(format string, v ...interface{})

Warningf calls Logf with warning severity.

type Options

type Options struct {
	ProjectID  string
	ServiceID  string
	VersionID  string
	InstanceID string
	LogType    string
	Labels     map[string]string
}

Options are used to configure a client.

func NewOptionSet

func NewOptionSet() *Options

NewOptionSet will create a new optionset, passed to the client.

func (*Options) SetInstanceID added in v1.0.1

func (o *Options) SetInstanceID(i string)

SetInstanceID will set an instance

func (*Options) SetLabels

func (o *Options) SetLabels(l map[string]string)

SetLabels will set labels.

func (*Options) SetProject

func (o *Options) SetProject(p string)

SetProject will set a project

func (*Options) SetService

func (o *Options) SetService(s string)

SetService will set a service

func (*Options) SetVersion

func (o *Options) SetVersion(v string)

SetVersion will set a verson

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL