# APM and Ghost blog

**URL:** <https://discuss.elastic.co/t/apm-and-ghost-blog/113720>\
**Category:** APM\
**Created:** [January 2, 2018, 4:41am UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720 "2018-01-02T04:41:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitchdavis](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@mitchdavis](https://discuss.elastic.co/u/mitchdavis)\
**Post date:** [January 2, 2018, 4:41am UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/1 "2018-01-02T04:41:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [January 2, 2018, 1:54pm UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/2 "2018-01-02T13:54:35Z")

</div>

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:

> <https://github.com/TryGhost/Ghost/blob/ae741b1a18ef36f9170d13bf0d2e1179de91edc0/index.js#L3>

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

---

<div class="post-metadata">

**Author:** ![mitchdavis](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@mitchdavis](https://discuss.elastic.co/u/mitchdavis)\
**Post date:** [January 2, 2018, 4:11pm UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/4 "2018-01-02T16:11:09Z")

</div>

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:

```auto
// # 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);
});

```

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [January 2, 2018, 5:55pm UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/5 "2018-01-02T17:55:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mitchdavis](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@mitchdavis](https://discuss.elastic.co/u/mitchdavis)\
**Post date:** [January 2, 2018, 6:44pm UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/6 "2018-01-02T18:44:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [January 3, 2018, 1:36pm UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/7 "2018-01-03T13:36:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mitchdavis](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@mitchdavis](https://discuss.elastic.co/u/mitchdavis)\
**Post date:** [January 3, 2018, 4:08pm UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/8 "2018-01-03T16:08:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [January 4, 2018, 2:33pm UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/9 "2018-01-04T14:33:01Z")

</div>

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](https://www.ghostforbeginners.com/monitoring-ghost-with-new-relic/) 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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 25, 2018, 10:33am UTC](https://discuss.elastic.co/t/apm-and-ghost-blog/113720/10 "2018-01-25T10:33:12Z")

</div>

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