-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from DataDog/darcy-rayner/metrics
Darcy rayner/metrics
- Loading branch information
Showing
37 changed files
with
1,905 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/DataDog/dd-lambda-go | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go v1.19.40 // indirect | ||
github.com/aws/aws-xray-sdk-go v1.0.0-rc.9 | ||
github.com/cenkalti/backoff v2.1.1+incompatible | ||
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect | ||
github.com/pkg/errors v0.8.1 // indirect | ||
) |
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,6 @@ | ||
github.com/aws/aws-sdk-go v1.19.40/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= | ||
github.com/aws/aws-xray-sdk-go v1.0.0-rc.9/go.mod h1:XtMKdBQfpVut+tJEwI7+dJFRxxRdxHDyVNp2tHXRq04= | ||
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= | ||
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575/go.mod h1:9d6lWj8KzO/fd/NrVaLscBKmPigpZpn5YawRPw+e3Yo= | ||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
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,105 @@ | ||
package metrics | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type ( | ||
// Client sends metrics to Datadog | ||
Client interface { | ||
SendMetrics(metrics []APIMetric) error | ||
} | ||
|
||
// APIClient send metrics to Datadog, via the Datadog API | ||
APIClient struct { | ||
apiKey string | ||
appKey string | ||
baseAPIURL string | ||
httpClient *http.Client | ||
context context.Context | ||
} | ||
|
||
postMetricsModel struct { | ||
Series []APIMetric `json:"series"` | ||
} | ||
) | ||
|
||
// MakeAPIClient creates a new API client with the given api and app keys | ||
func MakeAPIClient(ctx context.Context, baseAPIURL, apiKey, appKey string) *APIClient { | ||
httpClient := &http.Client{} | ||
return &APIClient{ | ||
apiKey: apiKey, | ||
appKey: appKey, | ||
baseAPIURL: baseAPIURL, | ||
httpClient: httpClient, | ||
context: ctx, | ||
} | ||
} | ||
|
||
// PrewarmConnection sends a redundant GET request to the Datadog API to prewarm the TSL connection | ||
func (cl *APIClient) PrewarmConnection() error { | ||
req, err := http.NewRequest("GET", cl.makeRoute("validate"), nil) | ||
if err != nil { | ||
return fmt.Errorf("Couldn't create prewarming request: %v", err) | ||
} | ||
req = req.WithContext(cl.context) | ||
|
||
cl.addAPICredentials(req) | ||
_, err = cl.httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("Couldn't contact server for prewarm request") | ||
} | ||
return nil | ||
} | ||
|
||
// SendMetrics posts a batch metrics payload to the Datadog API | ||
func (cl *APIClient) SendMetrics(metrics []APIMetric) error { | ||
content, err := marshalAPIMetricsModel(metrics) | ||
if err != nil { | ||
return fmt.Errorf("Couldn't marshal metrics model: %v", err) | ||
} | ||
body := bytes.NewBuffer(content) | ||
|
||
req, err := http.NewRequest("POST", cl.makeRoute("series"), body) | ||
if err != nil { | ||
return fmt.Errorf("Couldn't create send metrics request:%v", err) | ||
} | ||
req = req.WithContext(cl.context) | ||
|
||
defer req.Body.Close() | ||
|
||
cl.addAPICredentials(req) | ||
|
||
resp, err := cl.httpClient.Do(req) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Failed to send metrics to API") | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusCreated { | ||
return fmt.Errorf("Failed to send metrics to API. Status Code %d", resp.StatusCode) | ||
} | ||
return nil | ||
} | ||
|
||
func (cl *APIClient) addAPICredentials(req *http.Request) { | ||
query := req.URL.Query() | ||
query.Add(apiKeyParam, cl.apiKey) | ||
query.Add(appKeyParam, cl.appKey) | ||
req.URL.RawQuery = query.Encode() | ||
} | ||
|
||
func (cl *APIClient) makeRoute(route string) string { | ||
return fmt.Sprintf("%s/%s", cl.baseAPIURL, route) | ||
} | ||
|
||
func marshalAPIMetricsModel(metrics []APIMetric) ([]byte, error) { | ||
pm := postMetricsModel{} | ||
pm.Series = metrics | ||
return json.Marshal(pm) | ||
} |
Oops, something went wrong.