Skip to content

Commit

Permalink
feat(plugin): add http plugin
Browse files Browse the repository at this point in the history
Closes open-telemetry#157

Signed-off-by: Olivier Albertini <[email protected]>
  • Loading branch information
OlivierAlbertini committed Aug 5, 2019
1 parent 20c9a49 commit a99e08d
Show file tree
Hide file tree
Showing 10 changed files with 893 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-basic-tracer/src/BasicTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class BasicTracer implements types.Tracer {
private readonly _binaryFormat: types.BinaryFormat;
private readonly _httpTextFormat: types.HttpTextFormat;
private readonly _sampler: types.Sampler;
private readonly _scopeManager: ScopeManager;
protected readonly _scopeManager: ScopeManager;

/**
* Constructs a new Tracer instance.
Expand Down
11 changes: 11 additions & 0 deletions packages/opentelemetry-node-tracer/src/NodeTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ export class NodeTracer extends BasicTracer {

// @todo: Integrate Plugin Loader (pull/126).
}

/**
* Binds the trace context to the given event emitter.
* This is necessary in order to create child spans correctly in event
* handlers.
* @param emitter An event emitter whose handlers should have
* the trace context binded to them.
*/
wrapEmitter(emitter: NodeJS.EventEmitter, scope?: unknown): void {
this._scopeManager.bind(emitter, scope);
}
}
2 changes: 2 additions & 0 deletions packages/opentelemetry-node-tracer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export * from './NodeTracer';
37 changes: 37 additions & 0 deletions packages/opentelemetry-node-tracer/test/NodeTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '@opentelemetry/core';
import { AsyncHooksScopeManager } from '@opentelemetry/scope-async-hooks';
import { NodeTracer } from '../src/NodeTracer';
import { EventEmitter } from 'events';

describe('NodeTracer', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -180,4 +181,40 @@ describe('NodeTracer', () => {
assert.ok(tracer.getHttpTextFormat() instanceof HttpTraceContext);
});
});
describe.only('.wrapEmitter()', () => {
it('should not throw', () => {
const tracer = new NodeTracer({
scopeManager: new AsyncHooksScopeManager(),
});
tracer.wrapEmitter(undefined as any);
});
// TODO: remove skip onace https://github.com/open-telemetry/opentelemetry-js/pull/146 is merged
it.skip('should get current Span', (done) => {
const tracer = new NodeTracer({
scopeManager: new AsyncHooksScopeManager(),
});

const span = tracer.startSpan('my-span');
class FakeEventEmitter extends EventEmitter {
constructor() {
super()
}
test() {
this.emit('event');
}
}

tracer.withSpan(span, () => {
const fake = new FakeEventEmitter();
tracer.wrapEmitter(fake);
fake.on('event', () => {
setTimeout(() => {
assert.deepStrictEqual(tracer.getCurrentSpan(), span);
done();
}, 100);
});
fake.test();
});
});
});
});
13 changes: 12 additions & 1 deletion packages/opentelemetry-plugin-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,30 @@
"access": "public"
},
"devDependencies": {
"@opentelemetry/scope-async-hooks": "^0.0.1",
"@types/mocha": "^5.2.7",
"@types/node": "^12.6.9",
"@types/semver": "^6.0.1",
"@types/shimmer": "^1.0.1",
"@types/sinon":"^7.0.13",
"codecov": "^3.5.0",
"gts": "^1.1.0",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"sinon": "^7.3.2",
"c8": "^5.0.1",
"nock": "^10.0.6",
"ts-mocha": "^6.0.0",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
},
"dependencies": {
"@opentelemetry/core": "^0.0.1",
"@opentelemetry/node-tracer": "^0.0.1",
"@opentelemetry/types": "^0.0.1"
"@opentelemetry/types": "^0.0.1",
"@types/nock": "^10.0.3",
"nock": "^10.0.6",
"semver": "^6.3.0",
"shimmer": "^1.2.1"
}
}
Loading

0 comments on commit a99e08d

Please sign in to comment.