Line data Source code
1 : // Licensed under the Apache License, Version 2.0
2 : // Copyright 2025, Michael Bushe, All rights reserved.
3 : import 'package:dartastic_opentelemetry/src/trace/tracer_provider.dart';
4 : import 'package:dartastic_opentelemetry_api/dartastic_opentelemetry_api.dart';
5 : import '../metrics/meter_provider.dart';
6 :
7 : import '../resource/resource.dart';
8 :
9 : /// Factory function that creates an OTelSDKFactory with the specified configuration.
10 : ///
11 : /// Application developers should use the OTel class to create object, which uses this factory.
12 : /// This function serves as the default factory for creating OTelSDKFactory instances.
13 : /// It encapsulates the creation logic for the factory to make it simpler to create
14 : /// properly configured instances.
15 : ///
16 : /// @param apiEndpoint The endpoint URL for the OpenTelemetry collector
17 : /// @param apiServiceName The name of the service being instrumented
18 : /// @param apiServiceVersion The version of the service being instrumented
19 : /// @return A configured OTelSDKFactory instance
20 73 : OTelFactory otelSDKFactoryFactoryFunction({
21 : required String apiEndpoint,
22 : required String apiServiceName,
23 : required String apiServiceVersion,
24 : }) {
25 73 : return OTelSDKFactory(
26 : apiEndpoint: apiEndpoint,
27 : apiServiceName: apiServiceName,
28 : apiServiceVersion: apiServiceVersion,
29 : );
30 : }
31 :
32 : /// Factory implementation for creating OpenTelemetry SDK objects.
33 : ///
34 : /// The OTelSDKFactory extends the API factory to provide implementations
35 : /// of SDK-specific objects and override API object creation to use SDK implementations.
36 : /// This factory is the primary entry point for creating OpenTelemetry objects in the SDK.
37 : ///
38 : /// The OpenTelemetry specification requires the API to work without an SDK installed.
39 : /// When the SDK is installed, this factory replaces the API's factory to provide
40 : /// concrete implementations instead of no-op implementations.
41 : class OTelSDKFactory extends OTelAPIFactory {
42 : /// Creates a new OTelSDKFactory with the specified configuration.
43 : ///
44 : /// @param apiEndpoint The endpoint URL for the OpenTelemetry collector
45 : /// @param apiServiceName The name of the service being instrumented
46 : /// @param apiServiceVersion The version of the service being instrumented
47 : /// @param factoryFactory Optional factory function for creating new instances
48 73 : OTelSDKFactory(
49 : {required super.apiEndpoint,
50 : required super.apiServiceName,
51 : required super.apiServiceVersion,
52 : super.factoryFactory = otelSDKFactoryFactoryFunction});
53 :
54 : /// Creates a new Resource with the specified attributes and schema URL.
55 : ///
56 : /// Resources are immutable collections of attributes that describe the entity
57 : /// producing telemetry. They are an SDK concept that's not present in the API.
58 : ///
59 : /// @param attributes The attributes describing the resource
60 : /// @param schemaUrl Optional schema URL for the resource attributes
61 : /// @return A new Resource instance with the provided attributes
62 73 : Resource resource(Attributes attributes, [String? schemaUrl]) {
63 73 : return ResourceCreate.create(attributes, schemaUrl);
64 : }
65 :
66 : /// Creates an empty resource with no attributes.
67 : ///
68 : /// This is a convenience method for quickly creating an empty resource
69 : /// when no resource attributes are needed.
70 : ///
71 : /// @return A new empty Resource instance
72 0 : Resource resourceEmpty() {
73 0 : return resource(attributesFromMap({}), null);
74 : }
75 :
76 : /// Creates a TracerProvider with the specified configuration.
77 : ///
78 : /// This implementation overrides the API's method to create an SDK TracerProvider
79 : /// that produces real traces instead of no-op traces.
80 : ///
81 : /// @param endpoint The endpoint URL for the OpenTelemetry collector
82 : /// @param serviceName The name of the service being instrumented
83 : /// @param serviceVersion The version of the service being instrumented
84 : /// @param resource Optional resource describing the service
85 : /// @return A configured TracerProvider instance
86 73 : @override
87 : APITracerProvider tracerProvider(
88 : {required String endpoint,
89 : String serviceName =
90 : "@dart/opentelemetry_api", //TODO - @dart/dartastic_opentelemetry
91 : String? serviceVersion,
92 : Resource? resource}) {
93 73 : return SDKTracerProviderCreate.create(
94 73 : delegate: super.tracerProvider(
95 : endpoint: endpoint,
96 : serviceVersion: serviceVersion,
97 : serviceName: serviceName),
98 : resource: resource);
99 : }
100 :
101 : /// Creates a MeterProvider with the specified configuration.
102 : ///
103 : /// This implementation overrides the API's method to create an SDK MeterProvider
104 : /// that produces real metrics instead of no-op metrics.
105 : ///
106 : /// @param endpoint The endpoint URL for the OpenTelemetry collector
107 : /// @param serviceName The name of the service being instrumented
108 : /// @param serviceVersion The version of the service being instrumented
109 : /// @param resource Optional resource describing the service
110 : /// @return A configured MeterProvider instance
111 69 : @override
112 : APIMeterProvider meterProvider(
113 : {required String endpoint,
114 : String serviceName = "@dart/opentelemetry_api",
115 : String? serviceVersion,
116 : Resource? resource}) {
117 69 : return SDKMeterProviderCreate.create(
118 69 : delegate: super.meterProvider(
119 : endpoint: endpoint,
120 : serviceVersion: serviceVersion,
121 : serviceName: serviceName),
122 : resource: resource);
123 : }
124 : }
|