Odil
Odil

Reputation: 81

Cloud Foundry, fetching app events from NATS message bus

I'm quite new in Cloud Foundry and was curious about one thing. I know that Cloud Foundry uses NATS as its messaging bus between jobs. Being connected to the CF's NATS boxes via Java NATS connector I managed to get a lot of useful information about applications deployed on CF, droplets information, Health Manager report and many more. One of my goals was to request "dea.find.droplet" subject to NATS which causes jobs to return apps' CPU, memory, and disk usage over time.

Now I have to get information about so called "events" from the NATS messages. Using CF CLI tools we can call "cf events APP_NAME" to get this information. A typicals response to "cf events" is something like this

2015-09-15T10:37:35.00-0400   audit.app.update    [email protected]              state: STARTED
2015-09-03T10:43:08.00-0400   audit.app.restage    troy
2015-08-26T14:26:58.00-0400   app.crash           s-platform          index: 1, reason: CRASHED, exit_description: failed to start, exit_status: -1

I already created a maven jar app which runs on remote server, which gathers all the NATS messages, filters them and pushes to the Elastic Search for further analysis.

String reqMessage = "{\"include_stats\": true,"
						//+ "\"states\": [\"RUNNING\"],"
						+ "\"version\": \"" + version + "\","
						+ "\"droplet\": \"" + droplet + "\"}";


public void requestAppStats(String subject, String reqMessage) throws JSONException{
    			// Requests
    			//subject: "dea.find.droplet",
    			natsConnector.request(subject, reqMessage, 1, TimeUnit.MINUTES, 1, new MessageHandler() {
    			    @Override
    			    public void onMessage(Message message) {
    			        //LOGGER.info("Got a response: {}", message);
    			    	try {
    			    		
    			    		if (new JSONObject(message.getBody()).has("stats")){
    						
    			    			updateAppMetaDataList(message.getBody());
    						}
    			    		
    					} catch (JSONException e) {
    						e.printStackTrace();
    					}
    			    }
    			});

There is a way to get "events" info from CCDB database instances, but this approach is pretty slow and puts extra burden on the application. So I was wondering is there a way to catch "events" info from NATS messages just like I did with app usage info like this?

natsConnector.request(subject, reqMessage);

As you can see in order to get apps' status, resource usage we have to request "dea.find.droplet" with JSON request message.

It would be nice to know what happens when I call "cf events APP_NAME" from CF CLI tools. I looked up the source code of CF CLI tools (https://github.com/cloudfoundry/cli), which brought me to the CloudController job (https://github.com/cloudfoundry/cloud_controller_ng). Looks like CloudController is in charge of returning app events info. This is pure speculation though.

Maybe at least I could emulate this step to integrate into my app. Any help would be appreciated.

Upvotes: 2

Views: 643

Answers (1)

crhino
crhino

Reputation: 559

Odil, you are right that the Cloud Controller is in charge of events. App events as defined by Cloud Foundry live entirely in the CC database, they are just recorded events when you update/delete/create/etc your app, so they never go through the NATs message bus. I would be interested to hear more specifics on how going through the API is slow.

In general, I wonder why you have a need to connect directly to NATs. App usage statistics can be displayed through the cf CLI using cf app [APPNAME]. The state of the app is also exposed through the API, e.g. RUNNING/CRASHED/STOPPED.

Edit:

Here is a compiled list of all NATs topics that CF uses inside the system. Be aware though, CF is slowly moving away from NATs usage, e.g. the new Diego runtime communicates over HTTP with the Cloud Controller to start/stop applications.

Upvotes: 2

Related Questions