dietdog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: ISC Imports: 11 Imported by: 0

README

dietdog, a bare-bones DataDog logger

Stream logs from the standard library log.Logger to DataDog logs API.

Why?

I wanted to send logs of an existing Go program to the DataDog, but the official DataDog Go client is enormous, over 9 Mb of source code:

$ tar tvzf datadog-api-client-go-1.14.0.tar.gz | \
    awk '$NF~/\.go$/&&$NF!~/_test\.go$/{s+=$5}END{print s}'
9646862

Yes, I know about the datadog-agent. It requires more resources than the service that emits logs.

Documentation

Overview

Package dietdog couples log.Logger from the standard library with the DataDog logs API.

Index

Constants

View Source
const DefaultIntakeEndpoint = "https://http-intake.logs.datadoghq.com/api/v2/logs"

DefaultIntakeEndpoint is a default endpoint to send logs

Variables

This section is empty.

Functions

func New

func New(opts ...Option) *writer

New returns an io.WriteCloser which can be used as a log.Logger underlying writer. It buffers logged messages and submits them to the DataDog logs API endpoint.

To get it working, you need to create a new writer providing an API key:

sink := dietdog.New(dietdog.WithAuth(apiKey))
defer sink.Close()
logger := log.New(io.MultiWriter(os.Stderr, sink), "", 0)
logger.Println("Hello, world!")

When used without an API key, writes to it become no-op.

On a high log rate writes may occasionally block until the background log consumer catches up.

To release resouces and trigger the unsubmitted logs delivery, call its Close method. Be careful not to call Fatal* and Panic* methods of its parent Logger. Because this package delivers logs to DataDog asynchronously, it won't have time to deliver those final messages, because Fatal* and Panic* methods terminate the program.

Types

type Metadata

type Metadata struct {
	Tags     string // ddtags
	Source   string // ddsource
	Service  string // service
	Hostname string // hostname
}

Metadata describes additional fields of a log message. See DataDog API documentation for more details.

type Option

type Option func(*config)

Options customize behavior of this package.

func WithAuth

func WithAuth(token string) Option

WithAuth configures DataDog API key to use.

func WithEndpoint

func WithEndpoint(url string) Option

WithEndpoint overrides which endpoint logs are sent to.

func WithLogger

func WithLogger(l *log.Logger) Option

WithLogger configures writer with an additional logger to log its own errors to. Some errors it may log include: failed delivery attempts, dropped messages because of internal queue overflow.

Be careful not to provide Logger that you plan to reconfigure to write to this writer. Such configuration may eventually result in a deadlock.

It is recommended to use a dedicated logger with a custom prefix:

sink := dietdog.New(dietdog.WithAuth(apiKey),
  dietdog.WithLogger(log.New(os.Stderr, "DataDog logging: ", 0)))

func WithMetadata

func WithMetadata(m Metadata) Option

WithMetadata adds additional metatada to every log message sent.

Jump to

Keyboard shortcuts

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