• Stars
    star
    166
  • Rank 223,032 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 12 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Ruby client for DogStatsd

dogstatsd-ruby

A client for DogStatsD, an extension of the StatsD metric server for Datadog. Full API documentation is available in DogStatsD-ruby rubydoc.

Build Status

See CHANGELOG.md for changes. To suggest a feature, report a bug, or general discussion, open an issue.

Installation

First install the library:

gem install dogstatsd-ruby

Configuration

To instantiate a DogStatsd client:

# Import the library
require 'datadog/statsd'

# Create a DogStatsD client instance
statsd = Datadog::Statsd.new('localhost', 8125)
# ...
# release resources used by the client instance
statsd.close()

Or if you want to connect over Unix Domain Socket:

# Connection over Unix Domain Socket
statsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file')
# ...
# release resources used by the client instance
statsd.close()

Find a list of all the available options for your DogStatsD Client in the DogStatsD-ruby rubydoc or in the Datadog public DogStatsD documentation.

Migrating from v4.x to v5.x

If you are already using DogStatsD-ruby v4.x and you want to migrate to a version v5.x, the major change concerning you is the new threading model:

In practice, it means two things:

  1. Now that the client is buffering metrics before sending them, you have to call Datadog::Statsd#flush(sync: true) if you want synchronous behavior. In most cases, this is not needed, as the sender thread will automatically flush the buffered metrics if the buffer gets full or when you are closing the instance.

  2. You have to make sure you are either:

  • Using a singleton instance of the DogStatsD client instead of creating a new instance whenever you need one; this will let the buffering mechanism flush metrics regularly
  • Or properly disposing of the DogStatsD client instance when it is not needed anymore using the method Datadog::Statsd#close

If you have issues with the sender thread or the buffering mode, you can instantiate a client that behaves exactly as in v4.x (i.e. no sender thread and flush on every metric submission):

# Create a DogStatsD client instance using UDP
statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true, buffer_max_pool_size: 1)
# ...
statsd.close()

or

# Create a DogStatsD client instance using UDS
statsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file', single_thread: true, buffer_max_pool_size: 1)
# ...
statsd.close()

v5.x Common Pitfalls

Version v5.x of dogstatsd-ruby is using a sender thread for flushing. This provides better performance, but you need to consider the following pitfalls:

  1. Applications that use fork after having created the dogstatsd instance: the child process will automatically spawn a new sender thread to flush metrics.

  2. Applications that create multiple instances of the client without closing them: it is important to #close all instances to free the thread and the socket they are using otherwise you will leak those resources.

If you are using Sidekiq, please make sure to close the client instances that are instantiated. See this example on using DogStatsD-ruby v5.x with Sidekiq.

Applications that run into issues but can't apply these recommendations should use the single_thread mode which disables the use of the sender thread. Here is how to instantiate a client in this mode:

statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true)
# ...
# release resources used by the client instance and flush last metrics
statsd.close()

Origin detection over UDP

Origin detection is a method to detect which pod DogStatsD packets are coming from, in order to add the pod's tags to the tag list.

To enable origin detection over UDP, add the following lines to your application manifest:

env:
  - name: DD_ENTITY_ID
    valueFrom:
      fieldRef:
        fieldPath: metadata.uid

The DogStatsD client attaches an internal tag, entity_id. The value of this tag is the content of the DD_ENTITY_ID environment variable, which is the pod’s UID.

Usage

In order to use DogStatsD metrics, events, and Service Checks the Datadog Agent must be running and available.

Metrics

After the client is created, you can start sending custom metrics to Datadog. See the dedicated Metric Submission: DogStatsD documentation to see how to submit all supported metric types to Datadog with working code examples:

Some options are suppported when submitting metrics, like applying a Sample Rate to your metrics or tagging your metrics with your custom tags. Find all the available functions to report metrics in the DogStatsD-ruby rubydoc.

Events

After the client is created, you can start sending events to your Datadog Event Stream. See the dedicated Event Submission: DogStatsD documentation to see how to submit an event to Datadog your Event Stream.

Service Checks

After the client is created, you can start sending Service Checks to Datadog. See the dedicated Service Check Submission: DogStatsD documentation to see how to submit a Service Check to Datadog.

Maximum packet size in high-throughput scenarios

In order to have the most efficient use of this library in high-throughput scenarios, recommended values for the maximum packet size have already been set for both UDS (8192 bytes) and UDP (1432 bytes).

However, if are in control of your network and want to use a different value for the maximum packet size, you can do it by setting the buffer_max_payload_size parameter:

statsd = Datadog::Statsd.new('localhost', 8125, buffer_max_payload_size: 4096)
# ...
statsd.close()

Threading model

Starting with version 5.0, dogstatsd-ruby employs a new threading model where one instance of Datadog::Statsd can be shared between threads and where data sending is non-blocking (asynchronous).

When you instantiate a Datadog::Statsd, a sender thread is spawned. This thread will be called the Sender thread, as it is modeled by the Sender class. You can make use of single_thread: true to disable this behavior.

This thread is stopped when you close the statsd client (Datadog::Statsd#close). Instantiating a lot of statsd clients without calling #close after they are not needed anymore will most likely lead to threads being leaked.

The sender thread has the following logic (from Datadog::Statsd::Sender#send_loop):

while the sender message queue is not closed do
  read message from sender message queue

  if message is a Control message to flush
    flush buffer in connection
  else if message is a Control message to synchronize
    synchronize with calling thread
  else
    add message to the buffer
  end
end while

There are three different kinds of messages:

  1. a control message to flush the buffer in the connection
  2. a control message to synchronize any thread with the sender thread
  3. a message to append to the buffer

There is also an implicit message which closes the queue which will cause the sender thread to finish processing and exit.

statsd = Datadog::Statsd.new('localhost', 8125)

The message queue's maximum size (in messages) is given by the sender_queue_size argument, and has appropriate defaults for UDP (2048) and UDS (512).

The buffer_flush_interval, if enabled, is implemented with an additional thread which manages the timing of those flushes. This additional thread is used even if single_thread: true.

Usual workflow

You push metrics to the statsd client which writes them quickly to the sender message queue. The sender thread receives those message, buffers them and flushes them to the connection when the buffer limit is reached.

Flushing

When calling Datadog::Statsd#flush, a specific control message (:flush) is sent to the sender thread. When the sender thread receives it, it flushes its internal buffer into the connection.

Rendez-vous

It is possible to ensure a message has been consumed by the sender thread and written to the buffer by simply calling a rendez-vous right after. This is done when you are doing a synchronous flush using Datadog::Statsd#flush(sync: true).

Doing so means the caller thread is blocked and waiting until the data has been flushed by the sender thread.

This is useful when preparing to exit the application or when checking unit tests.

Thread-safety

By default, instances of Datadog::Statsd are thread-safe and we recommend that a single instance be reused by all application threads (even in applications that employ forking). The sole exception is the #close method β€” this method is not yet thread safe (work in progress here #209).

When using the single_thread: true mode, instances of Datadog::Statsd are still thread-safe, but you may run into contention on heavily-threaded applications, so we don’t recommend (for performance reasons) reusing these instances.

Versioning

This Ruby gem is using Semantic Versioning but please note that supported Ruby versions can change in a minor release of this library. As much as possible, we will add a "future deprecation" message in the minor release preceding the one dropping the support.

Ruby Versions

This gem supports and is tested on Ruby minor versions 2.1 through 3.1. Support for Ruby 2.0 was dropped in version 5.4.0.

Credits

dogstatsd-ruby is forked from Rein Henrichs' original Statsd client.

Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for further details.

More Repositories

1

go-profiler-notes

felixge's notes on the various go profiling methods that are available.
Jupyter Notebook
3,255
star
2

glommio

Glommio is a thread-per-core crate that makes writing highly parallel asynchronous applications in a thread-per-core architecture easier for rustaceans.
Rust
2,907
star
3

datadog-agent

Main repository for Datadog Agent
Go
2,716
star
4

stratus-red-team

☁️ ⚑ Granular, Actionable Adversary Emulation for the Cloud
Go
1,664
star
5

dd-agent

Datadog Agent Version 5
Python
1,291
star
6

integrations-core

Core integrations of the Datadog Agent
Python
878
star
7

zstd

Zstd wrapper for Go
C
724
star
8

the-monitor

Markdown files for Datadog's longform blog posts: https://www.datadoghq.com/blog/
Python
613
star
9

dd-trace-js

JavaScript APM Tracer
JavaScript
605
star
10

datadogpy

The Datadog Python library
Python
575
star
11

dd-trace-go

Datadog Go Library including APM tracing, profiling, and security monitoring.
Go
545
star
12

guarddog

🐍 πŸ” GuardDog is a CLI tool to Identify malicious PyPI and npm packages
Python
530
star
13

dd-trace-py

Datadog Python APM Client
Python
502
star
14

dd-trace-java

Datadog APM client for Java
Java
500
star
15

yubikey

YubiKey at Datadog
Shell
493
star
16

kafka-kit

Kafka storage rebalancing, automated replication throttle, cluster API and more
Go
480
star
17

dd-trace-php

Datadog PHP Clients
PHP
473
star
18

documentation

The source for Datadog's documentation site.
JavaScript
418
star
19

dd-trace-dotnet

.NET Client Library for Datadog APM
C#
412
star
20

security-labs-pocs

Proof of concept code for Datadog Security Labs referenced exploits.
Shell
355
star
21

go-python3

Go bindings to the CPython-3 API
Go
344
star
22

datadog-go

go dogstatsd client library for datadog
Go
332
star
23

terraform-provider-datadog

Terraform Datadog provider
Go
329
star
24

datadog-serverless-functions

Repo of AWS Lambda and Azure Functions functions that process streams and send data to Datadog
Python
326
star
25

helm-charts

Helm charts for Datadog products
Go
322
star
26

docker-dd-agent

Datadog Agent Dockerfile for Trusted Builds.
Roff
302
star
27

ansible-datadog

Ansible role for Datadog Agent
Jinja
294
star
28

datadog-operator

Datadog Agent Kubernetes Operator
Go
285
star
29

browser-sdk

Datadog Browser SDK
TypeScript
279
star
30

dd-trace-rb

Datadog Tracing Ruby Client
Ruby
261
star
31

threatest

Threatest is a CLI and Go framework for end-to-end testing threat detection rules.
Go
260
star
32

integrations-extras

Community developed integrations and plugins for the Datadog Agent.
Python
243
star
33

watermarkpodautoscaler

Custom controller that extends the Horizontal Pod Autoscaler
Go
207
star
34

pupernetes

Spin up a full fledged Kubernetes environment designed for local development & CI
Go
200
star
35

Miscellany

Miscellaneous scripts and tools
Python
197
star
36

php-datadogstatsd

A PHP client for DogStatsd
PHP
185
star
37

dd-sdk-ios

Datadog SDK for iOS - Swift and Objective-C.
Swift
183
star
38

java-dogstatsd-client

Java statsd client library
Java
177
star
39

sketches-go

Go implementations of the distributed quantile sketch algorithm DDSketch
Go
142
star
40

chaos-controller

πŸ’ πŸ”₯ Datadog Failure Injection System for Kubernetes
C
142
star
41

dd-sdk-android

Datadog SDK for Android (Compatible with Kotlin and Java)
Kotlin
140
star
42

kvexpress

Go program to move data in and out of Consul's KV store.
Go
128
star
43

HASH

HASH (HTTP Agnostic Software Honeypot)
JavaScript
119
star
44

docker-compose-example

A working example of using Docker Compose with Datadog
Python
116
star
45

malicious-software-packages-dataset

An open-source dataset of malicious software packages found in the wild, 100% vetted by humans.
Python
116
star
46

ebpf-manager

This manager helps handle the life cycle of your eBPF programs
Go
114
star
47

trace-examples

trace sample apps
Python
113
star
48

sketches-java

DDSketch: A Fast and Fully-Mergeable Quantile Sketch with Relative-Error Guarantees.
Java
108
star
49

dd-sdk-reactnative

Datadog SDK for ReactNative
TypeScript
105
star
50

gohai

System information collector
Go
102
star
51

datadog-lambda-js

The Datadog AWS Lambda Library for Node
TypeScript
101
star
52

chef-datadog

Chef cookbook for Datadog Agent & Integrations
Ruby
97
star
53

piecewise

Functions for piecewise regression on time series data
Python
96
star
54

orchestrion

A tool for adding instrumentation to Go code
Go
96
star
55

jmxfetch

Export JMX metrics
Java
96
star
56

extendeddaemonset

Kubernetes Extended Daemonset controller
Go
95
star
57

datadog-api-client-go

Golang client for the Datadog API
Go
95
star
58

dogstatsd-csharp-client

A DogStatsD client for C#/.NET
C#
94
star
59

gostackparse

Package gostackparse parses goroutines stack traces as produced by panic() or debug.Stack() at ~300 MiB/s.
Go
94
star
60

ansible-datadog-callback

Ansible callback to get stats & events directly into Datadog http://datadoghq.com
Python
93
star
61

dogapi-rb

Ruby client for Datadog's API
Ruby
92
star
62

redux-doghouse

Scoping helpers for building reusable components with Redux
JavaScript
90
star
63

build-plugin

Track your build performances like never before.
TypeScript
89
star
64

serverless-plugin-datadog

Serverless plugin to automagically instrument your Lambda functions with Datadog
TypeScript
87
star
65

ecommerce-workshop

Example eCommerce App for workshops and observability
Ruby
86
star
66

datadog-ci

Use Datadog from your CI.
TypeScript
85
star
67

ebpfbench

profile eBPF programs from Go
Go
83
star
68

datadog-lambda-python

The Datadog AWS Lambda Layer for Python
Python
80
star
69

sketches-py

Python implementations of the distributed quantile sketch algorithm DDSketch
Python
77
star
70

dirtypipe-container-breakout-poc

Container Excape PoC for CVE-2022-0847 "DirtyPipe"
77
star
71

datadog-api-client-typescript

Typescript client for the Datadog API
TypeScript
74
star
72

ddqa

Datadog's QA manager for releases of GitHub repositories
Python
73
star
73

datadog-trace-agent

Datadog Trace Agent archive (pre-6.10.0)
70
star
74

heroku-buildpack-datadog

Heroku Buildpack to run the Datadog Agent in a Dyno
Shell
69
star
75

datadog-api-client-python

Python client for the Datadog API
Python
68
star
76

datadog-static-analyzer

Datadog Static Analyzer
Rust
64
star
77

managed-kubernetes-auditing-toolkit

All-in-one auditing toolkit for identifying common security issues in managed Kubernetes environments. Currently supports AWS EKS.
Go
60
star
78

lading

A suite of data generation and load testing tools
Rust
60
star
79

datadog-lambda-extension

Rust
60
star
80

jsonapi

A marshaler/unmarshaler for JSON:API.
Go
59
star
81

datadog-cdk-constructs

CDK construct library to automagically instrument your Lambda functions with Datadog
TypeScript
58
star
82

datadog-lambda-go

The Datadog AWS Lambda package for Go
Go
57
star
83

datadog-api-client-java

Java client for the Datadog API
Java
54
star
84

serilog-sinks-datadog-logs

Serilog Sink that sends log events to Datadog https://www.datadoghq.com/
C#
53
star
85

puppet-datadog-agent

Puppet module to install the Datadog agent
Ruby
50
star
86

opencensus-go-exporter-datadog

Datadog exporter for OpenCensus metrics
Go
47
star
87

gello

:octocat: A self-hosted server for managing Trello cards based on GitHub webhook events
Python
45
star
88

datadog-cloudformation-resources

Python
44
star
89

effective-dashboards

A curated list of useful Datadog dashboards and Dashboard design best practices
44
star
90

ebpf-training

Go
44
star
91

jenkins-datadog-plugin

ARCHIVED: Current repository is now located https://github.com/jenkinsci/datadog-plugin
Java
42
star
92

dd-sdk-flutter

Flutter bindings and tools for utilizing Datadog Mobile SDKs
Dart
40
star
93

dd-opentracing-cpp

Datadog Opentracing C++ Client
C++
40
star
94

synthetics-ci-github-action

Use Browser and API tests in your CI/CD with Datadog Continuous Testing
TypeScript
40
star
95

rum-react-integration-examples

rum-react-integration
TypeScript
39
star
96

fluent-plugin-datadog

Fluentd output plugin for Datadog: https://www.datadog.com
Ruby
38
star
97

import-in-the-middle

Like `require-in-the-middle`, but for ESM import
JavaScript
38
star
98

ddprof

The Datadog Native Profiler for Linux
C++
35
star
99

datadog-sync-cli

Datadog cli tool to sync resources across organizations.
Python
33
star
100

apigentools

Generate API clients with ease
Python
32
star