public static <T> Function<HttpToolResponse, T> jsonContents(String element, Class<T> expected) { return jsonContents(new String[] {element}, expected); }
public static Function<HttpToolResponse, Boolean> responseCodeEquals(final int... expected) { List<Integer> expectedList = Lists.newArrayList(); for (int e : expected) { expectedList.add(e); } return Functionals.chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.in(expectedList))); }
@Test public void testStringContents() throws Exception { assertEquals(HttpValueFunctions.stringContentsFunction().apply(response), body); }
@Override public void connectSensors() { super.connectSensors(); HttpFeed.Builder httpFeedBuilder = HttpFeed.builder() .entity(this) .period(2000, TimeUnit.MILLISECONDS) .baseUri(sensors().get(FRAMEWORK_URL)) .credentialsIfNotNull(config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .poll(HttpPollConfig.forSensor(MARATHON_APPLICATIONS) .suppressDuplicates(true) .suburl("/v2/apps/") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("apps"), JsonFunctions.forEach(JsonFunctions.<String>getPath("id")))) .onFailureOrException(Functions.constant(Arrays.asList(new String[0])))) .poll(HttpPollConfig.forSensor(MARATHON_VERSION) .suppressDuplicates(true) .suburl("/v2/info/") .onSuccess(HttpValueFunctions.jsonContents("version", String.class)) .onFailureOrException(Functions.constant(""))) .poll(HttpPollConfig.forSensor(SERVICE_UP) .suppressDuplicates(true) .suburl("/ping") .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(Boolean.FALSE))); httpFeed = httpFeedBuilder.build(); }
.credentials(getConfig(MANAGEMENT_USER), getConfig(MANAGEMENT_PASSWORD)) .poll(new HttpPollConfig<Integer>(MANAGEMENT_STATUS) .onSuccess(HttpValueFunctions.responseCode()) .suppressDuplicates(true)) .poll(new HttpPollConfig<Boolean>(MANAGEMENT_URL_UP) .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(false)) .suppressDuplicates(true)) .poll(new HttpPollConfig<Integer>(REQUEST_COUNT) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("requestCount", Integer.class)) .onFailureOrException(EntityFunctions.attribute(this, REQUEST_COUNT)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(ERROR_COUNT) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("errorCount", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(TOTAL_PROCESSING_TIME) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("processingTime", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(MAX_PROCESSING_TIME) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("maxTime", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Long>(BYTES_RECEIVED) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("bytesReceived", Long.class))
@Test public void testPollsAndParsesHttpGetResponse() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(HttpPollConfig.forSensor(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(HttpPollConfig.forSensor(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "{\"foo\":\"myfoo\"}", TIMEOUT_MS); }
if (Strings.isBlank(jsonPath)) { successFunction = (Function) HttpValueFunctions.stringContentsFunction(); } else { successFunction = HttpValueFunctions.<T>jsonContentsFromPath(jsonPath); .checkSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant((T) null)) .onSuccess(successFunction)
@Override protected void connectSensors() { super.connectSensors(); ConfigToAttributes.apply(this); HostAndPort accessible = BrooklynAccessUtils.getBrooklynAccessibleAddress(this, getHttpPort()); String nodeJsUrl = String.format("http://%s:%d", accessible.getHostText(), accessible.getPort()); if (isHttpMonitoringEnabled()) { LOG.info("Connecting to {}", nodeJsUrl); httpFeed = HttpFeed.builder() .entity(this) .baseUri(nodeJsUrl) .poll(new HttpPollConfig<Boolean>(SERVICE_UP) .suppressDuplicates(true) .suburl(getConfig(NodeJsWebAppService.SERVICE_UP_PATH)) .checkSuccess(Predicates.alwaysTrue()) .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .setOnException(false)) .build(); } else { connectServiceUpIsRunning(); } WebAppServiceMethods.connectWebAppServerPolicies(this); }
@Test public void testLatency() throws Exception { assertEquals(HttpValueFunctions.latency().apply(response), Long.valueOf(fullLatency)); } }
@Test public void testContainsHeader() throws Exception { assertTrue(HttpValueFunctions.containsHeader(headerName).apply(response)); assertFalse(HttpValueFunctions.containsHeader("wrong_header").apply(response)); }
.poll(new HttpPollConfig<Boolean>(WEB_CONSOLE_ACCESSIBLE) .suburl("/v1/server/healthy") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(Boolean.class))) .onFailure(HttpValueFunctions.responseCodeEquals(404)) .setOnException(false)) .poll(new HttpPollConfig<ManagementNodeState>(MANAGEMENT_NODE_STATE) .suburl("/v1/server/ha/state") .onSuccess(Functionals.chain(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(String.class)), Enums.fromStringFunction(ManagementNodeState.class))) .setOnFailureOrException(null))
@Test(groups = {"Integration"}) public void testPollsAndParsesHttpGetResponseWithSsl() throws Exception { httpService = new HttpService(PortRanges.fromString("9000+"), true).start(); URI baseUrl = new URI(httpService.getUrl()); assertEquals(baseUrl.getScheme(), "https", "baseUrl="+baseUrl); feed = HttpFeed.builder() .entity(entity) .baseUri(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 200); Asserts.succeedsEventually(new Runnable() { @Override public void run() { String val = entity.getAttribute(SENSOR_STRING); assertTrue(val != null && val.contains("Hello, World"), "val="+val); }}); }
.baseUri(ambariUri) .poll(new HttpPollConfig<Boolean>(URL_REACHABLE) .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(false))) .build();
protected void initialize() { checkState(getConfig(URL) != null ^ getConfig(URL_SENSOR) != null, "Must set exactly one of url or urlSensor: url=%s; urlSensor=%s", getConfig(URL), getConfig(URL_SENSOR)); checkState(getConfig(URL_SENSOR) != null || getConfig(URL_POST_PROCESSING) == null, "Must not set urlPostProcessing without urlSensor"); Object configValue = getConfig(URL); if (configValue != null) { url.set(configValue.toString()); } httpFeed = HttpFeed.builder() .entity(entity) .period(getConfig(PERIOD)) .baseUri(Suppliers.compose(Urls.stringToUriFunction(), AtomicReferences.supplier(url))) .poll(new HttpPollConfig<Double>(REQUEST_LATENCY_IN_SECONDS_MOST_RECENT) .onResult(MathFunctions.divide(HttpValueFunctions.latency(), 1000.0d)) .setOnException(null)) .suspended() .build(); if (getUniqueTag()==null) uniqueTag = JavaClassNames.simpleClassName(getClass())+":"+ (getConfig(URL)!=null ? getConfig(URL) : getConfig(URL_SENSOR)); }
.onResult(HttpValueFunctions.containsHeader("Server")) .setOnException(false) .suppressDuplicates(true))
public static <T> Function<HttpToolResponse, T> jsonContentsFromPath(String path){ return Functionals.chain(jsonContents(), JsonFunctions.<T>getPath(path)); }
.poll(HttpPollConfig.forSensor(SERVICE_UP) .suburl("/master/health") .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(Boolean.FALSE))) .poll(HttpPollConfig.forSensor(CLUSTER_NAME) .poll(HttpPollConfig.forSensor(MEMORY_TOTAL_BYTES) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("mem_total_bytes", Long.class)) .onFailureOrException(Functions.constant(-1L))); httpFeed = httpFeedBuilder.build(); .description("Scan Cluster Slaves") .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("slaves"), new Function<JsonElement, List<String>>() { @Override public List<String> apply(JsonElement slaves) {
@Test public void testPollsAndParsesHttpPostResponse() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .method("post") .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .method("post") .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "{\"foo\":\"myfoo\"}", TIMEOUT_MS); }
protected void connectSensors() { URI webConsoleUri = getManagementContext().getManagementNodeUri().orNull(); sensors().set(WEB_CONSOLE_URI, webConsoleUri); if (webConsoleUri != null) { httpFeed = HttpFeed.builder() .entity(this) .period(200) .baseUri(webConsoleUri) .credentialsIfNotNull(getConfig(MANAGEMENT_USER), getConfig(MANAGEMENT_PASSWORD)) .poll(new HttpPollConfig<Boolean>(SERVICE_UP) .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .setOnFailureOrException(false)) .build(); } else { sensors().set(SERVICE_UP, true); } }
public static Function<HttpToolResponse, Boolean> responseCodeEquals(final int expected) { return Functionals.chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.equalTo(expected))); }