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

Moving Hapi Plugin to Instrumentation #380

Merged
merged 5 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions examples/hapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"@opentelemetry/api": "^0.18.0",
"@opentelemetry/exporter-jaeger": "^0.18.0",
"@opentelemetry/exporter-zipkin": "^0.18.0",
"@opentelemetry/hapi-instrumentation": "^0.13.1",
"@opentelemetry/instrumentation": "^0.18.0",
"@opentelemetry/instrumentation-hapi": "^0.13.1",
"@opentelemetry/instrumentation-http": "^0.18.0",
"@opentelemetry/node": "^0.18.0",
"@opentelemetry/plugin-http": "^0.18.0",
"@opentelemetry/tracing": "^0.18.0",
"axios": "^0.19.0"
},
Expand Down
8 changes: 5 additions & 3 deletions examples/hapi/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const tracer = require('./tracer')('example-hapi-server');
const api = require('@opentelemetry/api');
require('./tracer')('example-hapi-server');

// eslint-disable-next-line
const Hapi = require('@hapi/hapi');

Expand Down Expand Up @@ -62,7 +64,7 @@ const posts = ['post 0', 'post 1', 'post 2'];

function addPost(_, h) {
posts.push(`post ${posts.length}`);
const currentSpan = tracer.getCurrentSpan();
const currentSpan = api.getSpan(api.context.active());
currentSpan.addEvent('Added post');
currentSpan.setAttribute('Date', new Date());
console.log(`Added post: ${posts[posts.length - 1]}`);
Expand All @@ -80,7 +82,7 @@ async function showNewPost(request) {
}

function runTest(_, h) {
const currentSpan = tracer.getCurrentSpan();
const currentSpan = api.getSpan(api.context.active());
const { traceId } = currentSpan.context();
console.log(`traceid: ${traceId}`);
console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`);
Expand Down
30 changes: 15 additions & 15 deletions examples/hapi/tracer.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
'use strict';

const opentelemetry = require('@opentelemetry/api');
const api = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');
const { HapiInstrumentation } = require('@opentelemetry/instrumentation-hapi');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

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

module.exports = (serviceName) => {
const provider = new NodeTracerProvider({
plugins: {
'@hapi/hapi': {
enabled: true,
path: '@opentelemetry/hapi-instrumentation',
enhancedDatabaseReporting: true,
},
http: {
enabled: true,
path: '@opentelemetry/plugin-http',
},
},
});
const provider = new NodeTracerProvider();

let exporter;
if (EXPORTER === 'jaeger') {
Expand All @@ -34,5 +25,14 @@ module.exports = (serviceName) => {
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return opentelemetry.trace.getTracer('hapi-example');
registerInstrumentations({
instrumentations: [
new HapiInstrumentation({
enhancedDatabaseReporting: true,
}),
new HttpInstrumentation(),
],
});

return api.trace.getTracer('hapi-example');
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ For automatic instrumentation see the
## Installation

```bash
npm install --save @opentelemetry/hapi-instrumentation
npm install --save @opentelemetry/instrumentation-hapi
```
### Supported Versions
- @hapi/hapi `^17.0.0`
Expand All @@ -25,38 +25,42 @@ To load a specific instrumentation (Hapi in this case), specify it in the regist

```js
const { NodeTracerProvider } = require('@opentelemetry/node');
obecny marked this conversation as resolved.
Show resolved Hide resolved
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

const provider = new NodeTracerProvider();
provider.register();

const { registerInstrumentations } = require('@opentelemetry/instrumentation');
registerInstrumentations({
instrumentations: [
{
plugins: {
'@hapi/hapi': {
enabled: true,
// You may use a package name or absolute path to the file.
path: '@opentelemetry/hapi-instrumentation',
}
},
},
],
tracerProvider: provider,
});
```

To load all of the [supported instrumentations](https:/open-telemetry/opentelemetry-js#plugins), use below approach. Each instrumentation is only loaded when the module that it patches is loaded; in other words, there is no computational overhead for listing instrumentations for unused modules.
If instead you would just want to load a specific instrumentation only (**hapi** in this case);

```js
const { NodeTracerProvider } = require('@opentelemetry/node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { HapiInstrumentation } = require('@opentelemetry/instrumentation-hapi');
const provider = new NodeTracerProvider();
provider.register();

const hapiInstrumentation = new HapiInstrumentation();
hapiInstrumentation.setTracerProvider(provider);
```

You can combine loading default plugins and HapiInstrumentation at the same time:

```js
const { NodeTracerProvider } = require('@opentelemetry/node');
const { HapiInstrumentation } = require('@opentelemetry/instrumentation-hapi');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();

registerInstrumentations({
instrumentations: [
new HapiInstrumentation(),
],
tracerProvider: provider,
});

```

See [examples/hapi](https:/open-telemetry/opentelemetry-js-contrib/tree/main/examples/hapi) for a short example using Hapi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@opentelemetry/hapi-instrumentation",
"name": "@opentelemetry/instrumentation-hapi",
"version": "0.13.1",
"description": "OpenTelemetry Hapi automatic instrumentation package.",
"main": "build/src/index.js",
Expand Down Expand Up @@ -48,7 +48,6 @@
"@types/hapi__hapi": "20.0.1",
"@types/mocha": "7.0.2",
"@types/node": "12.12.47",
"@types/shimmer": "1.0.1",
"codecov": "3.7.0",
"gts": "3.1.0",
"mocha": "7.2.0",
Expand All @@ -63,8 +62,7 @@
},
"dependencies": {
"@opentelemetry/api": "^0.18.0",
"@opentelemetry/core": "^0.18.0",
"@opentelemetry/semantic-conventions": "^0.18.0",
"shimmer": "^1.2.1"
"@opentelemetry/instrumentation": "^0.18.0",
"@opentelemetry/semantic-conventions": "^0.18.0"
}
}
Loading