opbeat / opbeat-node

DEPRECATED - See Elastic APM instead: https://github.com/elastic/apm-agent-nodejs
BSD 2-Clause "Simplified" License
153 stars 15 forks source link

No route information when using Hapi with Glue #113

Closed robinvdvleuten closed 6 years ago

robinvdvleuten commented 7 years ago

Hi there,

I am using Glue to compose my Hapi server instance, but somehow no route information is displayed this way :(

joshwiens commented 7 years ago

Glad it's not just me.

hapi: 16.1.0 glue: 4.1.0 opbeat: 4.4.0

screen shot 2016-12-31 at 10 59 26 pm

AdriVanHoudt commented 7 years ago

are you requiring opbeat before composing? I don't see a direct reason glue would mess with opbeat

joshwiens commented 7 years ago

@AdriVanHoudt I've tried it in both my index.js as well as my prod.config.js both of which are imported before the server composition. See below.

'use strict';

import Glue from 'glue';
import Chalk from 'chalk';
import Path from 'path';
import Labbable from 'labbable';
import Config from './config';

const ServerConfig = Config.server['jtx-node-server'];

const opbeat = require('opbeat').start({ // eslint-disable-line no-unused-vars
  appId: '',
  organizationId: '',
  secretToken: ''
});

ServerConfig.uri = (ServerConfig.tls
  ? 'https://'
  : 'http://') + `${ServerConfig.host}:${ServerConfig.port}`;

/**
 * Server Glue Plugin Manifest
 */
const manifest = {
  server: {
    app: Config
  },

  connections: [
    {
      host: ServerConfig.host,
      port: ServerConfig.port
    }
  ],

  // Registrations for global plugins only
  registrations: [
    {
      plugin: {
        register: 'hapi-auth-bearer-token'
      }
    }, {
      plugin: {
        register: 'hapi-auth-jwt'
      }
    }, {
      plugin: {
        register: 'good',
        options: Config.good
      }
    }, {
      plugin: {
        register: 'chairo',
        options: Config.chairo
      }
    }, {
      plugin: {
        register: 'inert'
      }
    }, {
      plugin: {
        register: 'vision'
      }
    }, {
      plugin: {
        register: 'hapi-swagger',
        options: Config.swagger
      }
    }, {
      plugin: {
        register: 'dogwater',
        options: Config.dogwater
      }
    }, {
      plugin: {
        register: '../lib'
      }
    }
  ]
};

/**
 * In Memory Cache Configuration
 */
if (Config.cache) {
  const caches = [];
  Object.keys(Config.cache).forEach((key) => {
    caches.push(Config.cache[key]);
  });
  manifest.server.cache = caches;
}

/**
 * Instantiates Labbable for testing
 */
const labbable = module.exports = new Labbable();

const opts = {
  relativeTo: Path.join(__dirname, 'node_modules')
};

/**
 * Compose and Initialize Hapi Server
 */
Glue.compose(manifest, opts, (err, server) => {
  if (err) {
    throw err;
  }

  labbable.using(server);

  const bootMessage = `

   ${Chalk.magenta.underline('JobTrak Server Initialized!')}\n
    - ${Chalk.blue(Config.product.name + ' is listening on')} ${Chalk.white.underline(ServerConfig.uri)}\n
    - ${Chalk.blue('Swagger Documentation at ')} ${Chalk.white.underline(ServerConfig.uri + '/docs')}\n
    - ${Chalk.red('Environment: ' + Config.env)}\n
  `;

  server.initialize((err) => {
    if (err) {
      throw err;
    }

    if (Config.auth) {
      server.auth.default(Config.auth);
    }

    server.start((err) => {
      if (err) {
        throw err;
      }
      setTimeout(() => {
        console.log(bootMessage);
      }, 2000);
    });
  });
});

I could make the boilerplate this server is based off of public and include opbeat if that would be helpful for troubleshooting.

watson commented 7 years ago

@d3viant0ne you need to call the start() function on the Opbeat agent before importing/requiring anything. If not, Opbeat will not have a chance to hook into Hapi. E.g:

'use strict';

const opbeat = require('opbeat').start();

import Glue from 'glue';
import Chalk from 'chalk';
import Path from 'path';
import Labbable from 'labbable';
import Config from './config';

@robinvdvleuten Can this be your issue as well?

robinvdvleuten commented 7 years ago

@watson I do have opbeat as first line of the application's entrypoint

robinvdvleuten commented 7 years ago

I can try to create a sandbox app with the issue later

watson commented 7 years ago

@robinvdvleuten thanks, that would be really helpful :)

joshwiens commented 7 years ago

In the end, you just have to get it imported before glue.

I was able to keep it in my prod config & then move the config imports above glue. The snippet below is posting routes as one would expect and limits opbeat to anything running in a production env.

'use strict';

import Config from './config';
const ServerConfig = Config.server['jtx-node-server'];

import Chalk from 'chalk';
import Glue from 'glue';
import Labbable from 'labbable';
import Path from 'path';
watson commented 7 years ago

@d3viant0ne as long as your ./config file isn't requiring (directly or indirectly) any of the modules listed in the MODULES array, then it's fine. Then you could even move Opbeat outside of your config module again and have it just below the import Config from './config'; line if you like.

Opbeat just needs to be required and started before any of those modules listed in the MODULES array are required.

joshwiens commented 7 years ago

No, it's just environment specific configuration values exported as a module, works like a charm :)

screen shot 2017-01-02 at 7 40 57 am

Arcrueid commented 6 years ago

same problem with hapi 17.1.1

watson commented 6 years ago

Opbeat has joined forces with Elastic and this agent is therefore deprecated. So I'll go ahead and close this issue.

I'm sorry that you encountered this error. Our new product is called Elastic APM, it's open source, and the Node.js agent used there is based off this agent. Large parts of the source code have been rewritten since then, so the bug might have been fixed there already. If you're interested in trying out Elastic APM, we'd love your feedback.