-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API for registering a global instance.
- Loading branch information
Michael Yeager
committed
Nov 11, 2021
1 parent
9a4dd53
commit 5ff2864
Showing
3 changed files
with
14 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters