审计追踪

编辑

Elasticsearch.Net 和 NEST 为请求发出时请求管道中发生的事件提供了一个审计追踪。此审计追踪在响应中可用,如下例所示。

这里我们将使用嗅探连接池,因为它在启动时进行嗅探,并在首次使用前进行 ping 操作,因此我们可以获得包含一些事件的审计追踪

var pool = new SniffingConnectionPool(new []{ TestConnectionSettings.CreateUri() });
var connectionSettings = new ConnectionSettings(pool)
    .DefaultMappingFor<Project>(i => i
        .IndexName("project")
    );

var client = new ElasticClient(connectionSettings);

发出以下请求后

var response = client.Search<Project>(s => s
    .MatchAll()
);

审计追踪以人类可读的方式在调试信息中提供,类似于

Valid NEST response built from a successful low level call on POST: /project/doc/_search
# Audit trail of this API call:
 - [1] SniffOnStartup: Took: 00:00:00.0360264
 - [2] SniffSuccess: Node: https://127.0.0.1:9200/ Took: 00:00:00.0310228
 - [3] PingSuccess: Node: http://127.0.0.1:9200/ Took: 00:00:00.0115074
 - [4] HealthyResponse: Node: http://127.0.0.1:9200/ Took: 00:00:00.1477640
# Request:
<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>
# Response:
<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>

以帮助进行故障排除

var debug = response.DebugInformation;

但也可以手动访问

response.ApiCall.AuditTrail.Count.Should().Be(4, "{0}", debug);
response.ApiCall.AuditTrail[0].Event.Should().Be(SniffOnStartup, "{0}", debug);
response.ApiCall.AuditTrail[1].Event.Should().Be(SniffSuccess, "{0}", debug);
response.ApiCall.AuditTrail[2].Event.Should().Be(PingSuccess, "{0}", debug);
response.ApiCall.AuditTrail[3].Event.Should().Be(HealthyResponse, "{0}", debug);

每个审计都有一个开始和结束的 DateTime,可以提供对所用时间的一些了解

response.ApiCall.AuditTrail
    .Should().OnlyContain(a => a.Ended - a.Started >= TimeSpan.Zero);