func dial(address string, bufferSizeHint int) (conn net.Conn, bufferSize int, err error) {
	var network = "udp"
	var f *os.File
	if i := strings.Index(address, "://"); i >= 0 {
		network, address = address[:i], address[i+3:]
	}
	if conn, err = net.Dial(network, address); err != nil {
		return
	}
	uc, ok := conn.(*net.UDPConn)
	if !ok {
		bufferSize = bufferSizeHint
		return
	}
	if f, err = uc.File(); err != nil {
		conn.Close()
		return
	}
	defer f.Close()
	fd := int(f.Fd())
	if bufferSize, err = syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_SNDBUF); err != nil {
		conn.Close()
		return
	}
	bufferSize /= 2
	for bufferSizeHint > bufferSize && bufferSizeHint > 0 {
		if err := syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_SNDBUF, bufferSizeHint); err == nil {
			bufferSize = bufferSizeHint
			break
		}
		bufferSizeHint /= 2
	}
	const maxBufferSize = 65507
	if bufferSize > maxBufferSize {
		bufferSize = maxBufferSize
	}
	if bufferSize > bufferSizeHint {
		bufferSize = bufferSizeHint
	}
	syscall.SetNonblock(fd, true)
	return
}
taken from https://github.com/segmentio/stats/datadog
package dogstatsd
import (
	"context"
	"log"
	"math/rand"
	"net"
	"os"
	"strings"
	"sync"
	"sync/atomic"
	"syscall"
	"time"
	"github.com/coredns/coredns/plugin"
	"github.com/miekg/dns"
	"github.com/prometheus/client_golang/prometheus"
	dto "github.com/prometheus/client_model/go"
)
type Dogstatsd struct {
	Next plugin.Handler
	Addr string
	BufferSize int
	FlushInterval time.Duration
	Reg *prometheus.Registry
	EnableGoMetrics      bool
	EnableProcessMetrics bool
	ZoneNames []string
	once   sync.Once
	wg     sync.WaitGroup
	ctx    context.Context
	cancel context.CancelFunc
	zones  map[string]struct{}
	dockerClient dockerClient
	dockerCache  atomic.Value
	clients   counterStore
	names     counterStore
	exchanges counterStore
	randFloat64 func(min, max float64) float64
}
const (
	defaultAddr          = "udp://localhost:8125"
	defaultBufferSize    = 1024
	defaultFlushInterval = 1 * time.Minute
)
func init() {
	log.SetFlags(0)
}
func New() *Dogstatsd {
	return &Dogstatsd{
		Addr:          defaultAddr,
		BufferSize:    defaultBufferSize,
		FlushInterval: defaultFlushInterval,
		dockerClient: dockerClient{
			host: os.Getenv("DOCKER_HOST"),
		},
		clients:   makeCounterStore(),
		names:     makeCounterStore(),
		exchanges: makeCounterStore(),
	}
}
func (d *Dogstatsd) Name() string { return "dogstatsd" }
func (d *Dogstatsd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
	if cache, ok := d.dockerCache.Load().(map[string][]string); ok {
		addr := w.RemoteAddr().String()
		addr, _, _ = net.SplitHostPort(addr)
		for _, a := range cache[addr] {
			d.clients.incr(a)
			d.exchanges.incr(a + "/" + r.Question[0].Name)
		}
	}
	d.names.incr(r.Question[0].Name)
	return plugin.NextOrFailure(d.Name(), d.Next, ctx, w, r)
}
func (d *Dogstatsd) Start() {
	d.once.Do(d.init)
	d.wg.Add(1)
	go d.run(d.ctx)
}
func (d *Dogstatsd) Stop() {
	d.once.Do(d.init)
	d.cancel()
	d.wg.Wait()
}
func (d *Dogstatsd) init() {
	d.ctx, d.cancel = context.WithCancel(context.Background())
	d.zones = make(map[string]struct{}, len(d.ZoneNames))
	for _, zone := range d.ZoneNames {
		d.zones[zone] = struct{}{}
	}
}
func (d *Dogstatsd) run(ctx context.Context) {
	defer d.wg.Done()
	log.Printf("[INFO] dogstatsd %s { buffer %d; flush %s; go %t; process %t; zones %s }", d.Addr, d.BufferSize, d.FlushInterval, d.EnableGoMetrics, d.EnableProcessMetrics, d.ZoneNames)
	ticker := time.NewTicker(d.FlushInterval)
	defer ticker.Stop()
	state := make(state)
	for {
		d.refreshDockerCache()
		d.reportMetrics(state)
		select {
		case <-ticker.C:
		case <-ctx.Done():
			return
		}
	}
}
func (d *Dogstatsd) refreshDockerCache() {
	containers, err := d.dockerClient.listContainers()
	if err != nil {
		log.Printf("[ERROR] failed to list containers from docker at %s: %s", d.dockerClient.host, err)
		return
	}
	cache := map[string][]string{}
	for _, container := range containers {
		for _, network := range container.NetworkSettings.Networks {
			imageName := container.Image.name()
			ipAddress := network.IPAddress
			if len(ipAddress) == 0 {
				ipAddress = network.IPAMConfig.IPv4Address
			}
			if len(ipAddress) == 0 {
				ipAddress = network.IPAMConfig.IPv6Address
			}
			if len(ipAddress) != 0 {
				cache[ipAddress] = append(cache[ipAddress], imageName)
			}
		}
	}
	d.dockerCache.Store(cache)
}
func (d *Dogstatsd) reportMetrics(state state) {
	metrics, err := d.collectMetrics(state)
	if err != nil {
		log.Printf("[ERROR] collecting metrics: %s", err)
		return
	}
	if err := d.flushMetrics(metrics); err != nil {
		log.Printf("[ERROR] flushing metrics to the dogstatsd agent at %s: %s", d.Addr, err)
	}
}
func (d *Dogstatsd) collectMetrics(state state) ([]metric, error) {
	metricFamilies, err := d.Reg.Gather()
	if err != nil {
		return nil, err
	}
	metrics := make([]metric, 0, 2*len(metricFamilies))
	rand := d.randFloat64
	if rand == nil {
		rand = randFloat64
	}
	for _, f := range metricFamilies {
		if !d.EnableGoMetrics && isGoMetric(*f.Name) {
			continue
		}
		if !d.EnableProcessMetrics && isProcessMetric(*f.Name) {
			continue
		}
		for _, m := range f.Metric {
			if !d.matchZones(m) {
				continue
			}
			for _, v := range makeMetrics(f, m, rand) {
				if v, ok := state.observe(v); ok {
					metrics = append(metrics, v)
				}
			}
		}
	}
	for _, c := range d.clients.top(10) {
		metrics = append(metrics, c.metric("coredns.dns.clients.top10", "client"))
	}
	for _, c := range d.names.top(10) {
		metrics = append(metrics, c.metric("coredns.dns.names.top10", "name"))
	}
	for _, c := range d.exchanges.top(10) {
		metrics = append(metrics, c.metric("coredns.dns.exchanges.top10", "exchange"))
	}
	return metrics, nil
}
func (d *Dogstatsd) matchZones(m *dto.Metric) bool {
	hasZone := false
	if len(d.ZoneNames) == 0 {
		return true  
	}
	for _, label := range m.Label {
		if *label.Name == "zone" {
			hasZone = true
			if _, match := d.zones[*label.Value]; match {
				return true
			}
		}
	}
	return !hasZone  
}
func (d *Dogstatsd) flushMetrics(metrics []metric) error {
	conn, bufferSize, err := dial(d.Addr, d.BufferSize)
	if err != nil {
		return err
	}
	defer conn.Close()
	out := make([]byte, 0, bufferSize)
	buf := make([]byte, 0, bufferSize)
	for _, m := range metrics {
		buf = appendMetric(buf[:0], m)
		if len(buf) > bufferSize {
			log.Printf("[WARN] dogstatsd metric of size %d B exceeds the configured buffer size of %d B", len(buf), bufferSize)
			continue
		}
		if (len(out) + len(buf)) > bufferSize {
			if _, err := conn.Write(out); err != nil {
				return err
			}
			out = out[:0]
		}
		out = append(out, buf...)
	}
	if len(out) != 0 {
		_, err = conn.Write(out)
	}
	return err
}