github.com/google/cloudprober@v0.11.3/surfacers/datadog/client.go (about)

     1  // Copyright 2021 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package datadog
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"encoding/json"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"os"
    25  )
    26  
    27  const defaultServer = "api.datadoghq.com"
    28  
    29  type ddClient struct {
    30  	apiKey string
    31  	appKey string
    32  	server string
    33  	c      http.Client
    34  }
    35  
    36  // ddSeries A metric to submit to Datadog. See:
    37  // https://docs.datadoghq.com/developers/metrics/#custom-metrics-properties
    38  type ddSeries struct {
    39  	// The name of the host that produced the metric.
    40  	Host *string `json:"host,omitempty"`
    41  	// The name of the timeseries.
    42  	Metric string `json:"metric"`
    43  	// Points relating to a metric. All points must be tuples with timestamp and
    44  	// a scalar value (cannot be a string). Timestamps should be in POSIX time in
    45  	// seconds, and cannot be more than ten minutes in the future or more than
    46  	// one hour in the past.
    47  	Points [][]float64 `json:"points"`
    48  	// A list of tags associated with the metric.
    49  	Tags *[]string `json:"tags,omitempty"`
    50  	// The type of the metric either `count`, `gauge`, or `rate`.
    51  	Type *string `json:"type,omitempty"`
    52  }
    53  
    54  func newClient(server, apiKey, appKey string) *ddClient {
    55  	c := &ddClient{
    56  		apiKey: apiKey,
    57  		appKey: appKey,
    58  		server: server,
    59  		c:      http.Client{},
    60  	}
    61  	if c.apiKey == "" {
    62  		c.apiKey = os.Getenv("DD_API_KEY")
    63  	}
    64  
    65  	if c.appKey == "" {
    66  		c.appKey = os.Getenv("DD_APP_KEY")
    67  	}
    68  
    69  	if c.server == "" {
    70  		c.server = defaultServer
    71  	}
    72  
    73  	return c
    74  }
    75  
    76  func (c *ddClient) newRequest(series []ddSeries) (*http.Request, error) {
    77  	url := fmt.Sprintf("https://%s/api/v1/series", c.server)
    78  
    79  	// JSON encoding of the datadog series.
    80  	// {
    81  	//   "series": [{..},{..}]
    82  	// }
    83  	b, err := json.Marshal(map[string][]ddSeries{"series": series})
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	body := &bytes.Buffer{}
    89  	if _, err := body.Write(b); err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	req, err := http.NewRequest("POST", url, body)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	req.Header.Set("DD-API-KEY", c.apiKey)
    99  	req.Header.Set("DD-APP-KEY", c.appKey)
   100  
   101  	return req, nil
   102  }
   103  
   104  func (c *ddClient) submitMetrics(ctx context.Context, series []ddSeries) error {
   105  	req, err := c.newRequest(series)
   106  	if err != nil {
   107  		return nil
   108  	}
   109  
   110  	resp, err := c.c.Do(req.WithContext(ctx))
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	if resp.StatusCode >= 300 {
   116  		b, _ := ioutil.ReadAll(resp.Body)
   117  		return fmt.Errorf("error, HTTP status: %d, full response: %s", resp.StatusCode, string(b))
   118  	}
   119  
   120  	return nil
   121  }