Skip to content

Instantly share code, notes, and snippets.

@jamengual
Created October 27, 2020 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamengual/4c7dfd0c5ec957d4f33c6a34b28d8b81 to your computer and use it in GitHub Desktop.
Save jamengual/4c7dfd0c5ec957d4f33c6a34b28d8b81 to your computer and use it in GitHub Desktop.
datadog_dashboard_metrics_finder.go
package main
import (
"encoding/json"
"github.com/schollz/closestmatch"
"net/http"
"log"
"os"
"net/url"
"fmt"
"io/ioutil"
)
type DDresults struct {
Results struct {
Metrics []string `json:"metrics"`
} `json:"results"`
}
func GetUrl(myurl, prefix, apikey, appkey string) []string {
// Creating the request
req, err := http.NewRequest("GET", myurl, nil)
if err != nil {
log.Print(err)
os.Exit(1)
}
// Adding Query strings
q := url.Values{}
q.Add("q",prefix)
// Encode the query strings
req.URL.RawQuery = q.Encode()
// Adding headers
req.Header.Add("DD-API-KEY", apikey)
req.Header.Add("DD-APPLICATION-KEY", appkey)
// To print query string
// fmt.Println(req.URL.String())
// Use default http client to make the request
res, err := http.DefaultClient.Do(req)
// Make sure we close the connection when we recieve everything
defer res.Body.Close()
// Read bode and convert to string
bodyBytes, _ := ioutil.ReadAll(res.Body)
// // Convert to string...not sure
// bodyString := string(bodyBytes)
// fmt.Println("API Response as String:\n\n\n" + bodyString)
// Decode Json body
var results DDresults
json.Unmarshal(bodyBytes, &results)
return results.Results.Metrics
//fmt.Printf("API Response as struct %+v\n", results.Results.Metrics)
}
func main() {
myurl := "https://api.datadoghq.com/api/v1/search"
prefix := "metrics:aws.ecs"
apikey := "asdfasdfasdfasdfasdfasdf45345323"
appkey := "dsfasdfsaddfasdfasdf686868686687687686876sdfasdfasdf"
results := GetUrl(myurl,prefix,apikey,appkey)
fmt.Println(results)
wordsToTest := []string(results)
fmt.Println(wordsToTest[2])
// Choose a set of bag sizes, more is more accurate but slower
bagSizes := []int{3}
// Create a closestmatch object
cm := closestmatch.New(wordsToTest, bagSizes)
fmt.Println(cm.ClosestN("aws.ecs.service.cpuutilization.minimum", 3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment