This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a simple HTTP client instead of datadog api client library.
This change brings binary size back to 52M (from 61M). We need only 1% of the datadog API client's functionality and using it increases binary size by 17% to 22% (Ref: #628). I believe this is happening because in datadog API client library, basic symbols like APIClient or Configuration refer to a lot of other symbols[1]. This means that even after compilation and linking, almost all the symbols become part of the generated binary. I've verified that surfacer continues to work after this change. [1] - https://github.com/DataDog/datadog-api-client-go/blob/5c76e2376ad3d1ceba0eb1199bf9447808e88948/api/v1/datadog/client.go#L43 PiperOrigin-RevId: 385683900
- Loading branch information
Showing
4 changed files
with
272 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2021 The Cloudprober Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package datadog | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
const defaultServer = "api.datadoghq.com" | ||
|
||
type ddClient struct { | ||
apiKey string | ||
appKey string | ||
server string | ||
c http.Client | ||
} | ||
|
||
// ddSeries A metric to submit to Datadog. See: | ||
// https://docs.datadoghq.com/developers/metrics/#custom-metrics-properties | ||
type ddSeries struct { | ||
// The name of the host that produced the metric. | ||
Host *string `json:"host,omitempty"` | ||
// The name of the timeseries. | ||
Metric string `json:"metric"` | ||
// Points relating to a metric. All points must be tuples with timestamp and | ||
// a scalar value (cannot be a string). Timestamps should be in POSIX time in | ||
// seconds, and cannot be more than ten minutes in the future or more than | ||
// one hour in the past. | ||
Points [][]float64 `json:"points"` | ||
// A list of tags associated with the metric. | ||
Tags *[]string `json:"tags,omitempty"` | ||
// The type of the metric either `count`, `gauge`, or `rate`. | ||
Type *string `json:"type,omitempty"` | ||
} | ||
|
||
func newClient(server, apiKey, appKey string) *ddClient { | ||
c := &ddClient{ | ||
apiKey: apiKey, | ||
appKey: appKey, | ||
server: server, | ||
c: http.Client{}, | ||
} | ||
if c.apiKey == "" { | ||
c.apiKey = os.Getenv("DD_API_KEY") | ||
} | ||
|
||
if c.appKey == "" { | ||
c.appKey = os.Getenv("DD_APP_KEY") | ||
} | ||
|
||
if c.server == "" { | ||
c.server = defaultServer | ||
} | ||
|
||
return c | ||
} | ||
|
||
func (c *ddClient) newRequest(series []ddSeries) (*http.Request, error) { | ||
url := fmt.Sprintf("https://%s/api/v1/series", c.server) | ||
|
||
// JSON encoding of the datadog series. | ||
// { | ||
// "series": [{..},{..}] | ||
// } | ||
b, err := json.Marshal(map[string][]ddSeries{"series": series}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body := &bytes.Buffer{} | ||
if _, err := body.Write(b); err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", url, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("DD-API-KEY", c.apiKey) | ||
req.Header.Set("DD-APP-KEY", c.appKey) | ||
|
||
return req, nil | ||
} | ||
|
||
func (c *ddClient) submitMetrics(ctx context.Context, series []ddSeries) error { | ||
req, err := c.newRequest(series) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
resp, err := c.c.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode >= 300 { | ||
b, _ := ioutil.ReadAll(resp.Body) | ||
return fmt.Errorf("error, HTTP status: %d, full response: %s", resp.StatusCode, string(b)) | ||
} | ||
|
||
return nil | ||
} |
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,133 @@ | ||
// Copyright 2021 The Cloudprober Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package datadog | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"os" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
cAPIKey, cAppKey := "c-apiKey", "c-appKey" | ||
eAPIKey, eAppKey := "e-apiKey", "e-appKey" | ||
|
||
tests := []struct { | ||
desc string | ||
apiKey string | ||
appKey string | ||
server string | ||
env map[string]string | ||
wantClient *ddClient | ||
}{ | ||
{ | ||
desc: "keys-from-config", | ||
apiKey: cAPIKey, | ||
appKey: cAppKey, | ||
server: "", | ||
wantClient: &ddClient{ | ||
apiKey: cAPIKey, | ||
appKey: cAppKey, | ||
server: defaultServer, | ||
}, | ||
}, | ||
{ | ||
desc: "keys-from-env", | ||
env: map[string]string{ | ||
"DD_API_KEY": eAPIKey, | ||
"DD_APP_KEY": eAppKey, | ||
}, | ||
server: "test-server", | ||
wantClient: &ddClient{ | ||
apiKey: eAPIKey, | ||
appKey: eAppKey, | ||
server: "test-server", | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.desc, func(t *testing.T) { | ||
for k, v := range test.env { | ||
os.Setenv(k, v) | ||
} | ||
|
||
c := newClient(test.server, test.apiKey, test.appKey) | ||
if !reflect.DeepEqual(c, test.wantClient) { | ||
t.Errorf("got client: %v, want client: %v", c, test.wantClient) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewRequest(t *testing.T) { | ||
ts := time.Now().Unix() | ||
tags := []string{"probe:cloudprober_http"} | ||
metricType := "count" | ||
|
||
testSeries := []ddSeries{ | ||
{ | ||
Metric: "cloudprober.success", | ||
Points: [][]float64{[]float64{float64(ts), 99}}, | ||
Tags: &tags, | ||
Type: &metricType, | ||
}, | ||
{ | ||
Metric: "cloudprober.total", | ||
Points: [][]float64{[]float64{float64(ts), 100}}, | ||
Tags: &tags, | ||
Type: &metricType, | ||
}, | ||
} | ||
|
||
testClient := newClient("", "test-api-key", "test-app-key") | ||
req, err := testClient.newRequest(testSeries) | ||
|
||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
// Check URL | ||
wantURL := "https://api.datadoghq.com/api/v1/series" | ||
if req.URL.String() != wantURL { | ||
t.Errorf("Got URL: %s, wanted: %s", req.URL.String(), wantURL) | ||
} | ||
|
||
// Check request headers | ||
for k, v := range map[string]string{ | ||
"DD-API-KEY": "test-api-key", | ||
"DD-APP-KEY": "test-app-key", | ||
} { | ||
if req.Header.Get(k) != v { | ||
t.Errorf("%s header: %s, wanted: %s", k, req.Header.Get(k), v) | ||
} | ||
} | ||
|
||
// Check request body | ||
b, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
t.Errorf("Error reading request body: %v", err) | ||
} | ||
data := map[string][]ddSeries{} | ||
if err := json.Unmarshal(b, &data); err != nil { | ||
t.Errorf("Error unmarshaling request body: %v", err) | ||
} | ||
if !reflect.DeepEqual(data["series"], testSeries) { | ||
t.Errorf("s.Series: %v, testSeries: %v", data["series"], testSeries) | ||
} | ||
} |
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