Skip to content

Commit

Permalink
Imporve ability to run package code in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DarcyRaynerDD committed Dec 2, 2019
1 parent be5520c commit 5539755
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
28 changes: 26 additions & 2 deletions ddlambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package ddlambda

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -111,14 +112,32 @@ func Metric(metric string, value float64, tags ...string) {

// MetricWithTimestamp sends a distribution metric to DataDog with a custom timestamp
func MetricWithTimestamp(metric string, value float64, timestamp time.Time, tags ...string) {
listener := metrics.GetListener(GetContext())
ctx := GetContext()

if ctx == nil {
logger.Debug("no context available, did you wrap your handler?")
return
}

listener := metrics.GetListener(ctx)

if listener == nil {
logger.Error(fmt.Errorf("couldn't get metrics listener from current context"))
return
}
listener.AddDistributionMetric(metric, value, timestamp, tags...)
}

// InvokeDryRun is a utility to easily run your lambda for testing
func InvokeDryRun(callback func(ctx context.Context), cfg *Config) (interface{}, error) {
wrapped := WrapHandler(callback, cfg)
handler, ok := wrapped.(func(ctx context.Context, msg json.RawMessage) (interface{}, error))
if !ok {
logger.Debug("Could not unwrap lambda during dry run")
}
return handler(context.Background(), json.RawMessage("{}"))
}

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

mc := metrics.Config{
Expand All @@ -130,6 +149,7 @@ func (cfg *Config) toMetricsConfig() metrics.Config {
mc.ShouldRetryOnFailure = cfg.ShouldRetryOnFailure
mc.APIKey = cfg.APIKey
mc.KMSAPIKey = cfg.KMSAPIKey
mc.Site = cfg.Site
mc.ShouldUseLogForwarder = cfg.ShouldUseLogForwarder
}

Expand All @@ -139,7 +159,11 @@ func (cfg *Config) toMetricsConfig() metrics.Config {
if mc.Site == "" {
mc.Site = DefaultSite
}
mc.Site = fmt.Sprintf("https://api.%s/api/v1", mc.Site)
if strings.HasPrefix(mc.Site, "https://") || strings.HasPrefix(mc.Site, "http://") {
mc.Site = fmt.Sprintf("%s/api/v1", mc.Site)
} else {
mc.Site = fmt.Sprintf("https://api.%s/api/v1", mc.Site)
}

if !mc.ShouldUseLogForwarder {
shouldUseLogForwarder := os.Getenv(DatadogShouldUseLogForwarderEnvVar)
Expand Down
48 changes: 48 additions & 0 deletions ddlambda_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed
* under the Apache License Version 2.0.
*
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019 Datadog, Inc.
*/
package ddlambda

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestInvokeDryRun(t *testing.T) {
called := false
InvokeDryRun(func(ctx context.Context) {
called = true
globalCtx := GetContext()
assert.Equal(t, globalCtx, ctx)
}, nil)
assert.True(t, called)
}

func TestMetricsSilentFailWithoutWrapper(t *testing.T) {
Metric("my-metric", 100, "my:tag")
}

func TestMetricsSubmitWithWrapper(t *testing.T) {
called := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
w.WriteHeader(http.StatusCreated)
}))
defer server.Close()

InvokeDryRun(func(ctx context.Context) {
Metric("my-metric", 100, "my:tag")
}, &Config{
APIKey: "abc-123",
Site: server.URL,
})
assert.True(t, called)
}
1 change: 1 addition & 0 deletions internal/wrapper/wrap_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func WrapHandlerWithListeners(handler interface{}, listeners ...HandlerListener)
for _, listener := range listeners {
listener.HandlerFinished(ctx)
}
CurrentContext = nil
return result, err
}
}
Expand Down

0 comments on commit 5539755

Please sign in to comment.