lambdazap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

Lambda Zap

Add AWS lamba context fields to Uber's Zap

GoDoc Build Status Coverage Status Go Report

Installation

$ go get -u github.com/dougEfresh/lambdazap

Quick Start


package main
import (
    "context"

    "github.com/aws/aws-lambda-go/lambda"
    "github.com/dougEfresh/lambdazap"
    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
)

// Create a new lambda log context and use RequestID, FunctionName, InvokeFunctionArn and a variable from environment
var lambdazapper = lambdazap.New().
    With(lambdazap.AwsRequestID, lambdazap.FunctionName, lambdazap.InvokeFunctionArn).
    WithEnv("ZAP_TEST")

var logger *zap.Logger

func init() {
    // Init the logger outside of the handler
    logger, _ := zap.NewProduction()
}

func Handler(ctx context.Context) (string, error) {
     defer logger.Sync()
    logger.Info("Starting hander with context values ", lambdazapper.ContextValues(ctx)...)
    return "Uber zap with lambda context", nil
}

func main() {
    lambda.Start(Handler)
}

Usage

There are some non context fields such as FunctionName which you can add to all logging requests

lambdazapper := New(lambdazap.ProcessNonContextFields(false)).With(lambdazap.FunctionName, lambdazap.FunctionVersion, lambdazap.AwsRequestID)
logger.With(lambdazapper.NonContextValues()...))
logger.Info("only non context values")

The above will log FunctionName and FunctionVersion but not RequestId.

The Non Context fields are

FunctionName
FunctionVersion
LogGroupName
LogStreamName
MemoryLimitInMB

Note by default all context and non context will be logged. The option lambdazap.ProcessNonContextFields(false) will NOT log non context values (e.g. FunctionName) when used like this

logger.Info("only context values. No FunctionName!", lambdazapper.ContextValues()...)

Examples

See example handler with cloudformation.

List of fields

Prerequisites

go 1.x

Tests

$ go test -v

See travis.yaml for running benchmark tests

Benchmarks

In the spirit of Uber's zap logger, zero allocations are used:

Type Time Objects Allocated
Non Context ~150 ns/op 0 allocs/op
With Basic ~400 ns/op 0 allocs/op
With All ~733 ns/op 0 allocs/op

Deployment

Contributing

All PRs are welcome

Authors

License

This project is licensed under the Apache License - see the LICENSE file for details

Acknowledgments

TODO

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultNames = []string{
	AwsRequestID:          "requestId",
	FunctionName:          "functionName",
	FunctionVersion:       "functionVersion",
	LogGroupName:          "logGroupName",
	LogStreamName:         "logStreamName",
	InvokeFunctionArn:     "arn",
	CognitoIdentityID:     "cognitoIdentityId",
	CognitoIdentityPoolID: "cognitoIdentityPoolId",
	InstallationID:        "installationId",
	AppTitle:              "appTitle",
	AppVersionCode:        "appVersionCode",
	AppPackageName:        "appPackageName",
	MemoryLimitInMB:       "memoryLimitInMB",
}

DefaultNames of fields

Functions

func Extract

func Extract(ctx *lambdacontext.LambdaContext, field LambdaField) string

Extract a field from lambda context

Types

type ContextValuer

type ContextValuer interface {
	ContextValue(ctx *lambdacontext.LambdaContext, f LambdaField) (string, error)
}

ContextValuer Control how you get the value from a field and context

type LambdaField

type LambdaField int

LambdaField type alias

const (
	AwsRequestID LambdaField = iota
	CognitoIdentityID
	CognitoIdentityPoolID
	InstallationID
	AppTitle
	AppVersionCode
	AppPackageName
	InvokeFunctionArn
	FunctionName
	FunctionVersion
	LogGroupName
	LogStreamName
	MemoryLimitInMB
	END
)

Enums for fields

type LambdaLogContext

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

LambdaLogContext structure

func New

func New(options ...Option) *LambdaLogContext

New Create a new LambdaLogContext

func (*LambdaLogContext) ContextValue

ContextValue get the context value for a field

func (*LambdaLogContext) ContextValues

func (lc *LambdaLogContext) ContextValues(ctx context.Context) []zapcore.Field

ContextValues for the lambda context.

func (*LambdaLogContext) NonContextValues

func (lc *LambdaLogContext) NonContextValues() []zapcore.Field

NonContextValues e.g. lambdacontext.FunctionName or os.Getenv

func (*LambdaLogContext) With

func (lc *LambdaLogContext) With(fields ...LambdaField) *LambdaLogContext

With Add these fields to context Add static fields if processNonContextValues is true

func (*LambdaLogContext) WithAll

func (lc *LambdaLogContext) WithAll() *LambdaLogContext

WithAll Add all fields to logging context See ...

func (*LambdaLogContext) WithBasic

func (lc *LambdaLogContext) WithBasic() *LambdaLogContext

WithBasic Add basic logging context See ...

func (*LambdaLogContext) WithCustom

func (lc *LambdaLogContext) WithCustom(names ...string) *LambdaLogContext

WithCustom Add names from lambdacontext.ClientContext.Custom

func (*LambdaLogContext) WithEnv

func (lc *LambdaLogContext) WithEnv(names ...string) *LambdaLogContext

WithEnv Add Env from os.Getenv

func (*LambdaLogContext) WithOptions

func (lc *LambdaLogContext) WithOptions(opts ...Option) *LambdaLogContext

WithOptions add these options to the context.

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Logger.

func CustomNames

func CustomNames(n map[LambdaField]string) Option

CustomNames for fields

func CustomValues

func CustomValues(c ContextValuer) Option

CustomValues override context values. if you return an error the default ContextValue will be used.

func ProcessNonContextFields

func ProcessNonContextFields(b bool) Option

ProcessNonContextFields when calling ContextValues include or not include Values that are not part of the lambda context

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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