Skip to content

Commit

Permalink
feat(serverless-collector): added service name detection (#1468)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 authored Dec 3, 2024
1 parent d2996e1 commit 5491461
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
8 changes: 4 additions & 4 deletions example-apps/serverless-collector/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
'use strict';

const path = require('path');
const cors = require('cors');

const MODE = process.env.MODE || 'npm';
const APP_PORT = process.env.APP_PORT || 9191;
const DOWNSTREAM_URL = process.env.DOWNSTREAM_URL;

let packageToRequire = path.join(__dirname, '..', '..', '..', 'packages', 'serverless-collector');

if (MODE === 'npm') {
packageToRequire = '@instana/serverless-collector';
}

console.log(`Enabling @instana/serverless-collector (requiring ${packageToRequire})`);
require(packageToRequire);

const cors = require('cors');

const express = require('express');
const app = express();

Expand All @@ -45,5 +45,5 @@ app.get('/trace', (req, res) => {
});

app.listen(APP_PORT, () => {
console.log('Listening on port', APP_PORT);
console.log(`serverless-collector app started on port ${APP_PORT} in mode ${MODE}`);
});
19 changes: 15 additions & 4 deletions packages/serverless-collector/src/activate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,43 @@

const instanaCore = require('@instana/core');
const { backendConnector, consoleLogger } = require('@instana/serverless');

const identityProvider = require('./identity_provider');

const { tracing, util: coreUtil } = instanaCore;
const { normalizeConfig } = coreUtil;
const customMetrics = require('./metrics');

let logger = consoleLogger;

const config = normalizeConfig({});
config.logger = logger;

function init() {
async function init() {
// NOTE: We accept for `process.env.INSTANA_DEBUG` any string value - does not have to be "true".
if (process.env.INSTANA_DEBUG || process.env.INSTANA_LOG_LEVEL) {
logger.setLevel(process.env.INSTANA_DEBUG ? 'debug' : process.env.INSTANA_LOG_LEVEL);
}

// NOTE: This package will not collect metrics.
// NOTE: This package does not support autotracing.
// NOTE: This package does not support metrics.

try {
// NOTE: We have to call pre-init because we are running an async call to get the service name.
// The goal is to register our instrumentations as early as possible.
// Otherwise we can easily run into errors e.g. from `hasThePackageBeenInitializedTooLate`.
instanaCore.preInit();

const serviceName = await customMetrics.name(config, logger);
if (serviceName) {
config.serviceName = serviceName;
}

identityProvider.init();
backendConnector.init(identityProvider, logger, false, true, 950);
instanaCore.init(config, backendConnector, identityProvider);
tracing.activate();

logger.debug('@instana/serverless-collector initialized.');
logger.info('@instana/serverless-collector initialized.');

// eslint-disable-next-line no-unused-expressions
process.send && process.send('instana.serverless-collector.initialized');
Expand Down
11 changes: 11 additions & 0 deletions packages/serverless-collector/src/metrics/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* (c) Copyright IBM Corp. 2024
*/

'use strict';

module.exports = {
get name() {
return require('./name');
}
};
30 changes: 30 additions & 0 deletions packages/serverless-collector/src/metrics/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* (c) Copyright IBM Corp. 2024
*/

'use strict';

const instanaCore = require('@instana/core');

// NOTE:
// The service name will be "localhost" because the serverless collector does not support
// metrics. Metrics contain the package.json name. AFAIK there is currently
// no way to connect metrics with spans for **agentless non serverless** environments.
// https://jsw.ibm.com/browse/INSTA-3607
module.exports = (config, logger) => {
if (process.env.INSTANA_SERVICE_NAME) {
return process.env.INSTANA_SERVICE_NAME;
}

return new Promise(resolve => {
instanaCore.util.applicationUnderMonitoring.getMainPackageJsonStartingAtMainModule(config, (err, packageJson) => {
if (err) {
logger.debug('Failed to determine main package.json.', err);
return resolve();
}

logger.debug(`Found main package.json: ${packageJson.name}`);
resolve(packageJson.name);
});
});
};
1 change: 1 addition & 0 deletions packages/serverless/src/console_logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

'use strict';

// 30 = info
let minLevel = 30;

module.exports = exports = {
Expand Down

0 comments on commit 5491461

Please sign in to comment.