APM and Ghost blog

Hi there,

I'm looking to integrate APM into my ghost installation, but am unaware of where to start. I have tried adding it to my index.js, Gruntfile.js and a few other files but Ghost fails to start after that. Has anyone successfully been able to get this working or have any suggestions?

I haven't tried to run Ghost my self, but looking at their source code, I think the only place you need to add the agent is in the index.js file in the root. This is based on the assumption that the index.js file is the first file to be run when Ghost is started (which it seems to be based on the start script in package.json). As far as I can see the code doesn't need transpiling either, which would mean a slightly different setup as well.

You need to add it before any of the existing require statements, e.g: before the require('ghost-ignition') call on lin 3:

If it still wont work, let me know any if possible show me a code snippet of how you start the agent :slight_smile:

Unfortunately, it doesn't seem to work when I add it to the index.js file, ghost fails to start. I added it like so:

// # Ghost Startup
// Orchestrates the startup of Ghost when run from command line.
// Add this to the VERY top of the first file loaded in your app
var apm = require('elastic-apm-node').start({
  // Set required app name (allowed characters: a-z, A-Z, 0-9, -, _, and space)
  appName: 'appName',

  // Use if APM Server requires a token
  //secretToken: '',

  // Set custom APM Server URL (default: http://localhost:8200)
  serverUrl: 'http://localhost:8200',
})

var startTime = Date.now(),
    debug = require('ghost-ignition').debug('boot:index'),
    ghost, express, common, urlService, parentApp;

debug('First requires...');

ghost = require('./core');

debug('Required ghost');

express = require('express');
common = require('./core/server/lib/common');
urlService = require('./core/server/services/url');
parentApp = express();

debug('Initialising Ghost');
ghost().then(function (ghostServer) {
    // Mount our Ghost instance on our desired subdirectory path if it exists.
    parentApp.use(urlService.utils.getSubdir(), ghostServer.rootApp);

    debug('Starting Ghost');
    // Let Ghost handle starting our server instance.
    return ghostServer.start(parentApp).then(function afterStart() {
        common.logging.info('Ghost boot', (Date.now() - startTime) / 1000 + 's');

        // if IPC messaging is enabled, ensure ghost sends message to parent
        // process on successful start
        if (process.send) {
            process.send({started: true});
        }
    });
}).catch(function (err) {
    if (!common.errors.utils.isIgnitionError(err)) {
        err = new common.errors.GhostError({err: err});
    }

    if (process.send) {
        process.send({started: false, error: err.message});
    }

    common.logging.error(err);
    process.exit(-1);
});

Weird. Does Ghost give any specific kind of error message when you do this, or does it simply not respond to HTTP requests any more?

The only error I can see from ghost is:

Stack: Error: Ghost instance is not currently running.
at RestartCommand.run (/usr/lib/node_modules/ghost-cli/lib/commands/restart.js:9:35)
at precheck.then (/usr/lib/node_modules/ghost-cli/lib/command.js:206:52)
at process._tickCallback (internal/process/next_tick.js:109:7)
at Module.runMain (module.js:606:11)
at run (bootstrap_node.js:383:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:496:3

Apart from that, it doesn't respond to HTTP requests.

I imagine you're trying this out locally in development? How are you starting the Ghost instance?

Yes I am, after I've added the APM agent code I use ghost cli and issue ghost stop, then ghost start.

While I have not been able to reproduce the problem of Ghost not starting up, it does seems like the Ghost CLI doesn't run the index.js file, but instead starts up Ghost in a slightly different manner. I would imagine though that the index.js file is used in production. If I run the index.js file manually using node current/index.js then it all works.

I've been trying to read the Ghost CLI source code to see how the ghost start command works, but got lost in several layers of abstraction. But as far as I can see reading other guides on how to instrument Ghost, the index.js file seems to be the correct place to add the code snippet.

If Ghost have a support forum, I'd try to ask there as this seems like an issue related to how Ghost works more than how to set up Elastic APM. I'd be happy to know your progress though, as this might make a good blog post or docs article once we figure out the right way to do it, so that others don't have to go through the same :slight_smile:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.