Last active
October 2, 2023 11:20
-
-
Save Hunrik/9a8037f118b24ddfb689a9f907637494 to your computer and use it in GitHub Desktop.
Metrics simplified in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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