package thrift

import (
	"github.com/cyberdelia/statsd"
	"log"
	"os"
)

var (
	Metrics *Metric = NewMetric(os.Getenv("STATSD_URL"))
)

type Metric struct {
	client *statsd.Client
}

func NewMetric(statsdURL string) *Metric {
	var client *statsd.Client
	var err error

	if statsdURL != "" {
		client, err = statsd.Dial(statsdURL)

		if err != nil {
			log.Println(err.Error())

			return nil
		}
	}

	return &Metric{client}
}

func (m *Metric) Incr(metricName string) {
	if m.client != nil {
		m.client.Increment(metricName, 1, 1)
	}
}

func (m *Metric) Timing(metricName string, duration int64) {
	if m.client != nil {
		m.client.Timing(metricName, int(duration/1000000), 1)
	}
}

package thrift

import (
	"log"
	"os"
	"time"

	"github.com/upfluence/goutils/tracing"
	"github.com/upfluence/goutils/tracing/noop"
	"github.com/upfluence/goutils/tracing/statsd"
)

var (
	Metrics *Metric = NewMetric(os.Getenv("STATSD_URL"))
)

type Metric struct {
	tracer tracing.Tracer
}

func NewMetric(statsdURL string) *Metric {
	if statsdURL != "" {
		if t, err := statsd.NewTracer(statsdURL, ""); err != nil {
			log.Println("statsd dial: %s", err.Error())
		} else {
			return &Metric{t}
		}
	}
	return &Metric{&noop.Tracer{}}
}

func (m *Metric) Incr(metricName string) {
	m.tracer.Count(metricName, 1)
}

func (m *Metric) Timing(metricName string, duration time.Duration) {
	m.tracer.Timing(metricName, duration)
}

Use goutis tracer over plain statsd connection
Use goutis tracer over plain statsd connection
