Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: create a test app for OpenTelemetry work #1202

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions packages/opentelemetry/lib/config/instrumentations.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,41 @@ exports.createInstrumentationConfig = function createInstrumentationConfig() {
return [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-http': {
startOutgoingSpanHook(request) {
return {
['net.host.name']: `set-by-instrumentation-http-start-outgoing-span-hook: ${request.path}`,
['peer.service']: 'test-peer-service',
['peer.name']: 'test-peer-name'
};
},
startIncomingSpanHook() {
return {
['net.host.name']:
'set-by-instrumentation-http-start-incoming-span-hook',
['peer.service']: 'test-peer-service',
['peer.name']: 'test-peer-name'
};
},
ignoreIncomingRequestHook
},
'@opentelemetry/instrumentation-fs': {
enabled: false
},
'@opentelemetry/instrumentation-pino': {
enabled: false
},
'@opentelemetry/instrumentation-undici': {
startSpanHook(request) {
return {
['server.address']: `set-by-instrumentation-undici-start-span-hook: ${request.path}`,
['peer.service']: 'test-peer-service',
['peer.name']: 'test-peer-name'
};
}
}
}),
new RuntimeNodeInstrumentation()
})
// Commented out to avoid a lot of spam when testing
// new RuntimeNodeInstrumentation()
];
};

Expand Down
26 changes: 21 additions & 5 deletions packages/opentelemetry/lib/config/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,27 @@ exports.createMetricsConfig = function createMetricsConfig(options) {
headers['X-OTel-Key'] = options.apiGatewayKey;
}
config.metricReader = new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
url: options.endpoint,
compression: CompressionAlgorithm.GZIP,
headers
})
exportIntervalMillis: 5000,
exporter: {
// Test exporter to log metric names and attributes
export(metrics, done) {
metrics.scopeMetrics
.flatMap(({ metrics }) => metrics)
.map((metr) => ({
name: metr.descriptor.name,
attributes: metr.dataPoints.map((dp) => dp.attributes)
}))
.forEach((metric) => {
logger.info({
message: 'Exporting OpenTelementry metric',
metric
});
});
done({ code: 0 });
},
async forceFlush() {},
async shutdown() {}
}
});

logger.info({
Expand Down
17 changes: 9 additions & 8 deletions packages/opentelemetry/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ function setupOpenTelemetry({
};
instances.sdk.start();

// Set up host metrics if we have a metrics endpoint
if (metricsOptions?.endpoint) {
const meterProvider = /** @type {MeterProvider} */ (
opentelemetry.api.metrics.getMeterProvider()
);
instances.hostMetrics = new HostMetrics({ meterProvider });
instances.hostMetrics.start();
}
// Commented out to avoid a lot of spam when testing
// // Set up host metrics if we have a metrics endpoint
// if (metricsOptions?.endpoint) {
// const meterProvider = /** @type {MeterProvider} */ (
// opentelemetry.api.metrics.getMeterProvider()
// );
// instances.hostMetrics = new HostMetrics({ meterProvider });
// instances.hostMetrics.start();
// }

return instances;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/opentelemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
"@opentelemetry/instrumentation-runtime-node": "^0.7.0",
"@opentelemetry/sdk-node": "^0.53.0",
"@opentelemetry/semantic-conventions": "^1.26.0"
},
"devDependencies": {
"@types/express": "^4.17.21",
"express": "^4.21.0",
"node-fetch": "^2.6.7"
}
}
31 changes: 31 additions & 0 deletions packages/opentelemetry/test/end-to-end/fixtures/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const express = require('express');
const nodeFetch = require('node-fetch');

/**
* @returns {express.Application}
*/
exports.createTestApp = function createTestApp() {
const app = express();

app.get('/', async (request, response) => {
// Native fetch
const bulbasaur = await fetch(
'https://pokeapi.co/api/v2/pokemon/bulbasaur'
).then((response) => response.json());

// Node fetch
const squirtle = await nodeFetch(
'https://pokeapi.co/api/v2/pokemon/squirtle'
).then((response) => response.json());

response.send(
[
`Bulbasaur has the types: ${bulbasaur.types.map(({ type }) => type.name).join(', ')}`,
`Squirtle has the types: ${squirtle.types.map(({ type }) => type.name).join(', ')}`,
''
].join('\n')
);
});

return app;
};
12 changes: 12 additions & 0 deletions packages/opentelemetry/test/end-to-end/scripts/run-test-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { createTestApp } = require('../fixtures/app');
const logger = require('@dotcom-reliability-kit/logger');

const app = createTestApp();

const server = app.listen(4001, () => {
logger.info('Test app running on port 4001');
});

process.on('beforeExit', () => {
server.close();
});
22 changes: 22 additions & 0 deletions packages/opentelemetry/test/end-to-end/scripts/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { fork } = require('node:child_process');
const { join: joinPath } = require('node:path');

const cwd = __dirname;
const env = {
HEROKU_RELEASE_CREATED_AT: 'mock-release-date',
HEROKU_SLUG_COMMIT: 'mock-commit-hash',
LOG_LEVEL: 'debug',
OPENTELEMETRY_METRICS_ENDPOINT: 'http://localhost:4318/v1/metrics',
REGION: 'mock-region',
SYSTEM_CODE: 'reliability-kit/opentelemetry'
};

const app = fork(joinPath(cwd, 'run-test-app.js'), {
cwd,
env,
execArgv: ['--require', '@dotcom-reliability-kit/opentelemetry/setup']
});

app.on('close', (code) => {
process.exit(code);
});
Loading