golang 獲取es 日誌

package es

import (
    "bytes"
    "context"
    "encoding/json"
    "errors"
    "fmt"
    elasticsearch "github.com/elastic/go-elasticsearch/v6"
    log "github.com/sirupsen/logrus"
)

var es *elasticsearch.Client
var esAddr string = "http://ip:port" // es 地址及端口
var esIndex string = "job*"   // index 前綴,表示獲取job*的全部index

func init() {
    var err error
    config := elasticsearch.Config{}
    config.Addresses = []string{esAddr}
    es, err = elasticsearch.NewClient(config)
    if err != nil {
        log.Error(err.Error())
    }
}

func SearchByJob(job string) (*string, error) {  //經過job名稱獲取es日誌
    var (
        buf bytes.Buffer
        r   map[string]interface{}
        bt  bytes.Buffer
    )
    query := map[string]interface{}{
        "query": map[string]interface{}{
            "match_phrase": map[string]interface{}{
                "kubernetes.labels.tf-job-name": job,  //具體的查詢條件,能夠根據日誌格式進行修改
            },
        },
        "_source": map[string]interface{}{
            "includes": []interface{}{
                "log",
            },
        },
        "sort": map[string]interface{}{  // 排序
            "@timestamp": map[string]interface{}{
                "order": "asc",
            },
            "_id": map[string]interface{}{
                "order": "asc",
            },
        },
    }
    //fmt.Println(query)
    if err := json.NewEncoder(&buf).Encode(query); err != nil {
        log.Errorf("Error encoding query: %s", err)
        return nil, err
    }
    res, err := es.Search(
        es.Search.WithContext(context.Background()),
        es.Search.WithIndex(esIndex),
        es.Search.WithBody(&buf),
        es.Search.WithTrackTotalHits(true),
        es.Search.WithPretty(),
        es.Search.WithSize(100000),
    )
    if err != nil {
        log.Errorf("Error getting response: %s", err)
        return nil, err
    }
    defer res.Body.Close()
    fmt.Println(res)
    if res.IsError() {
        log.Error(res)
        return nil, errors.New(fmt.Sprint(res))
    }
    if res.StatusCode != 200 {
        log.Errorf("request error: %d", res.StatusCode)
        return nil, err
    }
    //fmt.Println(res)
    if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
        log.Errorf("Error parsing the response body: %s", err)
        return nil, err
    }
    log.Infof("log length: %d", len(r["hits"].(map[string]interface{})["hits"].([]interface{})))
    for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) {
        bt.WriteString(fmt.Sprintf("%s", hit.(map[string]interface{})["_source"].(map[string]interface{})["log"]))
    }
    logs := bt.String()
    //fmt.Println(logs)
    return &logs, nil
}
相關文章
相關標籤/搜索