|
9 | 9 |
|
10 | 10 | package org.elasticsearch.http;
|
11 | 11 |
|
| 12 | +import org.elasticsearch.common.Strings; |
12 | 13 | import org.elasticsearch.common.bytes.BytesArray;
|
| 14 | +import org.elasticsearch.common.bytes.BytesReference; |
13 | 15 | import org.elasticsearch.common.network.NetworkAddress;
|
14 | 16 | import org.elasticsearch.common.settings.ClusterSettings;
|
15 | 17 | import org.elasticsearch.common.settings.Settings;
|
16 | 18 | import org.elasticsearch.common.util.Maps;
|
| 19 | +import org.elasticsearch.common.xcontent.XContentHelper; |
17 | 20 | import org.elasticsearch.core.TimeValue;
|
18 | 21 | import org.elasticsearch.node.Node;
|
19 | 22 | import org.elasticsearch.rest.RestRequest;
|
|
22 | 25 | import org.elasticsearch.test.rest.FakeRestRequest;
|
23 | 26 | import org.elasticsearch.threadpool.DefaultBuiltInExecutorBuilders;
|
24 | 27 | import org.elasticsearch.threadpool.ThreadPool;
|
| 28 | +import org.elasticsearch.xcontent.ToXContent; |
| 29 | +import org.elasticsearch.xcontent.XContentFactory; |
| 30 | +import org.elasticsearch.xcontent.XContentType; |
25 | 31 |
|
| 32 | +import java.io.IOException; |
26 | 33 | import java.net.InetSocketAddress;
|
| 34 | +import java.time.Instant; |
27 | 35 | import java.util.HashMap;
|
28 | 36 | import java.util.List;
|
29 | 37 | import java.util.Map;
|
@@ -379,6 +387,55 @@ public void testClearsStatsIfDisabledConcurrently() throws InterruptedException
|
379 | 387 | }
|
380 | 388 | }
|
381 | 389 |
|
| 390 | + public void testToXContent() throws IOException { |
| 391 | + final var clientStats = new HttpStats.ClientStats( |
| 392 | + randomNonNegativeInt(), |
| 393 | + randomIdentifier(), |
| 394 | + randomIdentifier(), |
| 395 | + randomIdentifier(), |
| 396 | + randomIdentifier(), |
| 397 | + randomIdentifier(), |
| 398 | + randomIdentifier(), |
| 399 | + randomLongBetween(1, Long.MAX_VALUE), |
| 400 | + randomLongBetween(1, Long.MAX_VALUE), |
| 401 | + randomLongBetween(1, Long.MAX_VALUE), |
| 402 | + randomNonNegativeLong(), |
| 403 | + randomNonNegativeLong() |
| 404 | + ); |
| 405 | + final var description = Strings.toString(clientStats, false, true); |
| 406 | + logger.info("description: {}", description); |
| 407 | + |
| 408 | + final Map<String, Object> xcontentMap; |
| 409 | + try (var builder = XContentFactory.jsonBuilder()) { |
| 410 | + builder.humanReadable(true).value(clientStats, ToXContent.EMPTY_PARAMS); |
| 411 | + xcontentMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, XContentType.JSON).v2(); |
| 412 | + } |
| 413 | + |
| 414 | + assertEquals(description, clientStats.id(), xcontentMap.get("id")); |
| 415 | + assertEquals(description, clientStats.agent(), xcontentMap.get("agent")); |
| 416 | + assertEquals(description, clientStats.localAddress(), xcontentMap.get("local_address")); |
| 417 | + assertEquals(description, clientStats.remoteAddress(), xcontentMap.get("remote_address")); |
| 418 | + assertEquals(description, clientStats.lastUri(), xcontentMap.get("last_uri")); |
| 419 | + assertEquals(description, clientStats.forwardedFor(), xcontentMap.get("x_forwarded_for")); |
| 420 | + assertEquals(description, clientStats.opaqueId(), xcontentMap.get("x_opaque_id")); |
| 421 | + |
| 422 | + assertEquals(description, clientStats.openedTimeMillis(), xcontentMap.get("opened_time_millis")); |
| 423 | + assertEquals(description, Instant.ofEpochMilli(clientStats.openedTimeMillis()).toString(), xcontentMap.get("opened_time")); |
| 424 | + |
| 425 | + assertEquals(description, clientStats.closedTimeMillis(), xcontentMap.get("closed_time_millis")); |
| 426 | + assertEquals(description, Instant.ofEpochMilli(clientStats.closedTimeMillis()).toString(), xcontentMap.get("closed_time")); |
| 427 | + |
| 428 | + assertEquals(description, clientStats.lastRequestTimeMillis(), xcontentMap.get("last_request_time_millis")); |
| 429 | + assertEquals( |
| 430 | + description, |
| 431 | + Instant.ofEpochMilli(clientStats.lastRequestTimeMillis()).toString(), |
| 432 | + xcontentMap.get("last_request_time") |
| 433 | + ); |
| 434 | + |
| 435 | + assertEquals(description, clientStats.requestCount(), (long) xcontentMap.get("request_count")); |
| 436 | + assertEquals(description, clientStats.requestSizeBytes(), (long) xcontentMap.get("request_size_bytes")); |
| 437 | + } |
| 438 | + |
382 | 439 | private Map<String, String> getRelevantHeaders(HttpRequest httpRequest) {
|
383 | 440 | final Map<String, String> headers = Maps.newMapWithExpectedSize(4);
|
384 | 441 | final String[] relevantHeaderNames = new String[] { "user-agent", "x-elastic-product-origin", "x-forwarded-for", "x-opaque-id" };
|
|
0 commit comments