Skip to content

Commit

Permalink
feat: lint examples (#649)
Browse files Browse the repository at this point in the history
* refactor: use a single eslintrc for all examples folders

furthermore, override the strict rule that ships with airbnb

* fix: turn off no-use-before-define

* fix: install eslint in ci container

Co-Authored-By: Daniel Dyla <[email protected]>

* fix: ignore uninstalled packages lint errors

We will not want to install all examples in CI

Co-authored-by: Daniel Dyla <[email protected]>
Co-authored-by: Mayur Kale <[email protected]>
  • Loading branch information
3 people committed Jan 24, 2020
1 parent 230842f commit d7f4fe2
Show file tree
Hide file tree
Showing 45 changed files with 727 additions and 715 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ jobs:
root: packages/opentelemetry-types/docs
paths:
- out
- run:
name: Install minimal modules to lint examples
command: npm i -D eslint eslint-plugin-import eslint-config-airbnb-base
- run:
name: Lint examples
command: npm run lint-examples
docs-deploy:
docker:
- image: node:12
Expand Down
16 changes: 16 additions & 0 deletions examples/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"env": {
"node": true
},
"extends": "airbnb-base",
"parserOptions": {
"sourceType": "script"
},
"rules": {
"strict": ["error", "global"],
"no-use-before-define": ["error", "nofunc"],
"no-console": "off",
"import/no-unresolved": "off",
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
}
}
22 changes: 13 additions & 9 deletions examples/basic-tracer-node/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

const opentelemetry = require('@opentelemetry/core');
const { BasicTracerRegistry, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');
const { CollectorExporter } = require('@opentelemetry/exporter-collector');
const { CollectorExporter } = require('@opentelemetry/exporter-collector');

const options = {
serviceName: 'basic-service'
serviceName: 'basic-service',
};

// Initialize an exporter depending on how we were started
Expand All @@ -27,15 +29,15 @@ registry.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerRegistry bindings
opentelemetry.initGlobalTracerRegistry(registry);
const tracer = opentelemetry.getTracer('example-basic-tracer-node')
const tracer = opentelemetry.getTracer('example-basic-tracer-node');

// Create a span. A span must be closed.
const span = tracer.startSpan('main');
for (let i = 0; i < 10; i++) {
doWork(span);
const parentSpan = tracer.startSpan('main');
for (let i = 0; i < 10; i += 1) {
doWork(parentSpan);
}
// Be sure to end the span.
span.end();
parentSpan.end();

// flush and close the connection.
exporter.shutdown();
Expand All @@ -44,11 +46,13 @@ function doWork(parent) {
// Start another span. In this example, the main method already started a
// span, so that'll be the parent span, and this will be a child span.
const span = tracer.startSpan('doWork', {
parent: parent
parent,
});

// simulate some random work.
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i++) { }
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
// empty
}

// Set attributes to the span.
span.setAttribute('key', 'value');
Expand Down
31 changes: 19 additions & 12 deletions examples/basic-tracer-node/multi_exporter.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
'use strict';

const opentelemetry = require('@opentelemetry/core');
const { BasicTracerRegistry, BatchSpanProcessor, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');
const { CollectorExporter } = require('@opentelemetry/exporter-collector');
const { CollectorExporter } = require('@opentelemetry/exporter-collector');

const registry = new BasicTracerRegistry();

const zipkinExporter = new ZipkinExporter({serviceName: 'basic-service'});
const zipkinExporter = new ZipkinExporter({ serviceName: 'basic-service' });
const jaegerExporter = new JaegerExporter({
serviceName: 'basic-service',
});
const collectorExporter = new CollectorExporter({serviceName: 'basic-service'});
const collectorExporter = new CollectorExporter({ serviceName: 'basic-service' });

// It is recommended to use this BatchSpanProcessor for better performance
// and optimization, especially in production.
registry.addSpanProcessor(new BatchSpanProcessor(zipkinExporter, {
bufferSize: 10 // This is added for example, default size is 100.
// This is added for example, default size is 100.
bufferSize: 10,
}));

const tracer = opentelemetry.getTracer('default');

tracer.addSpanProcessor(new BatchSpanProcessor(jaegerExporter), {
bufferSize: 10
bufferSize: 10,
});

registry.addSpanProcessor(new SimpleSpanProcessor(collectorExporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerRegistry bindings
opentelemetry.initGlobalTracerRegistry(registry);
const tracer = opentelemetry.getTracer('default');

// Create a span. A span must be closed.
const span = tracer.startSpan('main');
for (let i = 0; i < 10; i++) {
doWork(span);
const parentSpan = tracer.startSpan('main');
for (let i = 0; i < 10; i += 1) {
doWork(parentSpan);
}
// Be sure to end the span.
span.end();
parentSpan.end();

// flush and close the connection.
zipkinExporter.shutdown();
Expand All @@ -44,11 +49,13 @@ function doWork(parent) {
// Start another span. In this example, the main method already started a
// span, so that'll be the parent span, and this will be a child span.
const span = tracer.startSpan('doWork', {
parent: parent
parent,
});

// simulate some random work.
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i++) { }
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
// empty
}

// Set attributes to the span.
span.setAttribute('key', 'value');
Expand Down
56 changes: 24 additions & 32 deletions examples/dns/client.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
'use strict';

const opentelemetry = require('@opentelemetry/core');
const config = require('./setup');

/**
* The trace instance needs to be initialized first, if you want to enable
* automatic tracing for built-in plugins (DNS in this case).
*/
config.setupTracerAndExporters('dns-client-service');

const tracer = require('./tracer')('example-dns');
// eslint-disable-next-line import/order
const dns = require('dns').promises;
const tracer = opentelemetry.getTracer('example-dns');

/** A function which makes a dns lookup and handles response. */
function makeLookup() {
// span corresponds to dns lookup. Here, we have manually created
// the span, which is created to track work that happens outside of the
// dns lookup query.
const span = tracer.startSpan('dnsLookup');
tracer.withSpan(span, async () => {
try {
await dns.lookup('montreal.ca');
} catch (error) {
span.setAttributes({
'error.name': error.name,
'error.message': error.message
});
}finally{
console.log(`traceid: ${span.context().traceId}`);
span.end();
}
});
// span corresponds to dns lookup. Here, we have manually created
// the span, which is created to track work that happens outside of the
// dns lookup query.
const span = tracer.startSpan('dnsLookup');
tracer.withSpan(span, async () => {
try {
await dns.lookup('montreal.ca');
} catch (error) {
span.setAttributes({
'error.name': error.name,
'error.message': error.message,
});
} finally {
console.log(`traceid: ${span.context().traceId}`);
span.end();
}
});

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

makeLookup();
39 changes: 0 additions & 39 deletions examples/dns/setup.js

This file was deleted.

40 changes: 40 additions & 0 deletions examples/dns/tracer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const opentelemetry = require('@opentelemetry/core');
const { NodeTracerRegistry } = require('@opentelemetry/node');
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');

const EXPORTER = process.env.EXPORTER || '';

module.exports = (serviceName) => {
const registry = new NodeTracerRegistry({
plugins: {
dns: {
enabled: true,
path: '@opentelemetry/plugin-dns',
// Avoid dns lookup loop with http zipkin calls
ignoreHostnames: ['localhost'],
},
},
});

let exporter;
if (EXPORTER.toLowerCase().startsWith('z')) {
exporter = new ZipkinExporter({
serviceName,
});
} else {
exporter = new JaegerExporter({
serviceName,
});
}

registry.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerRegistry bindings
opentelemetry.initGlobalTracerRegistry(registry);

return opentelemetry.getTracer();
};
21 changes: 7 additions & 14 deletions examples/grpc/client.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
'use strict';

const opentelemetry = require('@opentelemetry/core');
const config = require('./setup');

/**
* The trace instance needs to be initialized first, if you want to enable
* automatic tracing for built-in plugins (gRPC in this case).
*/
config.setupTracerAndExporters('grpc-client-service');

const tracer = require('./tracer')('example-grpc-client');
// eslint-disable-next-line import/order
const grpc = require('grpc');

const messages = require('./helloworld_pb');
const services = require('./helloworld_grpc_pb');

const PORT = 50051;
const tracer = opentelemetry.getTracer('example-grpc-client');

/** A function which makes requests and handles response. */
function main() {
Expand All @@ -26,17 +18,18 @@ function main() {
console.log('Client traceId ', span.context().traceId);
const client = new services.GreeterClient(
`localhost:${PORT}`,
grpc.credentials.createInsecure()
grpc.credentials.createInsecure(),
);
const request = new messages.HelloRequest();
let user;
if (process.argv.length >= 3) {
// eslint-disable-next-line prefer-destructuring
user = process.argv[2];
} else {
user = 'world';
}
request.setName(user);
client.sayHello(request, function(err, response) {
client.sayHello(request, (err, response) => {
span.end();
if (err) throw err;
console.log('Greeting:', response.getMessage());
Expand All @@ -46,7 +39,7 @@ function main() {
// 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.')
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
setTimeout(() => { console.log('Completed.'); }, 5000);
}

Expand Down
Loading

0 comments on commit d7f4fe2

Please sign in to comment.