使用olivere/elastic Elasticsearch Go库时,如何调试/查看生成的查询?

huangapple go评论91阅读模式
英文:

How can I debug / view the generated query when using's the olivere/elastic elasticsearch go library?

问题

我正在尝试弄清楚https://github.com/olivere/elastic库生成的查询是什么,即发送到elasticsearch服务器的实际JSON值查询。

有一些关于跟踪日志的文档(如下所示),但似乎没有包括查询。

client, err := elastic.NewClient(
...
elastic.SetTraceLog(log.New(os.Stdout,"",0)),
)

我在这里的文档中也找不到相关的内容:https://pkg.go.dev/github.com/olivere/elastic?utm_source=godoc

英文:

I'm trying to figure out what the generated query is for the https://github.com/olivere/elastic library, as in the actual JSON value query that gets sent to the elasticsearch server.

There is some documentation about a trace log (which I've used as shown below) but this does not seem to include query.

client, err := elastic.NewClient(
...
elastic.SetTraceLog(log.New(os.Stdout,"",0)),
)

I also can't seem to find anything relevant in the docs here: https://pkg.go.dev/github.com/olivere/elastic?utm_source=godoc

答案1

得分: 1

根据文档,你可以提供自己的HTTP客户端:

// 获取一个客户端。你也可以在这里提供自己的HTTP客户端。
client, err := elastic.NewClient(elastic.SetErrorLog(errorlog))

文档到此为止 :)...实际上,你必须提供一个Doer接口。
我实例化了一个实现Doer接口的结构体,并装饰了http.Do()方法以记录http请求的详细信息:

免责声明:
这只是一个我针对在Docker容器中运行的Elastic实例使用的最小示例,仅用于解答这个问题。在生产环境中,请勿运行不安全的TLS,不要硬编码凭据,根据需要配置http传输等。

package main

import (
	"context"
	"crypto/tls"
	"fmt"
	"net/http"
	"net/http/httputil"

	"github.com/olivere/elastic/v7"
)

type LoggingHttpElasticClient struct {
	c http.Client
}

func (l LoggingHttpElasticClient) Do(r *http.Request) (*http.Response, error) {
	// 记录http请求的详细信息
	requestDump, err := httputil.DumpRequest(r, true)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("reqDump: " + string(requestDump))
	return l.c.Do(r)
}

func main() {
	doer := LoggingHttpElasticClient{
		c: http.Client{
			// 在这里加载一个受信任的CA(如果在生产环境中运行)
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			},
		},
	}

	client, err := elastic.NewClient(
		// 在这里提供记录日志的doer
		elastic.SetHttpClient(doer),

		elastic.SetBasicAuth("elastic", "<password>"),
		elastic.SetURL("https://<address>:9200"),
		elastic.SetSniff(false), // 这是特定于我的Docker Elastic运行时的设置
	)
	if err != nil {
		panic(err)
	}

	/*
		生成一个随机的HTTP请求,以检查是否记录日志
	*/
	ac := client.Alias()
	ac.Add("myIndex", "myAlias").Do(context.Background())
}

以下是输出结果:

reqDump: POST /_aliases HTTP/1.1
Host: 127.0.0.1:9200
Accept: application/json
Authorization: Basic base64(<user>:<pass>)
Content-Type: application/json
User-Agent: elastic/7.0.32 (linux-amd64)

{"actions":[{"add":{"alias":"myAlias","index":"myIndex"}}]}

我认为SetTraceLog也可以使用,但我选择了已知的路径。

英文:

According to the documentation, you can provide your own HTTP client:

> // Obtain a client. You can also provide your own HTTP client here.
client, err := elastic.NewClient(elastic.SetErrorLog(errorlog))

Well, that's where the documentation ends :)... and actually you must provide a Doer interface.
I instantiated a struct that implements the Doer interface and decorated the http.Do() in order to log the http.request dump:

Disclamer:
This is just a minimal example I used against an elastic instance running in a docker container, for the scope of this question. In production, don't run insecure TLS, don't hard-code credentials, configure the http transport as needed etc.

package main

import (
	&quot;context&quot;
	&quot;crypto/tls&quot;
	&quot;fmt&quot;
	&quot;net/http&quot;
	&quot;net/http/httputil&quot;

	&quot;github.com/olivere/elastic/v7&quot;
)

type LoggingHttpElasticClient struct {
	c http.Client
}

func (l LoggingHttpElasticClient) Do(r *http.Request) (*http.Response, error) {
	// Log the http request dump
	requestDump, err := httputil.DumpRequest(r, true)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(&quot;reqDump: &quot; + string(requestDump))
	return l.c.Do(r)
}

func main() {
	doer := LoggingHttpElasticClient{
		c: http.Client{
			// Load a trusted CA here, if running in production
			Transport: &amp;http.Transport{
				TLSClientConfig: &amp;tls.Config{InsecureSkipVerify: true},
			},
		},
	}

	client, err := elastic.NewClient(
        // Provide the logging doer here
		elastic.SetHttpClient(doer),

		elastic.SetBasicAuth(&quot;elastic&quot;, &quot;&lt;password&gt;&quot;),
		elastic.SetURL(&quot;https://&lt;address&gt;:9200&quot;),
		elastic.SetSniff(false), // this is specific to my docker elastic runtime
	)
	if err != nil {
		panic(err)
	}

    /*
        Generate a random HTTP request to check if it&#39;s logged
    */
	ac := client.Alias()
	ac.Add(&quot;myIndex&quot;, &quot;myAlias&quot;).Do(context.Background())
}

And here is the output:

reqDump: POST /_aliases HTTP/1.1
Host: 127.0.0.1:9200
Accept: application/json
Authorization: Basic base64(&lt;user&gt;:&lt;pass&gt;)
Content-Type: application/json
User-Agent: elastic/7.0.32 (linux-amd64)

{&quot;actions&quot;:[{&quot;add&quot;:{&quot;alias&quot;:&quot;myAlias&quot;,&quot;index&quot;:&quot;myIndex&quot;}}]}

I assume the SetTraceLog may also be used, but I went for the known path.

huangapple
  • 本文由 发表于 2023年4月18日 20:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76044654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定