errlogger

package
v0.0.0-...-82535ca Latest Latest
Warning

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

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

Documentation

Overview

Package errlogger implements a logger that logs errors to Cloud Error Reporting service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Factory

func Factory(cfg *Config, req *RequestContext) logging.Factory

Factory returns a factory that produces error loggers scoped to a single request (if any).

Types

type CloudErrorReporter

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

CloudErrorReporter implements Sink by uploading reports to Cloud Error Reporting.

Construct it with NewCloudErrorReporter. Close it with Close.

func NewCloudErrorReporter

func NewCloudErrorReporter(ctx context.Context, opts ...option.ClientOption) (*CloudErrorReporter, error)

NewCloudErrorReporter constructs a CloudErrorReporter sink.

It must be closed via Close() when no longer used. The context is used to do the initial setup and, for logging Cloud Error Reporter's own errors, and for running internal uploader goroutines.

func (*CloudErrorReporter) Close

func (r *CloudErrorReporter) Close(ctx context.Context)

Close flushes pending errors and stops the sink.

func (*CloudErrorReporter) ReportError

func (r *CloudErrorReporter) ReportError(rep *ErrorReport)

ReportError asynchronously uploads the error report.

type Config

type Config struct {
	// Sink is where to send reports. Required.
	Sink Sink

	// ServiceContext is information about the running service. Required.
	ServiceContext *ServiceContext

	// UserResolver returns a token identifying an effected user. Optional.
	UserResolver func(ctx context.Context) string

	// StacklessErrorsOnly instructs to only log errors that do not have a stack
	// trace explicitly attached to them.
	//
	// This is useful in GCP environments where errors with stack traces logged
	// to Cloud Logging are already automatically recognized by the Cloud Error
	// Reporting. If we report such an error explicitly, we will end up with
	// a duplicate report.
	//
	// Error-level log lines without a stack trace aren't recognized by the Cloud
	// Error Reporting log scraper. They will still be reported explicitly.
	StacklessErrorsOnly bool
}

Config holds configuration for the error reporting logger.

type ErrorReport

type ErrorReport struct {
	// ServiceContext identifies the service, always set.
	ServiceContext *ServiceContext
	// RequestContext identifies the request, if any. Can be nil.
	RequestContext *RequestContext
	// User is a token used to count "affected users" in the aggregated reports.
	User string
	// Timestamp is when the error happened.
	Timestamp time.Time
	// Message is a human-readable error message.
	Message string
	// Stack is a stack trace to associated with the error message, always set.
	Stack string
}

ErrorReport is submitted to the sink.

type RequestContext

type RequestContext struct {
	// HTTPMethod is e.g. "GET" or "POST".
	HTTPMethod string
	// URL is a request's URL string.
	URL string
	// UserAgent is taken from the request headers.
	UserAgent string
	// RemoteIP is the callers IP address.
	RemoteIP string
	// TraceID is a trace ID associated with this request.
	TraceID string
}

RequestContext is information about the incoming request.

type ServiceContext

type ServiceContext struct {
	// Project is the cloud project name that hosts the service.
	Project string
	// Service is identifies of the service within the project.
	Service string
	// Version is the service's version string.
	Version string
}

ServiceContext is information about the running service.

type Sink

type Sink interface {
	// ReportError asynchronously uploads the error report.
	ReportError(rep *ErrorReport)
}

Sink uploads error reports to the error aggregation service.

Always best effort. Should handle errors internally. Should not block (e.g. by dropping excessive reports).

Jump to

Keyboard shortcuts

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