ReportClient { public void report(long systemTimeMillis, Table<String, Map<String, String>, Object> collectedData) { Request request = preparePost() .setUri(UPLOAD_URI) .setHeader("Content-Type", "application/gzip") .setBodySource(new CompressBodySource(systemTimeMillis, collectedData)) .build(); try { StringResponse response = httpClient.execute(request, createStringResponseHandler()); if (response.getStatusCode() != 204) { logger.warn("Failed to report stats: %s %s %s", response.getStatusCode(), response.getStatusMessage(), response.getBody()); } } catch (RuntimeException e) { logger.warn(e, "Exception when trying to report stats"); } } @Inject  ReportClient(NodeInfo nodeInfo, @ForReportClient HttpClient httpClient, ReportClientConfig reportClientConfig, ReportTagConfig reportTagConfig, ObjectMapper objectMapper); void report(long systemTimeMillis, Table<String, Map<String, String>, Object> collectedData);  }
@Test public void testReportingDisabled() { httpClient = new TestingHttpClient(); ReportClient client = new ReportClient(nodeInfo, httpClient, new ReportClientConfig().setEnabled(false), new ReportTagConfig(), objectMapper); client.report(System.currentTimeMillis(), collectedData); }
@Test public void testReportData() { ReportClient client = new ReportClient(nodeInfo, httpClient, new ReportClientConfig(), new ReportTagConfig(), objectMapper); client.report(TEST_TIME, collectedData); assertEquals(sentJson.size(), 2); for (Map<String, Object> map : sentJson) { assertEquals(map.keySet(), ImmutableSet.of("name", "timestamp", "value", "tags")); assertEquals(map.get("timestamp"), TEST_TIME); Map<String, String> tags = (Map<String, String>) map.get("tags"); assertEquals(tags.get("application"), "test-application"); assertEquals(tags.get("host"), "test.hostname"); assertEquals(tags.get("environment"), "test_environment"); assertEquals(tags.get("pool"), "test_pool"); } assertEquals(sentJson.get(0).get("name"), "Foo.Ba_r.Size"); assertEquals(sentJson.get(1).get("name"), "Foo.Size"); assertEquals(sentJson.get(0).get("value"), 1.2); assertEquals(sentJson.get(1).get("value"), 1.1); Map<String, String> tags = (Map<String, String>) sentJson.get(0).get("tags"); assertEquals(tags.keySet(), ImmutableSet.of("application", "host", "environment", "pool", "tag1")); assertEquals(tags.get("tag1"), "B_a_z"); tags = (Map<String, String>) sentJson.get(1).get("tags"); assertEquals(tags.keySet(), ImmutableSet.of("application", "host", "environment", "pool")); }
@Test public void testReportString() { ReportClient client = new ReportClient(nodeInfo, httpClient, new ReportClientConfig(), new ReportTagConfig(), objectMapper); collectedData = HashBasedTable.create(); collectedData.put("Foo.String", ImmutableMap.of(), "test value"); client.report(TEST_TIME, collectedData); assertEquals(sentJson, ImmutableList.of( ImmutableMap.of( "name", "Foo.String", "timestamp", TEST_TIME, "type", "string", "value", "test value", "tags", ImmutableMap.of( "application", "test-application", "host", "test.hostname", "environment", "test_environment", "pool", "test_pool" ) ) )); }
@Test public void testConfiguredTags() { ReportClient client = new ReportClient(nodeInfo, httpClient, new ReportClientConfig(), new ReportTagConfig() .setTags(ImmutableMap.of("foo", "ba:r", "baz", "quux")), objectMapper); client.report(TEST_TIME, collectedData); assertEquals(sentJson.size(), 2); for (Map<String, Object> map : sentJson) { assertEquals(map.keySet(), ImmutableSet.of("name", "timestamp", "value", "tags")); Map<String, String> tags = (Map<String, String>) map.get("tags"); assertEquals(tags.get("foo"), "ba:r"); assertEquals(tags.get("baz"), "quux"); } Map<String, String> tags = (Map<String, String>) sentJson.get(0).get("tags"); assertEquals(tags.keySet(), ImmutableSet.of("application", "host", "environment", "pool", "foo", "baz", "tag1")); tags = (Map<String, String>) sentJson.get(1).get("tags"); assertEquals(tags.keySet(), ImmutableSet.of("application", "host", "environment", "pool", "foo", "baz")); }
@Test public void testNoReportHost() { ReportClient client = new ReportClient(nodeInfo, httpClient, new ReportClientConfig().setPulseIncludeHostTag(false), new ReportTagConfig() .setTags(ImmutableMap.of("foo", "ba:r", "baz", "quux")), objectMapper); client.report(TEST_TIME, collectedData); assertEquals(sentJson.size(), 2); for (Map<String, Object> map : sentJson) { assertEquals(map.keySet(), ImmutableSet.of("name", "timestamp", "value", "tags")); Map<String, String> tags = (Map<String, String>) map.get("tags"); assertEquals(tags.get("foo"), "ba:r"); assertEquals(tags.get("baz"), "quux"); } Map<String, String> tags = (Map<String, String>) sentJson.get(0).get("tags"); assertEquals(tags.keySet(), ImmutableSet.of("application", "environment", "pool", "foo", "baz", "tag1")); tags = (Map<String, String>) sentJson.get(1).get("tags"); assertEquals(tags.keySet(), ImmutableSet.of("application", "environment", "pool", "foo", "baz")); }