Skip to content

Commit 48bb12d

Browse files
Merge pull request #15 from DataDog/darcy.rayner/add-timestamp-options
Add option to specify timestamp of custom metric
2 parents 6399ef8 + 1fc193f commit 48bb12d

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ func myHandler(ctx context.Context, event MyEvent) (string, error) {
6262

6363
## Custom Metrics
6464

65-
Custom metrics can be submitted using the `Distribution` function. The metrics are submitted as [distribution metrics](https://docs.datadoghq.com/graphing/metrics/distributions/).
65+
Custom metrics can be submitted using the `Metric` function. The metrics are submitted as [distribution metrics](https://docs.datadoghq.com/graphing/metrics/distributions/).
6666

67-
**IMPORTANT NOTE:** If you have already been submitting the same custom metric as non-distribution metric (e.g., gauge, count, or histogram) without using the datadog-lambda-go lib, you MUST pick a new metric name to use for `ddlambda.Distribution`. Otherwise that existing metric will be converted to a distribution metric and the historical data prior to the conversion will be no longer queryable.
67+
**IMPORTANT NOTE:** If you have already been submitting the same custom metric as non-distribution metric (e.g., gauge, count, or histogram) without using the datadog-lambda-go lib, you MUST pick a new metric name to use for `ddlambda.Metric`. Otherwise that existing metric will be converted to a distribution metric and the historical data prior to the conversion will be no longer queryable.
6868

6969
```go
70-
ddlambda.Distribution(
70+
ddlambda.Metric(
7171
"coffee_house.order_value", // Metric name
7272
12.45, // The value
7373
"product:latte", "order:online" // Associated tags

ddlambda.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ func GetContext() context.Context {
9999
}
100100

101101
// Distribution sends a distribution metric to DataDog
102+
// Deprecated: Use Metric method instead
102103
func Distribution(metric string, value float64, tags ...string) {
104+
Metric(metric, value, tags...)
105+
}
106+
107+
// Metric sends a distribution metric to DataDog
108+
func Metric(metric string, value float64, tags ...string) {
109+
MetricWithTimestamp(metric, value, time.Now(), tags...)
110+
}
111+
112+
// MetricWithTimestamp sends a distribution metric to DataDog with a custom timestamp
113+
func MetricWithTimestamp(metric string, value float64, timestamp time.Time, tags ...string) {
103114
listener := metrics.GetListener(GetContext())
104115
if listener == nil {
105116
logger.Error(fmt.Errorf("couldn't get metrics listener from current context"))
106117
return
107118
}
108-
listener.AddDistributionMetric(metric, value, tags...)
119+
listener.AddDistributionMetric(metric, value, timestamp, tags...)
109120
}
110121

111122
func (cfg *Config) toMetricsConfig() metrics.Config {

internal/metrics/listener.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ func (l *Listener) HandlerFinished(ctx context.Context) {
9292
}
9393

9494
// AddDistributionMetric sends a distribution metric
95-
func (l *Listener) AddDistributionMetric(metric string, value float64, tags ...string) {
95+
func (l *Listener) AddDistributionMetric(metric string, value float64, timestamp time.Time, tags ...string) {
9696

9797
// We add our own runtime tag to the metric for version tracking
9898
tags = append(tags, getRuntimeTag())
9999

100100
if l.config.ShouldUseLogForwarder {
101101
logger.Debug("sending metric via log forwarder")
102-
unixTime := time.Now().Unix()
102+
unixTime := timestamp.Unix()
103103
lm := logMetric{
104104
MetricName: metric,
105105
Value: value,
@@ -120,7 +120,7 @@ func (l *Listener) AddDistributionMetric(metric string, value float64, tags ...s
120120
Tags: tags,
121121
Values: []MetricValue{},
122122
}
123-
m.AddPoint(time.Now(), value)
123+
m.AddPoint(timestamp, value)
124124
logger.Debug(fmt.Sprintf("adding metric \"%s\", with value %f", metric, value))
125125
l.processor.AddMetric(&m)
126126
}

internal/metrics/listener_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/http"
1515
"net/http/httptest"
1616
"testing"
17+
"time"
1718

1819
"github.com/stretchr/testify/assert"
1920
)
@@ -45,7 +46,7 @@ func TestAddDistributionMetricWithAPI(t *testing.T) {
4546

4647
listener := MakeListener(Config{APIKey: "12345", Site: server.URL})
4748
ctx := listener.HandlerStarted(context.Background(), json.RawMessage{})
48-
listener.AddDistributionMetric("the-metric", 2, "tag:a", "tag:b")
49+
listener.AddDistributionMetric("the-metric", 2, time.Now(), "tag:a", "tag:b")
4950
listener.HandlerFinished(ctx)
5051
assert.True(t, called)
5152
}
@@ -60,7 +61,7 @@ func TestAddDistributionMetricWithLogForwarder(t *testing.T) {
6061

6162
listener := MakeListener(Config{APIKey: "12345", Site: server.URL, ShouldUseLogForwarder: true})
6263
ctx := listener.HandlerStarted(context.Background(), json.RawMessage{})
63-
listener.AddDistributionMetric("the-metric", 2, "tag:a", "tag:b")
64+
listener.AddDistributionMetric("the-metric", 2, time.Now(), "tag:a", "tag:b")
6465
listener.HandlerFinished(ctx)
6566
assert.False(t, called)
6667
}

0 commit comments

Comments
 (0)