Skip to content

Commit

Permalink
Add API for registering a global instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Yeager committed Nov 11, 2021
1 parent 9a4dd53 commit 5ff2864
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 34 deletions.
3 changes: 2 additions & 1 deletion lib/sdk.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export 'src/sdk/common/attribute.dart' show Attribute;
export 'src/sdk/open_telemetry.dart' show OpenTelemetrySdk;
export 'src/sdk/open_telemetry.dart'
show registerGlobalTracerProvider, globalTracerProvider;
export 'src/sdk/trace/exporters/collector_exporter.dart' show CollectorExporter;
export 'src/sdk/trace/exporters/console_exporter.dart' show ConsoleExporter;
export 'src/sdk/trace/propagation/w3c_trace_context_propagator.dart'
Expand Down
41 changes: 11 additions & 30 deletions lib/src/sdk/open_telemetry.dart
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import 'package:opentelemetry/sdk.dart';
import '../../sdk.dart';

class OpenTelemetrySdk {
static final OpenTelemetrySdk _globalOpenTelemetry =
OpenTelemetrySdk._global();
TracerProvider _tracerProvider;
final TracerProvider _noopTracerProvider = TracerProvider();
TracerProvider _tracerProvider = _noopTracerProvider;

OpenTelemetrySdk._global();

/// Returns a new [OpenTelemetrySdk] and registers it as the global
/// [OpenTelemetrySdk]. Future calls to getter globalOpenTelemetry will return
/// the registered [OpenTelemetrySdk] instance.
///
/// This should be called once as early as possible in your application
/// initialization logic. A [StateError] will be thrown if this method is
/// attempted to be called multiple times in the lifecycle of an
/// application - ensure you have only one for use as the global instance.
factory OpenTelemetrySdk.global(TracerProvider tracerProvider) {
if (_globalOpenTelemetry._tracerProvider != null) {
throw StateError(
'A global instance of OpenTelemetry has already been created. '
'OpenTelemetry.global must be called only once before any calls '
'to getter globalOpenTelemetry.');
}

_globalOpenTelemetry._tracerProvider = tracerProvider;

return _globalOpenTelemetry;
void registerGlobalTracerProvider(TracerProvider tracerProvider) {
if (_tracerProvider != _noopTracerProvider) {
throw StateError('A global TracerProvider has already been created. '
'registerGlobalTracerProvider must be called only once before any '
'calls to getter globalTracerProvider.');
}

OpenTelemetrySdk(this._tracerProvider);

static OpenTelemetrySdk get globalOpenTelemetry => _globalOpenTelemetry;
TracerProvider get tracerProvider => _tracerProvider;
_tracerProvider = tracerProvider;
}

TracerProvider get globalTracerProvider => _tracerProvider;
4 changes: 1 addition & 3 deletions lib/src/sdk/trace/tracer_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import '../../api/instrumentation_library.dart' as version_api;
import '../../api/span_processors/span_processor.dart';
import '../../api/trace/tracer_provider.dart' as api;
import '../instrumentation_library.dart';
import 'exporters/console_exporter.dart';
import 'id_generator.dart';
import 'span_processors/simple_processor.dart';
import 'tracer.dart';

/// A registry for creating named [Tracer]s.
Expand All @@ -16,7 +14,7 @@ class TracerProvider implements api.TracerProvider {
IdGenerator _idGenerator;

TracerProvider({List<SpanProcessor> processors, IdGenerator idGenerator}) {
_processors = processors ?? [SimpleSpanProcessor(ConsoleExporter())];
_processors = processors ?? []; // Default to a no-op TracerProvider.
_idGenerator = idGenerator ?? IdGenerator();
// TODO: O11Y-1027: Per spec, a Sampler defaulted to ParentBased(root=AlwaysOn) added here.
}
Expand Down

0 comments on commit 5ff2864

Please sign in to comment.