datadog-go is a library that provides a dogstatsd client in Golang.
Go 1.7+ is officially supported. Older versions might work but are not tested.
$ go get github.com/DataDog/datadog-go/statsd
Start by creating a new client:
client, err := statsd.New("127.0.0.1:8125",
statsd.WithNamespace("flubber."), // prefix every metric with the app name
statsd.WithTags([]string{"region:us-east-1a"}), // send the EC2 availability zone as a tag with every metric
// add more options here...
)
if err != nil {
log.Fatal(err)
}
You can find a list of all the available options here.
After the client is created, you can start sending metrics:
client.Gauge("kafka.health", 1, []string{"env:production", "partition:1", "partition:2"}, 1)
Each metric call requires the same parameters:
name (string)
: The metric name that will show up in Datadogvalue
: The value of the metric. Type depends on the metric typetags ([]string)
: The list of tags to apply to the metric. Multiple tags can have the same keyrate (float)
: The sampling rate in[0,1]
. For example0.5
means that half the calls will result in a metric being sent to Datadog. Set to1
to disable sampling
You can find all the available functions to report metrics here.
- The client can use the
DD_AGENT_HOST
and (optionally) theDD_DOGSTATSD_PORT
environment variables to build the target address if theaddr
parameter is empty. - If the
DD_ENTITY_ID
environment variable is found, its value will be injected as a globaldd.internal.entity_id
tag. This tag will be used by the Datadog Agent to insert container tags to the metrics. You should onlyappend
to thec.Tags
slice to avoid overwriting this global tag.
To enable origin detection and set the DD_ENTITY_ID
environment variable, add the following lines to your application manifest
env:
- name: DD_ENTITY_ID
valueFrom:
fieldRef:
fieldPath: metadata.uid
The version 6 (and above) of the Agent accepts packets through a Unix Socket datagram connection. Details about the advantages of using UDS over UDP are available in our docs.
You can use this protocol by giving a unix:https:///path/to/dsd.socket
address argument to the New
constructor.
In very high throughput environments it is possible to improve performance by changing the values of some kernel options.
sysctl -w net.unix.max_dgram_qlen=X
- Set datagram queue size to X (default value is usually 10).sysctl -w net.core.wmem_max=X
- Set the max size of the send buffer for all the host sockets.
Run the tests with:
$ go test
Please see: http:https://godoc.org/github.com/DataDog/datadog-go/statsd
datadog-go is released under the MIT license.
Original code by ooyala.