@@ -10,6 +10,7 @@ package ddlambda
10
10
11
11
import (
12
12
"context"
13
+ "encoding/json"
13
14
"fmt"
14
15
"net/http"
15
16
"os"
@@ -111,14 +112,33 @@ func Metric(metric string, value float64, tags ...string) {
111
112
112
113
// MetricWithTimestamp sends a distribution metric to DataDog with a custom timestamp
113
114
func MetricWithTimestamp (metric string , value float64 , timestamp time.Time , tags ... string ) {
114
- listener := metrics .GetListener (GetContext ())
115
+ ctx := GetContext ()
116
+
117
+ if ctx == nil {
118
+ logger .Debug ("no context available, did you wrap your handler?" )
119
+ return
120
+ }
121
+
122
+ listener := metrics .GetListener (ctx )
123
+
115
124
if listener == nil {
116
125
logger .Error (fmt .Errorf ("couldn't get metrics listener from current context" ))
117
126
return
118
127
}
119
128
listener .AddDistributionMetric (metric , value , timestamp , tags ... )
120
129
}
121
130
131
+ // InvokeDryRun is a utility to easily run your lambda for testing
132
+ func InvokeDryRun (callback func (ctx context.Context ), cfg * Config ) (interface {}, error ) {
133
+ wrapped := WrapHandler (callback , cfg )
134
+ // Convert the wrapped handler to it's underlying raw handler type
135
+ handler , ok := wrapped .(func (ctx context.Context , msg json.RawMessage ) (interface {}, error ))
136
+ if ! ok {
137
+ logger .Debug ("Could not unwrap lambda during dry run" )
138
+ }
139
+ return handler (context .Background (), json .RawMessage ("{}" ))
140
+ }
141
+
122
142
func (cfg * Config ) toMetricsConfig () metrics.Config {
123
143
124
144
mc := metrics.Config {
@@ -130,6 +150,7 @@ func (cfg *Config) toMetricsConfig() metrics.Config {
130
150
mc .ShouldRetryOnFailure = cfg .ShouldRetryOnFailure
131
151
mc .APIKey = cfg .APIKey
132
152
mc .KMSAPIKey = cfg .KMSAPIKey
153
+ mc .Site = cfg .Site
133
154
mc .ShouldUseLogForwarder = cfg .ShouldUseLogForwarder
134
155
}
135
156
@@ -139,7 +160,11 @@ func (cfg *Config) toMetricsConfig() metrics.Config {
139
160
if mc .Site == "" {
140
161
mc .Site = DefaultSite
141
162
}
142
- mc .Site = fmt .Sprintf ("https://api.%s/api/v1" , mc .Site )
163
+ if strings .HasPrefix (mc .Site , "https://" ) || strings .HasPrefix (mc .Site , "http://" ) {
164
+ mc .Site = fmt .Sprintf ("%s/api/v1" , mc .Site )
165
+ } else {
166
+ mc .Site = fmt .Sprintf ("https://api.%s/api/v1" , mc .Site )
167
+ }
143
168
144
169
if ! mc .ShouldUseLogForwarder {
145
170
shouldUseLogForwarder := os .Getenv (DatadogShouldUseLogForwarderEnvVar )
0 commit comments