datadogwriter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2025 License: MPL-2.0 Imports: 10 Imported by: 0

README

Iris Datadog Writer

an AGILira library

CI

External Datadog writer module for the Iris.

Overview

This module implements the iris.SyncWriter interface to provide high-performance log shipping to Datadog Logs API. It's designed as an external module to keep the core Iris library dependency-free while enabling powerful log aggregation capabilities.

Features

  • High Performance: Uses timecache for optimized timestamp generation
  • Batching: Configurable batch sizes and flush intervals
  • Resilience: Built-in retry logic and error handling
  • Concurrent: Safe for concurrent use with internal buffering
  • Datadog Integration: Native support for Datadog tags, service, environment, and version

Installation

go get github.com/agilira/iris-writer-datadog

Usage

package main

import (
    "log"
    "time"
    
    "github.com/agilira/iris"
    datadogwriter "github.com/agilira/iris-writer-datadog"
)

func main() {
    // Configure Datadog writer
    config := datadogwriter.Config{
        APIKey:      "your-datadog-api-key",
        Site:        "datadoghq.com", // or "datadoghq.eu"
        Service:     "my-service",
        Environment: "production",
        Version:     "1.0.0",
        Source:      "go",
        Hostname:    "web-server-01",
        Tags: map[string]string{
            "team": "backend",
            "component": "api",
        },
        BatchSize:     1000,
        FlushInterval: time.Second,
        Timeout:       10 * time.Second,
        OnError: func(err error) {
            log.Printf("Datadog writer error: %v", err)
        },
    }
    
    writer, err := datadogwriter.New(config)
    if err != nil {
        log.Fatal(err)
    }
    defer writer.Close()
    
    // Use with Iris logger
    logger := iris.New(iris.WithSyncWriter(writer))
    
    logger.Info("Hello from Iris to Datadog!")
}

Configuration

  • APIKey: Datadog API key for authentication (required)
  • Site: Datadog site (default: "datadoghq.com", also supports "datadoghq.eu")
  • Service: Service name to tag logs with
  • Environment: Environment to tag logs with (e.g., "production", "staging")
  • Version: Version to tag logs with
  • Source: Source to tag logs with (default: "go")
  • Hostname: Hostname to tag logs with
  • Tags: Additional static tags to attach to all logs
  • BatchSize: Number of records to batch before sending (default: 1000)
  • FlushInterval: Maximum time to wait before flushing incomplete batches (default: 1s)
  • Timeout: HTTP request timeout (default: 10s)
  • OnError: Optional error callback function
  • MaxRetries: Number of retry attempts (default: 3)
  • RetryDelay: Delay between retries (default: 100ms)
  • EnableCompression: Enable gzip compression for HTTP requests to reduce bandwidth (default: false)

Datadog Integration

This writer sends logs directly to Datadog's Logs API with the following features:

  • Structured Logging: JSON format optimized for Datadog
  • Automatic Tagging: Service, environment, version, and custom tags
  • Level Mapping: Iris log levels mapped to Datadog severity levels
  • Timestamp Precision: Millisecond precision timestamps
  • Batch Optimization: Efficient batching for high-throughput scenarios
  • Compression Support: Optional gzip compression for reduced bandwidth usage
High-Volume Logging

For applications with high log volume, enable compression to reduce network overhead:

config := datadogwriter.Config{
    APIKey:            "your-datadog-api-key",
    Site:              "datadoghq.com",
    Service:           "my-service",
    EnableCompression: true, // Reduces bandwidth usage
    BatchSize:         1000, // Larger batches for efficiency
    FlushInterval:     5 * time.Second,
}

Architecture

This module is part of the Iris modular ecosystem:

iris (core) → SyncWriter interface → iris-writer-datadog → Datadog Logs API

The external architecture ensures:

  • Zero dependencies in core Iris
  • Independent versioning and updates
  • Modular functionality that can be added/removed as needed
  • Performance optimizations specific to Datadog integration

iris-writer-datadog is licensed under the Mozilla Public License 2.0.


iris-writer-datadog • an AGILira library

Documentation

Overview

Package datadogwriter provides a Datadog Logs API writer for the Iris logging library.

This package implements the iris.SyncWriter interface to enable high-performance log shipping to Datadog. It supports batching, retry logic, and comprehensive configuration options for production use.

Basic Usage

config := datadogwriter.Config{
	APIKey:      "your-datadog-api-key",
	Site:        "datadoghq.com", // or "datadoghq.eu"
	Service:     "my-service",
	Environment: "production",
	Version:     "1.0.0",
}

writer, err := datadogwriter.New(config)
if err != nil {
	log.Fatal(err)
}
defer writer.Close()

logger := iris.New(iris.WithSyncWriter(writer))
logger.Info("Hello from Iris to Datadog!")

Configuration

The Config struct provides extensive customization options:

  • APIKey: Required Datadog API key for authentication
  • Site: Datadog site (datadoghq.com, datadoghq.eu, etc.)
  • Service, Environment, Version: Standard Datadog tags
  • BatchSize: Number of logs to batch before sending (default: 1000)
  • FlushInterval: Maximum time before flushing incomplete batches (default: 1s)
  • Timeout: HTTP request timeout (default: 10s)
  • OnError: Optional error callback function
  • MaxRetries: Number of retry attempts (default: 3)
  • RetryDelay: Delay between retries (default: 100ms)

Performance

This writer is optimized for high-throughput logging:

  • Batches multiple log entries in single HTTP requests
  • Uses time-based flushing to ensure timely delivery
  • Employs efficient JSON marshaling for Datadog's format
  • Implements retry logic with exponential backoff
  • Thread-safe for concurrent logging operations

Error Handling

The writer includes comprehensive error handling:

  • Configurable retry logic for transient failures
  • Optional error callback for monitoring integration
  • Graceful degradation on persistent failures
  • Proper resource cleanup on shutdown

Integration

This package integrates seamlessly with the Iris ecosystem:

iris (core) → SyncWriter interface → iris-writer-datadog → Datadog Logs API

The external architecture ensures zero dependencies in core Iris while providing powerful log aggregation capabilities for Datadog users.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// APIKey is the Datadog API key for authentication
	APIKey string

	// Site is the Datadog site (e.g., "datadoghq.com", "datadoghq.eu")
	Site string

	// Service name to tag logs with
	Service string

	// Environment to tag logs with
	Environment string

	// Version to tag logs with
	Version string

	// Source to tag logs with (e.g., "go", "application")
	Source string

	// Hostname to tag logs with
	Hostname string

	// Additional tags to attach to all logs
	Tags map[string]string

	// BatchSize is the maximum number of log entries to batch before sending
	BatchSize int

	// FlushInterval is the maximum time to wait before flushing incomplete batches
	FlushInterval time.Duration

	// Timeout for HTTP requests to Datadog
	Timeout time.Duration

	// OnError is an optional callback for handling errors
	OnError func(error)

	// MaxRetries is the number of retry attempts for failed requests
	MaxRetries int

	// RetryDelay is the delay between retry attempts
	RetryDelay time.Duration

	// EnableCompression enables gzip compression for HTTP requests to reduce bandwidth
	EnableCompression bool
}

Config holds the configuration for the Datadog writer

type LogEntry

type LogEntry struct {
	Timestamp int64          `json:"timestamp"`
	Level     string         `json:"status"`
	Message   string         `json:"message"`
	Service   string         `json:"service,omitempty"`
	Source    string         `json:"ddsource,omitempty"`
	Tags      string         `json:"ddtags,omitempty"`
	Hostname  string         `json:"hostname,omitempty"`
	Env       string         `json:"env,omitempty"`
	Version   string         `json:"version,omitempty"`
	Fields    map[string]any `json:",inline"`
}

LogEntry represents a single log entry for Datadog

type Writer

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

Writer implements iris.SyncWriter for Datadog Logs API

func New

func New(config Config) (*Writer, error)

New creates a new Datadog writer with the given configuration

func (*Writer) Close

func (w *Writer) Close() error

Close flushes remaining logs and shuts down the writer

func (*Writer) WriteRecord

func (w *Writer) WriteRecord(record *iris.Record) error

WriteRecord implements iris.SyncWriter

Directories

Path Synopsis
examples
integration command

Jump to

Keyboard shortcuts

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