-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use its own Datadog client (#23)
- Loading branch information
Showing
8 changed files
with
108 additions
and
26 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package mtr | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/DataDog/datadog-go/statsd" | ||
) | ||
|
||
const ( | ||
DefaultNamespace = "reader." | ||
// DefaultAddr is the default address for where the DD agent would be running on a single host machine | ||
DefaultAddr = "127.0.0.1:8125" | ||
) | ||
|
||
type Client interface { | ||
Timing(name string, value time.Duration, tags map[string]string) | ||
Incr(name string, tags map[string]string) | ||
Gauge(name string, tags map[string]string, value float64) | ||
Count(name string, value int64, tags map[string]string) | ||
} | ||
|
||
type statsClient struct { | ||
client *statsd.Client | ||
rate float64 | ||
} | ||
|
||
func toDatadogTags(tags map[string]string) []string { | ||
var retTags []string | ||
for key, val := range tags { | ||
retTags = append(retTags, fmt.Sprintf("%s:%s", key, val)) | ||
} | ||
|
||
return retTags | ||
} | ||
|
||
func (s *statsClient) Count(name string, value int64, tags map[string]string) { | ||
_ = s.client.Count(name, value, toDatadogTags(tags), s.rate) | ||
} | ||
|
||
func (s *statsClient) Timing(name string, value time.Duration, tags map[string]string) { | ||
_ = s.client.Timing(name, value, toDatadogTags(tags), s.rate) | ||
} | ||
|
||
func (s *statsClient) Incr(name string, tags map[string]string) { | ||
_ = s.client.Incr(name, toDatadogTags(tags), s.rate) | ||
} | ||
|
||
func (s *statsClient) Gauge(name string, tags map[string]string, value float64) { | ||
_ = s.client.Gauge(name, value, toDatadogTags(tags), s.rate) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mtr | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/DataDog/datadog-go/statsd" | ||
"github.com/artie-labs/transfer/lib/stringutil" | ||
"os" | ||
|
||
"github.com/artie-labs/reader/constants" | ||
"github.com/artie-labs/reader/lib/logger" | ||
) | ||
|
||
func InjectDatadogIntoCtx(ctx context.Context, namespace string, tags []string, samplingRate float64) context.Context { | ||
host := os.Getenv("TELEMETRY_HOST") | ||
port := os.Getenv("TELEMETRY_PORT") | ||
address := DefaultAddr | ||
if !stringutil.Empty(host, port) { | ||
address = fmt.Sprintf("%s:%s", host, port) | ||
logger.FromContext(ctx).WithField("address", address).Info("overriding telemetry address with env vars") | ||
} | ||
|
||
datadogClient, err := statsd.New(address) | ||
if err != nil { | ||
logger.FromContext(ctx).WithError(err).Fatal("failed to create datadog client") | ||
} | ||
|
||
datadogClient.Tags = tags | ||
datadogClient.Namespace = stringutil.Override(DefaultNamespace, namespace) | ||
return context.WithValue(ctx, constants.MtrKey, &statsClient{ | ||
client: datadogClient, | ||
rate: samplingRate, | ||
}) | ||
} | ||
|
||
func FromContext(ctx context.Context) Client { | ||
metricsClientVal := ctx.Value(constants.MtrKey) | ||
if metricsClientVal == nil { | ||
logger.FromContext(ctx).Fatal("metrics client is nil") | ||
} | ||
|
||
metricsClient, isOk := metricsClientVal.(Client) | ||
if !isOk { | ||
logger.FromContext(ctx).Fatal("metrics client is not mtr.Client type") | ||
} | ||
|
||
return metricsClient | ||
} |
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
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