initialize static method

void initialize({
  1. String endpoint = OTelFactory.defaultEndpoint,
  2. String? serviceName = OTelAPI.defaultServiceName,
  3. String? serviceVersion = OTelAPI.defaultServiceVersion,
  4. OTelFactoryCreationFunction? oTelFactoryCreationFunction,
})

Typically developers will want to initialize OTel (the SDK), and not OTelAPI (the no-op API). The initialize method must be called before any other methods.

The global default TracerProvider and it's tracers will use the provided parameters. endpoint is a url, http://localhost:4317 uses the default port for the default gRPC protocol on a local host, http://localhost:4318 for http. serviceName SHOULD uniquely identify the instrumentation scope, such as the instrumentation library (e.g. @dart/dartastic_opentelemetry_api), package, module or class name. serviceVersion defaults to the matching OTel spec version plus a release version of this library, currently 1.11.0.0 otelFactoryCreationFunction defaults to a function that constructs the noop OTelAPIFactory as required by the specification. A factory method is required for serialization across execution contexts (isolates). Using OTel.initialize in the dartastic_opentelemetry library sets this factory to an SDK factory, which is the typical usage.

Implementation

static void initialize(
    {String endpoint = OTelFactory.defaultEndpoint,
    String? serviceName = OTelAPI.defaultServiceName,
    String? serviceVersion = OTelAPI.defaultServiceVersion,
    OTelFactoryCreationFunction? oTelFactoryCreationFunction}) {
  if (OTelFactory.otelFactory != null) {
    throw StateError(
        'OTelAPI can only be initialized once. If you need multiple endpoints or service names or versions create a named TracerProvider');
  }
  if (endpoint.isEmpty) {
    throw ArgumentError(
        'endpoint must not be empty.  Since the API (but not the SDK) uses noop processors by default, this can be any url for the API such as http://localhost:4317.');
  }
  if (serviceName == null || serviceName.isEmpty) {
    throw ArgumentError('serviceName must not be null or the empty string.');
  }
  if (serviceVersion == null || serviceVersion.isEmpty) {
    throw ArgumentError(
        'serviceVersion must not be null or the empty string.');
  }
  final factoryFactory =
      oTelFactoryCreationFunction ?? otelApiFactoryFactoryFunction;
  OTelFactory.otelFactory = factoryFactory(
      apiEndpoint: endpoint,
      apiServiceName: serviceName,
      apiServiceVersion: serviceVersion);
}