@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); }
@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(); }
.baseUri(managementUri) .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)
@Test public void testPollsAndParsesHttpErrorResponseLocal() throws Exception { int unboundPort = Networking.nextAvailablePort(10000); feed = HttpFeed.builder() .entity(entity) .baseUri("http://localhost:" + unboundPort + "/path/should/not/exist") .poll(new HttpPollConfig<String>(SENSOR_STRING) .onSuccess(Functions.constant("success")) .onFailure(Functions.constant("failure")) .onException(Functions.constant("error"))) .build(); assertSensorEventually(SENSOR_STRING, "error", TIMEOUT_MS); }
HttpPollConfig<T> pollConfig = new HttpPollConfig<T>(sensor) .checkSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant((T) null)) .onSuccess(successFunction) .suppressDuplicates(Boolean.TRUE.equals(suppressDuplicates)) .period(period);
.credentialsIfNotNull(config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .header("Accept", "application/json") .poll(new HttpPollConfig<Boolean>(SERVICE_UP) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), new Function<JsonElement, Boolean>() { @Override .onFailureOrException(Functions.constant(managed ? Boolean.FALSE : true))) .poll(new HttpPollConfig<Long>(TASK_STARTED_AT) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), new Function<JsonElement, Long>() { @Override .onFailureOrException(Functions.<Long>constant(-1L))) .poll(new HttpPollConfig<Long>(TASK_STAGED_AT) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), new Function<JsonElement, Long>() { @Override .onFailureOrException(Functions.<Long>constant(-1L))) .poll(new HttpPollConfig<String>(Attributes.HOSTNAME) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), new Function<JsonElement, String>() { @Override
@Override public void connectSensors() { super.connectSensors(); connectServiceUpIsRunning(); HostAndPort hostAndPort = BrooklynAccessUtils.getBrooklynAccessibleAddress(this, sensors().get(DOCKER_REGISTRY_PORT)); sensors().set(Attributes.MAIN_URI, URI.create("https://" + hostAndPort + "/v2")); httpFeed = HttpFeed.builder() .entity(this) .period(Duration.seconds(3)) .baseUri(getAttribute(Attributes.MAIN_URI)) .poll(new HttpPollConfig<Boolean>(Attributes.SERVICE_UP) .onSuccess(Functions.constant(true)) .onFailureOrException(Functions.constant(false))) .poll(new HttpPollConfig<List<String>>(DOCKER_REGISTRY_CATALOG) .suburl("/_catalog") .onSuccess(Functionals.chain( HttpValueFunctions.jsonContents(), JsonFunctions.walk("repositories"), JsonFunctions.forEach(JsonFunctions.cast(String.class)))) .onFailureOrException(Functions.constant(Collections.<String>emptyList()))) .build(); }
private void connectAuthenticatedSensors() { hostsHttpFeed = HttpFeed.builder() .entity(this) .period(1000, TimeUnit.MILLISECONDS) .baseUri(String.format("%s/api/v1/hosts", ambariUri)) .credentials(usernamePasswordCredentials.getUserName(), usernamePasswordCredentials.getPassword()) .header(HttpHeaders.AUTHORIZATION, HttpTool.toBasicAuthorizationValue(usernamePasswordCredentials)) .poll(new HttpPollConfig<List<String>>(REGISTERED_HOSTS) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), getHosts())) .onFailureOrException(Functions.<List<String>>constant(ImmutableList.<String>of()))) .build(); clusterHttpFeed = HttpFeed.builder() .entity(this) .period(1000, TimeUnit.MILLISECONDS) .baseUri(String.format("%s/api/v1/clusters/%s/requests/%d", ambariUri, getConfig(AmbariCluster.CLUSTER_NAME), 1)) .credentials(usernamePasswordCredentials.getUserName(), usernamePasswordCredentials.getPassword()) .header(HttpHeaders.AUTHORIZATION, HttpTool.toBasicAuthorizationValue(usernamePasswordCredentials)) .poll(new HttpPollConfig<String>(CLUSTER_STATE) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), getRequestState())) .onFailureOrException(Functions.<String>constant(null))) .build(); }
.baseUri(webConsoleUri) .credentialsIfNotNull(getConfig(MANAGEMENT_USER), getConfig(MANAGEMENT_PASSWORD)) .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); }}); }
@Test public void testSetsConnectionTimeout() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .connectionTimeout(Duration.TEN_SECONDS) .socketTimeout(Duration.TEN_SECONDS) .onSuccess(HttpValueFunctions.responseCode())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); }
@Override public void connectSensors() { super.connectSensors(); ConfigToAttributes.apply(this); // "up" is defined as returning a valid HTTP response from nginx (including a 404 etc) httpFeed = addFeed(HttpFeed.builder() .uniqueTag("nginx-poll") .entity(this) .period(getConfig(HTTP_POLL_PERIOD)) .baseUri(new UrlInferencer()) .poll(new HttpPollConfig<String>(SENSOR_STRING) .onResult(HttpValueFunctions.stringContentsFunction()) .setOnException("Failed") .suppressDuplicates(true)) .build()); }
@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); }
if (!config.isEnabled()) continue; @SuppressWarnings({ "unchecked", "rawtypes" }) HttpPollConfig<?> configCopy = new HttpPollConfig(config); if (configCopy.getPeriod() < 0) configCopy.period(builder.period); String method = config.getMethod(); Map<String,String> headers = config.buildHeaders(baseHeaders); byte[] body = config.getBody(); Duration connectionTimeout = config.getConnectionTimeout(); Duration socketTimeout = config.getSocketTimeout(); throw new IllegalStateException("Not permitted to supply baseUri and baseUriProvider"); Map<String,String> baseUriVars = ImmutableMap.copyOf(checkNotNull(builder.baseUriVars, "baseUriVars")); URI uri = config.buildUri(builder.baseUri, baseUriVars); baseUriProvider = Suppliers.ofInstance(uri); } else if (!builder.baseUriVars.isEmpty()) {
.entity(this) .baseUri(url) .poll(new HttpPollConfig<String>(SERVER_NAME) .onSuccess(HttpValueFunctions.jsonContents("name", String.class))) .poll(new HttpPollConfig<Integer>(SERVER_STATUS) .onSuccess(HttpValueFunctions.jsonContents("status", Integer.class))) .poll(new HttpPollConfig<Boolean>(SERVER_OK) .onSuccess(HttpValueFunctions.jsonContents("ok", Boolean.class))) .poll(new HttpPollConfig<String>(SERVER_BUILD_TIMESTAMP) .onSuccess(HttpValueFunctions.jsonContents(new String[]{"version", "build_timestamp"}, String.class))) .poll(new HttpPollConfig<String>(SERVER_BUILD_HASH) .onSuccess(HttpValueFunctions.jsonContents(new String[]{"version", "build_hash"}, String.class))) .poll(new HttpPollConfig<Boolean>(SERVER_IS_BUILD_SNAPSHOT) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "build_snapshot"}, Boolean.class))) .poll(new HttpPollConfig<String>(SERVER_LUCENE_VERSION) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "lucene_version"}, String.class))) .poll(new HttpPollConfig<String>(SERVER_ES_VERSION) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "es_version"}, String.class))) .build();
private void newMultiFeed(URL baseUrl) { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(HttpPollConfig.forMultiple() .onSuccess(new Function<HttpToolResponse,Void>() { @Override public Void apply(HttpToolResponse response) { entity.sensors().set(SENSOR_INT, response.getResponseCode()); if (response.getResponseCode()==200) entity.sensors().set(SENSOR_STRING, response.getContentAsString()); return null; } }) .onFailureOrException(Functionals.function(EntityFunctions.settingSensorsConstant(entity, MutableMap.<AttributeSensor<?>,Object>of( SENSOR_INT, -1, SENSOR_STRING, PollConfig.REMOVE)))) .period(100)) .build(); }
@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); }
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); } }
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)); }
public static HttpPollConfig<Void> forMultiple() { return new HttpPollConfig<Void>(PollConfig.NO_SENSOR); }