datadog

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: MIT Imports: 9 Imported by: 0

README

Introduction

This package contains a ready to use hook for thelogrus logging package that will collect and send logs to datadog via the http intake. It will batch logs up on a timer or to the maximum amount per request before sending to datadog, Which ever is sooner.

It also modifies the log format to ensure that datadog can properly read the message attribute.

It allows for the basic service, hostname and source datadog tags to be provided as well as any custom global tags you want to add via a map[string]string.

Installing the module

> go get github.com/GlobalFreightSolutions/logrus-datadog-hook

Using the Module

package main

import (
  "time"

  datadog "github.com/GlobalFreightSolutions/logrus-datadog-hook"
  "github.com/sirupsen/logrus"
)

func main() {
  apiKey := "YOUR_API_KEY_HERE"
  options := &datadog.Options{
    ApiKey: &apiKey
  }
  hook, err := datadog.New(options)
  if err != nil {
    panic(err.Error())
  }

  logger := logrus.New()
  logger.AddHook(hook)
  
  // This ensures that the logger exits gracefully and all buffered logs are sent before closing down
	logrus.DeferExitHandler(hook.Close)

  for {
    logger.Info("This is a log sent to datadog")
    time.Sleep(30 * time.Second)
  }
}

Documentation

Index

Constants

View Source
const (
	DatadogHostUS endpoint = "https://http-intake.logs.datadoghq.com"
	DatadogHostEU endpoint = "https://http-intake.logs.datadoghq.eu"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DatadogHook

type DatadogHook struct {
	ApiKey                string
	Service               string
	Hostname              string
	Source                string
	Tags                  *map[string]string
	ClientBatchingEnabled bool
	MinLevel              logrus.Level
	MaxRetry              int
	DatadogEndpoint       endpoint
	Formatter             logrus.Formatter
	// contains filtered or unexported fields
}

func New

func New(options *Options) (*DatadogHook, error)

Creates and Starts a new DatadogHook

func (*DatadogHook) Close

func (h *DatadogHook) Close()

Closes the Datadog hook and shuts down gracefully after sending all

func (*DatadogHook) Fire

func (h *DatadogHook) Fire(entry *logrus.Entry) error

Fire - implement Hook interface fire the entry

func (*DatadogHook) Levels

func (h *DatadogHook) Levels() []logrus.Level

Levels - implement Hook interface supporting all levels

type Options

type Options struct {
	// The Datadog Api Key needed to authenticate
	ApiKey *string
	// The Minimum level of log to send to datadog, default is logrus.InfoLevel
	MinimumLoggingLevel *logrus.Level
	// The datadog endpoint to send logs to, default is DatadogHostUS
	DatadogEndpoint *endpoint
	// The Service tag to add to all logs
	Service *string
	// The Host tag to add to all logs
	Host *string
	// The source tag to add to all logs
	Source *string
	// A map of custom tags to add to every log
	GlobalTags *map[string]string
	// Controls whether logs are batched locally before sending to Datadog; Defaults to true
	ClientBatchingEnabled *bool
}

Jump to

Keyboard shortcuts

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