grpc_zerolog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

Documentation

Overview

Package grpc_zerolog is a gRPC logging middleware backed by zerolog loggers

It accepts a user-configured `zerolog.Logger` that will be used for logging completed gRPC calls. The same Logger will be used for logging completed gRPC calls, and be populated into the `context.Context` passed into gRPC handler code.

On calling `StreamServerInterceptor` or `UnaryServerInterceptor` this logging middleware will add gRPC call information to the ctx so that it will be present on subsequent use of the `ctxzerolog` logger.

This package also implements request and response *payload* logging, both for server-side and client-side. These will be logged as structured `jsonpb` fields for every message received/sent (both unary and streaming). For that please use `Payload*Interceptor` functions for that. Please note that the user-provided function that determines whether to log the full request/response payload needs to be written with care, this can significantly slow down gRPC.

If a deadline is present on the gRPC request the grpc.request.deadline tag is populated when the request begins. grpc.request.deadline is a string representing the time (RFC3339) when the current call will expire.

Zerolog can also be made as a backend for gRPC library internals. For that use `ReplaceGrpcLoggerV2`.

*Server Interceptor* Below is a JSON formatted example of a log that would be logged by the server interceptor:

{
  "level": "info",					// string zerolog log levels
  "msg": "finished unary call",				// string  log message
  "grpc.code": "OK",					// string  grpc status code
  "grpc.method": "Ping",				// string  method name
  "grpc.service": "mwitkow.testproto.TestService",      // string  full name of the called service
  "grpc.start_time": "2006-01-02T15:04:05Z07:00",       // string  RFC3339 representation of the start time
  "grpc.request.deadline": "2006-01-02T15:04:05Z07:00",   // string  RFC3339 deadline of the current request if supplied
  "grpc.request.value": "something",			// string  value on the request
  "grpc.time_ms": 1.234,				// float32 run time of the call in ms
  "peer.address": {
    "IP": "127.0.0.1",					// string  IP address of calling party
    "Port": 60216,					// int     port call is coming in on
    "Zone": ""						// string  peer zone for caller
  },
  "span.kind": "server",				// string  client | server
  "system": "grpc"					// string

  "custom_field": "custom_value",			// string  user defined field
  "custom_tags.int": 1337,				// int     user defined tag on the ctx
  "custom_tags.string": "something",			// string  user defined tag on the ctx
}

*Payload Interceptor* Below is a JSON formatted example of a log that would be logged by the payload interceptor:

{
  "level": "info",							// string zerolog log levels
  "msg": "client request payload logged as grpc.request.content",   	// string log message

  "grpc.request.content": {						// object content of RPC request
    "value": "something",						// string defined by caller
    "sleepTimeMs": 9999							// int    defined by caller
  },
  "grpc.method": "Ping",						// string method being called
  "grpc.service": "mwitkow.testproto.TestService",			// string service being called
  "span.kind": "client",						// string client | server
  "system": "grpc"							// string
}

Please see examples and tests for examples of use.

Example (Initialization)

Initialization shows a relatively complex initialization sequence.

package main

import (
	"os"

	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
	"github.com/rs/zerolog"

	grpc_zerolog "github.com/winebarrel/grpc-zerolog"
	"google.golang.org/grpc"
)

var (
	zerologLogger zerolog.Logger = zerolog.New(os.Stderr)
	customFunc    grpc_zerolog.CodeToLevel
)

func main() {
	// Shared options for the logger, with a custom gRPC code to log level function.
	opts := []grpc_zerolog.Option{
		grpc_zerolog.WithLevels(customFunc),
	}
	// Make sure that log statements internal to gRPC library are logged using the zerolog Logger as well.
	grpc_zerolog.ReplaceGrpcLoggerV2(&zerologLogger)
	// Create a server, make sure we put the grpc_ctxtags context before everything else.
	_ = grpc.NewServer(
		grpc_middleware.WithUnaryServerChain(
			grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
			grpc_zerolog.UnaryServerInterceptor(&zerologLogger, opts...),
		),
		grpc_middleware.WithStreamServerChain(
			grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
			grpc_zerolog.StreamServerInterceptor(&zerologLogger, opts...),
		),
	)
}
Output:

Example (InitializationWithDurationFieldOverride)
package main

import (
	"os"
	"time"

	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
	"github.com/rs/zerolog"

	grpc_zerolog "github.com/winebarrel/grpc-zerolog"
	"google.golang.org/grpc"
)

var zerologLogger zerolog.Logger = zerolog.New(os.Stderr)

func main() {
	// Shared options for the logger, with a custom duration to log field function.
	opts := []grpc_zerolog.Option{
		grpc_zerolog.WithDurationField(func(duration time.Duration) (key string, value interface{}) {
			return "grpc.time_ns", duration.Nanoseconds()
		}),
	}
	_ = grpc.NewServer(
		grpc_middleware.WithUnaryServerChain(
			grpc_ctxtags.UnaryServerInterceptor(),
			grpc_zerolog.UnaryServerInterceptor(&zerologLogger, opts...),
		),
		grpc_middleware.WithStreamServerChain(
			grpc_ctxtags.StreamServerInterceptor(),
			grpc_zerolog.StreamServerInterceptor(&zerologLogger, opts...),
		),
	)
}
Output:

Index

Examples

Constants

View Source
const (
	// SystemField is used in every log statement made through grpc_zerolog. Can be overwritten before any initialization code.
	SystemField = "system"

	// KindField describes the log field used to indicate whether this is a server or a client log statement.
	KindField = "span.kind"
)

Variables

View Source
var DefaultDurationToField = DurationToTimeMillisField

DefaultDurationToField is the default implementation of converting request duration to a log field (key and value).

View Source
var (
	// JSONPbMarshaller is the marshaller used for serializing protobuf messages.
	// If needed, this variable can be reassigned with a different marshaller with the same Marshal() signature.
	// NOTE: We need to maintain proto v1 support since the test Messages are still v1.
	JSONPbMarshaller grpc_logging.JsonPbMarshaler = &jsonpb.Marshaler{}
)

Functions

func DefaultClientCodeToLevel

func DefaultClientCodeToLevel(code codes.Code) zerolog.Level

DefaultClientCodeToLevel is the default implementation of gRPC return codes to log levels for client side.

func DefaultCodeToLevel

func DefaultCodeToLevel(code codes.Code) zerolog.Level

DefaultCodeToLevel is the default implementation of gRPC return codes to log levels for server side.

func DefaultMessageProducer

func DefaultMessageProducer(ctx context.Context, format string, level zerolog.Level, code codes.Code, err error, fields map[string]interface{})

DefaultMessageProducer writes the default message.

func DurationToDurationField

func DurationToDurationField(duration time.Duration) (key string, value interface{})

DurationToDurationField uses the duration value to log the request duration.

func DurationToTimeMillisField

func DurationToTimeMillisField(duration time.Duration) (key string, value interface{})

DurationToTimeMillisField converts the duration to milliseconds and uses the key `grpc.time_ms`.

func PayloadStreamClientInterceptor

func PayloadStreamClientInterceptor(logger *zerolog.Logger, decider grpc_logging.ClientPayloadLoggingDecider) grpc.StreamClientInterceptor

PayloadStreamClientInterceptor returns a new streaming client interceptor that logs the payloads of requests and responses.

func PayloadStreamServerInterceptor

func PayloadStreamServerInterceptor(logger *zerolog.Logger, decider grpc_logging.ServerPayloadLoggingDecider) grpc.StreamServerInterceptor

PayloadStreamServerInterceptor returns a new server server interceptors that logs the payloads of requests.

This *only* works when placed *after* the `grpc_zerolog.StreamServerInterceptor`. However, the logging can be done to a separate instance of the logger.

func PayloadUnaryClientInterceptor

func PayloadUnaryClientInterceptor(logger *zerolog.Logger, decider grpc_logging.ClientPayloadLoggingDecider) grpc.UnaryClientInterceptor

PayloadUnaryClientInterceptor returns a new unary client interceptor that logs the payloads of requests and responses.

func PayloadUnaryServerInterceptor

func PayloadUnaryServerInterceptor(logger *zerolog.Logger, decider grpc_logging.ServerPayloadLoggingDecider) grpc.UnaryServerInterceptor

PayloadUnaryServerInterceptor returns a new unary server interceptors that logs the payloads of requests.

This *only* works when placed *after* the `grpc_zerolog.UnaryServerInterceptor`. However, the logging can be done to a separate instance of the logger.

func ReplaceGrpcLoggerV2

func ReplaceGrpcLoggerV2(logger *zerolog.Logger)

ReplaceGrpcLoggerV2 sets the given zerolog.Logger as a gRPC-level logger. This should be called *before* any other initialization, preferably from init() functions.

func StreamClientInterceptor

func StreamClientInterceptor(logger *zerolog.Logger, opts ...Option) grpc.StreamClientInterceptor

StreamClientInterceptor returns a new streaming client interceptor that optionally logs the execution of external gRPC calls.

func StreamServerInterceptor

func StreamServerInterceptor(logger *zerolog.Logger, opts ...Option) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new streaming server interceptor that adds zerolog.Logger to the context.

func UnaryClientInterceptor

func UnaryClientInterceptor(logger *zerolog.Logger, opts ...Option) grpc.UnaryClientInterceptor

UnaryClientInterceptor returns a new unary client interceptor that optionally logs the execution of external gRPC calls.

func UnaryServerInterceptor

func UnaryServerInterceptor(logger *zerolog.Logger, opts ...Option) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptors that adds zerolog.Logger to the context.

Types

type CodeToLevel

type CodeToLevel func(code codes.Code) zerolog.Level

CodeToLevel function defines the mapping between gRPC return codes and interceptor log level.

type DurationToField

type DurationToField func(duration time.Duration) (key string, value interface{})

DurationToField function defines how to produce duration fields for logging.

type MessageProducer

type MessageProducer func(ctx context.Context, format string, level zerolog.Level, code codes.Code, err error, fields map[string]interface{})

MessageProducer produces a user defined log message.

type Option

type Option func(*options)

Option is the function signature for an Option setter.

func WithCodes

func WithCodes(f grpc_logging.ErrorToCode) Option

WithCodes customizes the function for mapping errors to error codes.

func WithDecider

func WithDecider(f grpc_logging.Decider) Option

WithDecider customizes the function for deciding if the gRPC interceptor logs should log.

Example
package main

import (
	"os"

	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
	"github.com/rs/zerolog"

	grpc_zerolog "github.com/winebarrel/grpc-zerolog"
	"google.golang.org/grpc"
)

var zerologLogger zerolog.Logger = zerolog.New(os.Stderr)

func main() {
	opts := []grpc_zerolog.Option{
		grpc_zerolog.WithDecider(func(methodFullName string, err error) bool {
			// will not log gRPC calls if it was a call to healthcheck and no error was raised
			if err == nil && methodFullName == "blah.foo.healthcheck" {
				return false
			}

			// by default you will log all calls
			return true
		}),
	}

	_ = []grpc.ServerOption{
		grpc_middleware.WithStreamServerChain(
			grpc_ctxtags.StreamServerInterceptor(),
			grpc_zerolog.StreamServerInterceptor(&zerologLogger, opts...)),
		grpc_middleware.WithUnaryServerChain(
			grpc_ctxtags.UnaryServerInterceptor(),
			grpc_zerolog.UnaryServerInterceptor(&zerologLogger, opts...)),
	}
}
Output:

func WithDurationField

func WithDurationField(f DurationToField) Option

WithDurationField customizes the function for mapping request durations to log fields.

func WithLevels

func WithLevels(f CodeToLevel) Option

WithLevels customizes the function for mapping gRPC return codes and interceptor log level statements.

func WithMessageProducer

func WithMessageProducer(f MessageProducer) Option

WithMessageProducer customizes the function for message formation.

Directories

Path Synopsis
Package ctxzerolog is a ctxlogger that is backed by zerolog.
Package ctxzerolog is a ctxlogger that is backed by zerolog.

Jump to

Keyboard shortcuts

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