Deploy AppMon Node.js Agents on Cloud Foundry sample app

Overview

Cloud Foundry is an enterprise PaaS (Platform as a Service.) Cloud Foundry itself is an open source project, but a commercial distribution is available from Pivotal. Instructions for setting up a Cloud Foundry demo/development environment utilizing vagrant/virtualbox called bosh-lite is available here: https://github.com/cloudfoundry/bosh-lite

Requirements

Prepare a System Profile for Cloud Foundry Node.js applications

Node.js applications deployed on Cloud Foundry use the application name as the agent name. This example uses the agent name of node with the contains filter to match any application name containing node.

Create Cloud Foundry security group

The Cloud Foundry security group allows outbound connectivity to the AppMon Collector.

Create security.json

[
{
"protocol": "tcp",
"destination": "192.168.1.48",
"ports": "8042"
}
]

Create dynatrace_group

cf create-security-group dynatrace_group ./security.json

Apply the security group to Cloud Foundry

cf bind-running-security-group dynatrace_group
cf bind-staging-security-group dynatrace_group

Fetch the Node.js sample application

Clone source for cf-sample-app-nodejs sample application.

git clone https://github.com/cgfrost/cf-sample-app-nodejs.git

Fetch and extract the AppMon Node.js agent

The AppMon node.js agent can be retrieved from the Downloads area of the community portal.

Extract the AppMon Node.js agent inside of the application directory and remove the tarball archive:

tar -xvzf dynatrace-nodejsagent-6.2.0.1239-linux-x64.tgz
rm dynatrace-nodejsagent-6.2.0.1239-linux-x64.tgz

Configure the Node.js application to use the AppMon Agent

There are different ways to deploy the agent to the Node.js application. The configuration can be manually coded into the application or retrieved from the Cloud Foundry user provided service configured earlier.

Manual Configuration

Add the following lines near the beginning of the In the sample application's server.js file add the following lines towards the beginning of the file. Simply replace the IP address in the URL with IP of the AppMon Collector:

try {
    require('./dynatrace-6.2/agent/nodejs/nodejsagent.js')({server: 'http://192.168.1.48:8042', agentName: Sample_node_application});
} catch (err) {
    console.error(err.toString());
}

Use the following to deploy the application to Cloud Foundry.

cf push cf-sample-app-nodejs -b nodejs_buildpack

Cloud Foundry User Provided Service Configuration

This configuration uses the cfenv node package to fetch and parse Cloud Foundry environment variables.

Add the cfenv package to the node application by altering the dependencies section of the package.json as shown in the following example. This adds cfenv without a defined version.

  "dependencies": {
    "bower": "^1.3.12",
    "express": "4.12.x",
    "jade": "1.11.x",
    "less-middleware": "2.0.x",
    "cfenv": "\*"
  },

Next, edit the application's server.js file to include the lines of code needed to deploy the agent and configuration based on fetched environment variables managed by Cloud Foundry. Use cfenv to fetch the application's name from Cloud Foundry and to fetch the server address for the user provided service created earlier. Add the following lines at the top of the server.js file.

var cfenv = require("cfenv")
var appEnv = cfenv.getAppEnv()
var dynatraceEnv = appEnv.getServiceCreds(/cf-dynatrace-nodejs/)
var dynatraceServer = dynatraceEnv.server
try {
    require('./dynatrace-6.2/agent/nodejs/nodejsagent.js')({server: dynatraceServer, agentName: appEnv.name});
} catch (err) {
    console.error(err.toString());
}

Create a user provided service pointing the Cloud Foundry environment at the AppMon Collector. The collector is located at 192.168.1.48 in the following example.

cf create-user-provided-service cf-dynatrace-nodejs -p '{"server":"192.168.1.48:8042"}'

Deploy application to Cloud Foundry. Use the --no-start flag to attachto AppMon before starting the application.

cf push cf-sample-app-nodejs -b nodejs_buildpack --no-start

Cloud Foundry uses a bind mechanism to attach applications to external services. This is not necessary if using the manual configuration.

cf bind-service cf-sample-app-nodejs cf-dynatrace-nodejs

Start application

  • Use the following command to start the application.
cf start cf-sample-app-nodejs

At this point agents should be deployed along with the application. If the application is scaled to additional instances by Cloud Foundry new, new instances are automatically instrumented with AppMon.