{"id":13747399,"url":"https://github.com/DataDog/dogstatsd-ruby","last_synced_at":"2025-05-09T08:32:37.116Z","repository":{"id":3595398,"uuid":"4659423","full_name":"DataDog/dogstatsd-ruby","owner":"DataDog","description":"A Ruby client for DogStatsd","archived":false,"fork":false,"pushed_at":"2025-04-29T20:46:51.000Z","size":835,"stargazers_count":189,"open_issues_count":21,"forks_count":139,"subscribers_count":62,"default_branch":"master","last_synced_at":"2025-05-05T10:07:29.156Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.datadoghq.com/","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DataDog.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2012-06-14T04:43:39.000Z","updated_at":"2025-05-02T15:53:13.000Z","dependencies_parsed_at":"2024-01-13T03:01:07.455Z","dependency_job_id":"6e7f4b4c-4e6a-44a4-b055-1987e869eb97","html_url":"https://github.com/DataDog/dogstatsd-ruby","commit_stats":{"total_commits":503,"total_committers":94,"mean_commits":5.351063829787234,"dds":0.7892644135188867,"last_synced_commit":"694f42131f54c354bb3b23d8e571ea9151705a88"},"previous_names":[],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataDog%2Fdogstatsd-ruby","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataDog%2Fdogstatsd-ruby/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataDog%2Fdogstatsd-ruby/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataDog%2Fdogstatsd-ruby/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DataDog","download_url":"https://codeload.github.com/DataDog/dogstatsd-ruby/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252661151,"owners_count":21784561,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T06:01:27.580Z","updated_at":"2025-05-09T08:32:37.090Z","avatar_url":"https://github.com/DataDog.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# dogstatsd-ruby\n\nA client for DogStatsD, an extension of the StatsD metric server for Datadog. Full API documentation is available in [DogStatsD-ruby rubydoc](https://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/Datadog/Statsd).\n\n[![Build Status](https://secure.travis-ci.org/DataDog/dogstatsd-ruby.svg)](http://travis-ci.org/DataDog/dogstatsd-ruby)\n\nSee [CHANGELOG.md](CHANGELOG.md) for changes. To suggest a feature, report a bug, or general discussion, [open an issue](http://github.com/DataDog/dogstatsd-ruby/issues/).\n\n## Installation\n\nFirst install the library:\n\n```\ngem install dogstatsd-ruby\n```\n\n## Configuration\n\nTo instantiate a DogStatsd client:\n\n```ruby\n# Import the library\nrequire 'datadog/statsd'\n\n# Create a DogStatsD client instance\nstatsd = Datadog::Statsd.new('localhost', 8125)\n# ...\n# release resources used by the client instance\nstatsd.close()\n```\nOr if you want to connect over Unix Domain Socket:\n```ruby\n# Connection over Unix Domain Socket\nstatsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file')\n# ...\n# release resources used by the client instance\nstatsd.close()\n```\n\nFind a list of all the available options for your DogStatsD Client in the [DogStatsD-ruby rubydoc](https://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/Datadog/Statsd) or in the [Datadog public DogStatsD documentation](https://docs.datadoghq.com/developers/dogstatsd/?code-lang=ruby#client-instantiation-parameters).\n\n### Migrating from v4.x to v5.x\n\nIf you are already using DogStatsD-ruby v4.x and you want to migrate to a version v5.x, the major\nchange concerning you is the new [threading model](#threading-model):\n\nIn practice, it means two things:\n\n1. 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.\n\n2. You have to make sure you are either:\n\n  * 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\n  * Or properly disposing of the DogStatsD client instance when it is not needed anymore using the method `Datadog::Statsd#close`\n\nIf 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):\n\n```ruby\n# Create a DogStatsD client instance using UDP\nstatsd = Datadog::Statsd.new('localhost', 8125, single_thread: true, buffer_max_pool_size: 1)\n# ...\nstatsd.close()\n```\n\nor\n\n```ruby\n# Create a DogStatsD client instance using UDS\nstatsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file', single_thread: true, buffer_max_pool_size: 1)\n# ...\nstatsd.close()\n```\n\n### v5.x Common Pitfalls\n\nVersion v5.x of `dogstatsd-ruby` is using a sender thread for flushing. This provides better performance, but you need to consider the following pitfalls:\n\n1. Applications that use `fork` after having created the dogstatsd instance: the child process will automatically spawn a new sender thread to flush metrics.\n\n2. 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.\n\nIf you are using [Sidekiq](https://github.com/mperham/sidekiq), please make sure to close the client instances that are instantiated. [See this example on using DogStatsD-ruby v5.x with Sidekiq](https://github.com/DataDog/dogstatsd-ruby/blob/master/examples/sidekiq_example.rb).\n\nApplications that run into issues but can't apply these recommendations should use the `single_thread` mode which disables the use of the sender thread.\nHere is how to instantiate a client in this mode:\n\n```ruby\nstatsd = Datadog::Statsd.new('localhost', 8125, single_thread: true)\n# ...\n# release resources used by the client instance and flush last metrics\nstatsd.close()\n```\n\n### Origin detection over UDP\n\nOrigin 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.\n\nTo enable origin detection over UDP, add the following lines to your application manifest:\n\n```yaml\nenv:\n  - name: DD_ENTITY_ID\n    valueFrom:\n      fieldRef:\n        fieldPath: metadata.uid\n```\n\nThe 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.\n\n## Usage\n\nIn order to use DogStatsD metrics, events, and Service Checks the Datadog Agent must be [running and available](https://docs.datadoghq.com/developers/dogstatsd/?tab=ruby).\n\n### Metrics\n\nAfter the client is created, you can start sending custom metrics to Datadog. See the dedicated [Metric Submission: DogStatsD documentation](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?tab=ruby) to see how to submit all supported metric types to Datadog with working code examples:\n\n* [Submit a COUNT metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#count).\n* [Submit a GAUGE metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#gauge).\n* [Submit a SET metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#set)\n* [Submit a HISTOGRAM metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#histogram)\n* [Submit a DISTRIBUTION metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#distribution)\n\nSome options are suppported when submitting metrics, like [applying a Sample Rate to your metrics](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?tab=ruby#metric-submission-options) or [tagging your metrics with your custom tags](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?tab=ruby#metric-tagging). Find all the available functions to report metrics in the [DogStatsD-ruby rubydoc](https://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/Datadog/Statsd).\n\n### Events\n\nAfter the client is created, you can start sending events to your Datadog Event Stream. See the dedicated [Event Submission: DogStatsD documentation](https://docs.datadoghq.com/events/guides/dogstatsd/?code-lang=ruby) to see how to submit an event to Datadog your Event Stream.\n\n### Service Checks\n\nAfter the client is created, you can start sending Service Checks to Datadog. See the dedicated [Service Check Submission: DogStatsD documentation](https://docs.datadoghq.com/developers/service_checks/dogstatsd_service_checks_submission/?tab=ruby) to see how to submit a Service Check to Datadog.\n\n### Maximum packet size in high-throughput scenarios\n\nIn order to have the most efficient use of this library in high-throughput scenarios,\nrecommended values for the maximum packet size have already been set for both UDS (8192 bytes)\nand UDP (1432 bytes).\n\nHowever, if are in control of your network and want to use a different value for the maximum packet\nsize, you can do it by setting the `buffer_max_payload_size` parameter:\n\n```ruby\nstatsd = Datadog::Statsd.new('localhost', 8125, buffer_max_payload_size: 4096)\n# ...\nstatsd.close()\n```\n\n## Threading model\n\nStarting 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).\n\nWhen 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](../lib/datadog/statsd/sender.rb) class. You can make use of `single_thread: true` to disable this behavior.\n\nThis 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.\n\nThe sender thread has the following logic (from `Datadog::Statsd::Sender#send_loop`):\n\n```\nwhile the sender message queue is not closed do\n  read message from sender message queue\n\n  if message is a Control message to flush\n    flush buffer in connection\n  else if message is a Control message to synchronize\n    synchronize with calling thread\n  else\n    add message to the buffer\n  end\nend while\n```\n\nThere are three different kinds of messages:\n\n1. a control message to flush the buffer in the connection\n2. a control message to synchronize any thread with the sender thread\n3. a message to append to the buffer\n\nThere is also an implicit message which closes the queue which will cause the sender thread to finish processing and exit.\n\n\n```ruby\nstatsd = Datadog::Statsd.new('localhost', 8125)\n```\n\nThe message queue's maximum size (in messages) is given by the `sender_queue_size` argument, and has appropriate defaults for UDP (2048), UDS (512) and `single_thread: true` (1).\n\nThe `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`.\n\n### Usual workflow\n\nYou 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.\n\n### Flushing\n\nWhen 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.\n\n### Rendez-vous\n\nIt 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)`.\n\nDoing so means the caller thread is blocked and waiting until the data has been flushed by the sender thread.\n\nThis is useful when preparing to exit the application or when checking unit tests.\n\n### Thread-safety\n\nBy 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](https://github.com/DataDog/dogstatsd-ruby/pull/209)).\n\nWhen 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.\n\n### Delaying serialization\n\nBy default, message serialization happens synchronously whenever stat methods such as `#increment` gets called, blocking the caller. If the blocking is impacting your program's performance, you may want to consider the `delay_serialization: true` mode.\n\nThe `delay_serialization: true` mode delays the serialization of metrics to avoid the wait when submitting metrics. Serialization will still have to happen at some point, but it might be postponed until a more convenient time, such as after an HTTP request has completed.\n\nIn `single_thread: true` mode, you'll probably want to set `sender_queue_size:` from it's default of `1` to some greater value, so that it can benefit from `delay_serialization: true`. Messages will then be queued unserialized in the sender queue and processed normally whenever `sender_queue_size` is reached or `#flush` is called. You might set `sender_queue_size: Float::INFINITY` to allow for an unbounded queue that will only be processed on explicit `#flush`.\n\nIn `single_thread: false` mode, `delay_serialization: true`, will cause serialization to happen inside the sender thread.\n\n## Versioning\n\nThis Ruby gem is using [Semantic Versioning](https://guides.rubygems.org/patterns/#semantic-versioning) but please note that supported Ruby versions can change in a minor release of this library.\nAs much as possible, we will add a \"future deprecation\" message in the minor release preceding the one dropping the support.\n\n## Ruby Versions\n\nThis gem supports and is tested on Ruby minor versions 2.1 through 3.3.\nSupport for Ruby 2.0 was dropped in version 5.4.0.\n\n## Credits\n\ndogstatsd-ruby is forked from Rein Henrichs' [original Statsd client](https://github.com/reinh/statsd).\n\nCopyright (c) 2011 Rein Henrichs. See LICENSE.txt for\nfurther details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDataDog%2Fdogstatsd-ruby","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDataDog%2Fdogstatsd-ruby","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDataDog%2Fdogstatsd-ruby/lists"}