dogstatsd
(require dogstatsd) | package: racket-dogstatsd |
1 Introduction
Provides a Racket DogStatsD client for sending metrics to Datadog. DogStatsD is an extension of the StatsD protocol developed by Etsy for metrics aggregation. DogStatsD requires that you have the Datadog Agent running on the host, or another service listening for udp traffic on port 8125.
Example:
(require racket/list dogstatsd) (module+ main ; Create a socket on 127.0.0.1:8125 which the Agent listens on (sock-create) (define (send-times) ; Wrap some code with a timer metric - the elapsed time (ms) for the execution ; of the body will be sent to the agent (with-timer "rkt.timer" #:tags '("proc:send-times" "proc:with-timer") (let ([xs (range 10)]) (for ([i xs]) ; Send a counter metric (counter "rkt.counter" i #:tags '("proc:send-times" "aeon:12") #:sample-rate 0.25)))) (println (current-seconds)) (sleep 2) (send-times)) (send-times))
2 Setup a socket
procedure
(sock-create [ #:host-name? host-name #:host-port? host-port]) → udp? host-name : string? = "127.0.0.1" host-port : number? = 8125
procedure
(sock-close) → void?
3 Metrics
procedure
(counter name value [ #:sample-rate sample-rate #:tags tags]) → void? name : string? value : number? sample-rate : number? = #f tags : (listof string?) = #f
Examples:
; Simple counter (counter "dogeapp.downloads" 5) ; Counter with tags (counter "dogeapp.dowloads" 5 #:tags '("size:small" "type:shibainu")) ; Counter with sample-rate (counter "dogeapp.dowloads" 5 #:sample-rate 0.25)
procedure
(gauge name value [ #:sample-rate sample-rate #:tags tags]) → void? name : string? value : number? sample-rate : number? = #f tags : (listof string?) = #f
Examples:
; Simple gauge (gauge "dogeapp.users.active" (get-active-users)) ; Gauge with tags (gauge "dogeapp.users.active" (get-active-users) #:tags '("type:shibainu")) ; Gauge with sample-rate (gauge "dogeapp.users.active" (get-active-users) #:sample-rate 0.25)
procedure
(histogram name value [ #:sample-rate sample-rate #:tags tags]) → void? name : string? value : number? sample-rate : number? = #f tags : (listof string?) = #f
Examples:
; Simple histogram (histogram "dogeapp.uploads.filesize" file-size) ; Histogram with tags (histogram "dogeapp.uploads.filesize" file-size #:tags '("type:shibainu")) ; Histogram with sample-rate (histogram "dogeapp.uploads.filesize" file-size #:sample-rate 0.45)
procedure
(set name value [ #:sample-rate sample-rate #:tags tags]) → void? name : string? value : (or/c string? number?) sample-rate : number? = #f tags : (listof string?) = #f
Examples:
; Simple set (set "dogeapp.users.uniques" (current-user-id)) ; Set with tags (set "dogeapp.users.uniques" (current-user-id) #:tags '("type:shibainu")) ; Set with sample-rate (set "dogeapp.users.uniques" (current-user-id) #:sample-rate 0.75)
4 Timers
Timers measure the amount of time a section of code takes to execute, like the time it takes to render a web page. This module provides a manual timer procedure and a with-timer macro you can wrap code with.
procedure
(timer name value [ #:sample-rate sample-rate #:tags tags]) → void? name : string? value : number? sample-rate : number? = #f tags : (listof string?) = #f
Examples:
(define timed (... some procedure that returns milliseconds you wish to record ...)) ; Simple timer (timer "dogeapp.page.render" timed) ; Timer with tags (timer "dogeapp.page.render" timed #:tags '("type:shibainu")) ; Timer with sample-rate (timer "dogeapp.page.render" timed #:sample-rate 0.45)
syntax
(with-timer name tags-clause sample-clause body ...)
name = metric-name | ... tags-clause =
| #:tags (tags-id:id) | #:tags (tags-id:id) ... sample-clause =
| #:sample-rate sample-id:id | #:sample-rate sample-id:id ...
name : string?
tags-clause : (listof string?)
sample-clause : number?
Example:
; Time the execution of code in milliseconds and send metric to Agent (with-timer "dogeapp.page.render" #:tags '("type:threaded") ; code you want to instrument here (...)) ; Used in a define, will set timed-value to the result of (call-databass) ; and send the timing metric to the Agent (define timed-value (with-timer "dogeapp.databass.call" (call-databass ...)))
5 Buffers
For performance sensitive code you may want to batch up metrics before sending to the Agent. In this case you can create buffered versions of a metric, which only dispatch to the Agent when their limit has been reached.
procedure
(make-buffered base-proc [limit]) → procedure?
base-proc : procedure? limit : number? = 25
Examples:
; Will buffer 15 metrics before sending to the Agent (define buffered-counter (make-buffered counter 15)) ; Only the first 15 metrics will be sent (for ([x (range 17)]) (buffered-counter "dogeapp.items" x #:tags '("type:buffered")))
6 Events
procedure
(event title text [ #:timestamp timestamp #:hostname hostname #:aggregation-ket aggregation-key #:priority priority #:source-type-name source-type-name #:alert-type alert-type #:tags tags]) → void? title : string? text : string? timestamp : number? = #f hostname : string? = #f aggregation-key : string? = #f priority : (one-of/c "normal" "low") = "normal" source-type-name : string? = #f
alert-type : (one-of/c "info" "error" "warning" "success") = "info" tags : (listof string?) = #f
Example:
(define (render-page page) (with-handlers ([exn:fail? (λ (e) (event "Page render error" (exn:fail-message e) #:alert-type "error"))]) ... render page ...))
7 Service Checks
DogStatsD can send Service checks to track the status of services your application depends on.
Status constants
value
value
value
value
procedure
(service-check name status [ #:timestamp timestamp #:hostname hostname #:message message #:tags tags]) → void? name : string? status : (one-of/c OK WARNING CRITICAL UNKNOWN) timestamp : number? = #f hostname : string? = #f message : string? = #f tags : (listof string?) = #f
(define (get-redis-connection) (with-handlers ([exn:fail? (λ (e) (service-check "dogeapp.redis.connection" CRITICAL #:message (exn:fail-message e)))]) (define conn ...) (serivce-check "dogeapp.redis.connection" OK) conn))