Skip to content

Instantly share code, notes, and snippets.

@jredl-va
Created July 5, 2021 14:44
Show Gist options
  • Save jredl-va/7869f484cd1e63f91659bc7fa2374f9d to your computer and use it in GitHub Desktop.
Save jredl-va/7869f484cd1e63f91659bc7fa2374f9d to your computer and use it in GitHub Desktop.
Uber Cadence Metrics
package vendastacadence
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"time"
"github.com/vendasta/gosdks/config"
"github.com/cactus/go-statsd-client/v5/statsd"
"github.com/uber-go/tally"
)
var (
safeCharacters = []rune{'_'}
sanitizeOptions = tally.SanitizeOptions{
NameCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: safeCharacters,
},
KeyCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: safeCharacters,
},
ValueCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: safeCharacters,
},
ReplacementCharacter: tally.DefaultReplacementCharacter,
}
)
func newScope(appName, host string) (tally.Scope, io.Closer, error) {
ddAgentAddr := os.Getenv("DD_AGENT_ADDR")
if ddAgentAddr == "" {
nodeName := os.Getenv("GKE_NODENAME")
if nodeName != "" {
ddAgentAddr = fmt.Sprintf("%s:8125", nodeName)
} else {
ddAgentAddr = "dd-agent.default.svc.cluster.local:8125"
}
}
statter, err := statsd.NewClientWithConfig(&statsd.ClientConfig{
Address: ddAgentAddr,
UseBuffered: true,
FlushInterval: 100 * time.Millisecond,
FlushBytes: 1440,
})
if err != nil {
return tally.NoopScope, ioutil.NopCloser(bytes.NewBuffer([]byte{})), err
}
reporter := NewReporter(statter, Options{
SampleRate: 1.0,
})
scope, closer := tally.NewRootScope(tally.ScopeOptions{
Tags: map[string]string{
"env": config.CurEnv().Name(),
"service": appName,
"namespace": config.GetGkeNamespace(),
"host": host,
},
Reporter: reporter,
SanitizeOptions: &sanitizeOptions,
}, time.Second)
return scope, closer, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment