-
Notifications
You must be signed in to change notification settings - Fork 825
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
feature(trace): adds named tracer factory #420
Changes from 8 commits
09835be
7f9d1bb
0f2f443
bbe8767
5b01c76
b3e5508
4d30e58
191eb4a
2930934
527f0be
d8524db
868a88e
1e7f7f8
0324f3e
8695501
b74e44a
8beea48
05abd69
9150295
4528fab
af4e624
6c1e42d
8e9428a
27f16b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
'use strict'; | ||
|
||
const opentelemetry = require('@opentelemetry/core'); | ||
const { NodeTracer } = require('@opentelemetry/node'); | ||
const { NodeTracerFactory } = 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 || ''; | ||
|
||
function setupTracerAndExporters(service) { | ||
const tracer = new NodeTracer({ | ||
const factory = new NodeTracerFactory({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instance? |
||
plugins: { | ||
grpc: { | ||
enabled: true, | ||
|
@@ -17,6 +17,7 @@ function setupTracerAndExporters(service) { | |
} | ||
} | ||
}); | ||
const tracer = factory.getTracer(); | ||
|
||
let exporter; | ||
if (EXPORTER.toLowerCase().startsWith('z')) { | ||
|
@@ -33,8 +34,8 @@ function setupTracerAndExporters(service) { | |
|
||
tracer.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
|
||
// Initialize the OpenTelemetry APIs to use the BasicTracer bindings | ||
opentelemetry.initGlobalTracer(tracer); | ||
// Initialize the OpenTelemetry APIs to use the BasicTracerFactory bindings | ||
opentelemetry.initGlobalTracerFactory(factory); | ||
} | ||
|
||
exports.setupTracerAndExporters = setupTracerAndExporters; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*! | ||
* Copyright 2019, 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. | ||
*/ | ||
|
||
import * as types from '@opentelemetry/types'; | ||
import { NoopTracer } from './NoopTracer'; | ||
|
||
export class NoopTracerFactory implements types.TracerFactory { | ||
bg451 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private readonly _tracer: types.Tracer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not omit constructor entirely and do: private readonly _tracer = new NoopTracer(); |
||
|
||
constructor() { | ||
this._tracer = new NoopTracer(); | ||
} | ||
|
||
getTracer(name?: string, version?: string): types.Tracer { | ||
return this._tracer; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*! | ||
* Copyright 2019, 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. | ||
*/ | ||
|
||
import * as types from '@opentelemetry/types'; | ||
import { NoopTracerFactory } from './NoopTracerFactory'; | ||
|
||
// Acts a bridge to the global tracer factory that can be safely called before the | ||
// global tracer factory is initialized. The purpose of the delegation is to avoid the | ||
// sometimes nearly intractable initialization order problems that can arise in | ||
// applications with a complex set of dependencies. Also allows for the factory | ||
// to be changed/disabled during runtime without needing to change reference | ||
// to the global factory. | ||
export class TracerFactoryDelegate implements types.TracerFactory { | ||
private _currentTracerFactory: types.TracerFactory; | ||
private readonly _tracerFactory: types.TracerFactory | null; | ||
private readonly _fallbackTracerFactory: types.TracerFactory; | ||
|
||
// Wrap a TracerFactory with a TracerDelegateFactory. Provided factory becomes the default | ||
// fallback factory for when a global factory has not been initialized | ||
constructor( | ||
tracerFactory?: types.TracerFactory, | ||
fallbackTracerFactory?: types.TracerFactory | ||
) { | ||
this._tracerFactory = tracerFactory || null; | ||
this._fallbackTracerFactory = | ||
fallbackTracerFactory || new NoopTracerFactory(); | ||
this._currentTracerFactory = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we have here |
||
this._tracerFactory || this._fallbackTracerFactory; // equivalent to this.start() | ||
} | ||
|
||
// Begin using the user provided tracer factory. Stop always falling back to fallback tracer | ||
// factory. | ||
start(): void { | ||
this._currentTracerFactory = | ||
this._tracerFactory || this._fallbackTracerFactory; | ||
} | ||
|
||
// Stop the delegate from using the provided tracer factory. Begin to use the fallback factory | ||
stop(): void { | ||
this._currentTracerFactory = this._fallbackTracerFactory; | ||
} | ||
|
||
// -- TracerFactory interface implementation below -- // | ||
getTracer(name?: string, version?: string): types.Tracer { | ||
return this._currentTracerFactory.getTracer.apply( | ||
bg451 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this._currentTracerFactory, | ||
// tslint:disable-next-line:no-any | ||
arguments as any | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/*! | ||
* Copyright 2019, 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. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import { NoopTracerFactory } from '../../src/trace/NoopTracerFactory'; | ||
import { NOOP_SPAN } from '../../src/trace/NoopSpan'; | ||
|
||
describe('NoopTracerFactory', () => { | ||
it('should return a NOOP_SPAN', () => { | ||
const factory = new NoopTracerFactory(); | ||
const span = factory.getTracer().startSpan('my-span'); | ||
assert.deepStrictEqual(span, NOOP_SPAN); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use
BasicTracerFactory.instance()
instead of directly callingnew
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this (initialization of global tracer factory and
BasicTracerFactory
) seems a little burdensome for the end users. I was thinking something like this:The
getTracerFactory
function is responsible for returning the correspondingTracerFactory
and we should create (and assign) theTracerFactory
instance statically once you load the library (maybe inindex.ts
). WDYT?