Skip to content
Merged
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
26 changes: 26 additions & 0 deletions examples/router/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const baseConfig = require('../../eslint.config');

module.exports = {
...baseConfig,
env: {
node: true,
},
};
1 change: 1 addition & 0 deletions examples/router/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
53 changes: 39 additions & 14 deletions examples/router/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// required to initialize the service name for the auto-instrumentation
Expand All @@ -10,25 +26,34 @@ function makeRequest(path) {
// span corresponds to outgoing requests. Here, we have manually created
// the span, which is created to track work that happens outside of the
// request lifecycle entirely.
http.get({
host: 'localhost',
headers: {
accept: 'text/plain',
http.get(
{
host: 'localhost',
headers: {
accept: 'text/plain',
},
port: 8080,
path,
},
port: 8080,
path,
}, (response) => {
response.on('data', (chunk) => console.log(path, '::', chunk.toString('utf8')));
response.on('end', () => {
console.log(path, 'status', response.statusCode);
});
});
response => {
response.on('data', chunk =>
console.log(path, '::', chunk.toString('utf8'))
);
response.on('end', () => {
console.log(path, 'status', response.statusCode);
});
}
);

// The process must live for at least the interval past any traces that
// must be exported, or some risk being lost if they are recorded after the
// last export.
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
setTimeout(() => { console.log('Completed.'); }, 5000);
console.log(
'Sleeping 5 seconds before shutdown to ensure all records are flushed.'
);
setTimeout(() => {
console.log('Completed.');
}, 5000);
}

makeRequest('/hello/world');
Expand Down
2 changes: 2 additions & 0 deletions examples/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"description": "Example of router integration with OpenTelemetry",
"main": "index.js",
"scripts": {
"lint": "eslint . --ext=ts,js,mjs",
"lint:fix": "eslint . --ext=ts,js,mjs --fix",
"zipkin:server": "cross-env EXPORTER=zipkin node ./server.js",
"zipkin:client": "cross-env EXPORTER=zipkin node ./client.js",
"jaeger:server": "cross-env EXPORTER=jaeger node ./server.js",
Expand Down
21 changes: 19 additions & 2 deletions examples/router/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

require('./tracer')('example-router-server');
Expand All @@ -16,7 +32,8 @@ const router = Router();
router.use(setDefaultName);

router.param('name', (req, res, next, name) => {
req.params.name = typeof name === 'string' ? name.toUpperCase() : req.defaultName;
req.params.name =
typeof name === 'string' ? name.toUpperCase() : req.defaultName;
next();
});

Expand All @@ -33,7 +50,7 @@ router.get('/err', function erroringRoute(req, res, next) {

// eslint-disable-next-line prefer-arrow-callback, func-names
const server = http.createServer(function (req, res) {
router(req, res, (error) => {
router(req, res, error => {
if (error) {
res.statusCode = 500;
} else {
Expand Down
38 changes: 27 additions & 11 deletions examples/router/tracer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const opentelemetry = require('@opentelemetry/api');
Expand All @@ -7,14 +23,19 @@ diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.VERBOSE);

const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-base');
const {
SimpleSpanProcessor,
ConsoleSpanExporter,
} = require('@opentelemetry/sdk-trace-base');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');

const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { RouterInstrumentation } = require('@opentelemetry/instrumentation-router');
const {
RouterInstrumentation,
} = require('@opentelemetry/instrumentation-router');

const Exporter = ((exporterParam) => {
const Exporter = (exporterParam => {
if (typeof exporterParam === 'string') {
const exporterString = exporterParam.toLowerCase();
if (exporterString.startsWith('z')) {
Expand All @@ -27,23 +48,18 @@ const Exporter = ((exporterParam) => {
return ConsoleSpanExporter;
})(process.env.EXPORTER);

module.exports = (serviceName) => {
module.exports = serviceName => {
const exporter = new Exporter({
serviceName,
});

const provider = new NodeTracerProvider({
spanProcessors: [
new SimpleSpanProcessor(exporter),
],
spanProcessors: [new SimpleSpanProcessor(exporter)],
});

registerInstrumentations({
tracerProvider: provider,
instrumentations: [
HttpInstrumentation,
RouterInstrumentation,
],
instrumentations: [HttpInstrumentation, RouterInstrumentation],
});

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
Expand Down