Elasticsearch in Go

You can use Go for programmatic Elasticsearch querying in the ChaosSearch indexed data.

Go includes an official Elasticsearch library. If you are a Go developer, you can create Go applications to search the ChaosSearch indexed data using the Elasticsearch API.

Sample Go Code

This sample application shows the framework of a Go application and Elasticsearch request. Make sure that you gather information like the ChaosSearch domain name that you plan to access, the Refinery view that you will be searching against, and your account's API access key and secret key for authenticating your user access to ChaosSearch.

package main

import (
  "fmt"
  "io/ioutil"
  "net/http"
  "strings"
  "time"
  "github.com/aws/aws-sdk-go/aws/credentials"
  "github.com/aws/aws-sdk-go/aws/signer/v4"
)

func main() {

  endpoint := "https://<REPLACE_WITH_HOST_NAME>/elastic/<REPLACE_WITH_VIEW_NAME>/_search"
  region := "us-east-1" // e.g. us-east-1
  service := "es"

  payload := strings.NewReader(`{
      "size": 5,
      "header": {
         "index": "<REPLACE_WITH_VIEW_NAME>",
         "preference": 1687555313933
         },  
        "query": {
                    "bool": {
                      "must": [], 
                      "filter": [
                        {   
                          "bool": {
                            "filter": [
                              {   
                                "bool": {
                                  "should": [
                                    {   
                                      "match": {
                                        "accountName": "xxxxxx"
                                      }   
                                    }   
                                  ],  
                                  "minimum_should_match": 1
                                }   
                              },  
                              {   
                                "bool": {
                                  "should": [
                                    {   
                                      "match": {
                                        "type": "SESSION_SUMMARY"
                                      }   
                                    }   
                                  ],  
                                  "minimum_should_match": 1
                                }
                              }
                            ]
                          }
                        },
                        {
                          "range": {
                            "timestamp": {
                              "gte": "2023-06-20T19:05:30.213Z",
                              "lte": "2023-06-23T19:25:33.213Z",
                              "format": "strict_date_optional_time"
                            }
                          }
                        }
                      ],
                      "should": [],
                      "must_not": []
                    }
                  }
  }`)

  // Get credentials from environment variables and create the Signature Version 4 signer
  // Set the below environment variables
  // export AWS_SECRET_ACCESS_KEY=<REPLACE_WITH_SECRET_ACCESS_KEY>
  // export AWS_ACCESS_KEY_ID=<REPLACE_WITH_ACCESS_KEY>

  credentials := credentials.NewEnvCredentials()
  signer := v4.NewSigner(credentials)

  // An HTTP client for sending the request
  client := &http.Client{}

  // Form the HTTP request
  req, err := http.NewRequest(http.MethodPost, endpoint, payload)
  if err != nil {
    fmt.Print(err)
  }

  // You can probably infer Content-Type programmatically, but here, we just say that it's JSON
  req.Header.Add("Content-Type", "application/json")

  // Sign the request, send it, and print the response
  signer.Sign(req, payload, service, region, time.Now())
  resp, err := client.Do(req)
  if err != nil {
    fmt.Print(err)
  }
  body, error := ioutil.ReadAll(resp.Body)
if error != nil {
  fmt.Println(error)
}
resp.Body.Close()
fmt.Println(string(body))
}