Skip to content

Instantly share code, notes, and snippets.

@Hunrik
Last active October 2, 2023 11:20
Show Gist options
  • Save Hunrik/9a8037f118b24ddfb689a9f907637494 to your computer and use it in GitHub Desktop.
Save Hunrik/9a8037f118b24ddfb689a9f907637494 to your computer and use it in GitHub Desktop.
Metrics simplified in golang
// pkg/metrics/metrics.go
package metrics
import (
"log"
"github.com/DataDog/datadog-go/v5/statsd"
)
var s statsd.ClientInterface = &statsd.NoOpClient{}
func init() {
client, err := statsd.New("127.0.0.1:8125")
if err != nil {
log.Printf("[ERROR] [STATSD] Failed to init statsd: %s\n", err)
return
}
s = client
}
type Incrementable string
func (i Incrementable) Inc(tags ...string) {
s.Incr(string(i), tags, 1)
}
var UserCreated = Incrementable("user.created")
var UserDeleted = Incrementable("user.deleted")
// Inside application code
metrics.UserCreated.Inc("type:business", "source:landing")
metrics.UserDeleted.Inc("type:business")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment