|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using Apache.Arrow.Adbc.Telemetry.Traces.Exporters.FileExporter; |
| 21 | +using OpenTelemetry; |
| 22 | +using OpenTelemetry.Resources; |
| 23 | +using OpenTelemetry.Trace; |
| 24 | + |
| 25 | +namespace Apache.Arrow.Adbc.Telemetry.Traces.Exporters |
| 26 | +{ |
| 27 | + public class ExportersBuilder |
| 28 | + { |
| 29 | + private static readonly IReadOnlyDictionary<string, Func<string, string?, TracerProvider?>> s_tracerProviderFactoriesDefault; |
| 30 | + |
| 31 | + private readonly string _sourceName; |
| 32 | + private readonly string? _sourceVersion; |
| 33 | + private readonly IReadOnlyDictionary<string, Func<string, string?, TracerProvider?>> _tracerProviderFactories; |
| 34 | + |
| 35 | + static ExportersBuilder() |
| 36 | + { |
| 37 | + var defaultProviders = new Dictionary<string, Func<string, string?, TracerProvider?>> |
| 38 | + { |
| 39 | + [ExportersOptions.Exporters.None] = NewNoopTracerProvider, |
| 40 | + [ExportersOptions.Exporters.Otlp] = NewOtlpTracerProvider, |
| 41 | + [ExportersOptions.Exporters.Console] = NewConsoleTracerProvider, |
| 42 | + [ExportersOptions.Exporters.AdbcFile] = NewAdbcFileTracerProvider, |
| 43 | + }; |
| 44 | + s_tracerProviderFactoriesDefault = defaultProviders; |
| 45 | + } |
| 46 | + |
| 47 | + private ExportersBuilder(Builder builder) |
| 48 | + { |
| 49 | + _sourceName = builder.SourceName; |
| 50 | + _sourceVersion = builder.SourceVersion; |
| 51 | + _tracerProviderFactories = builder.TracerProviderFactories; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Build an <see cref="ExportersBuilder"/> from different possible Exporters. Use the Add* functions to add |
| 56 | + /// one or more <see cref="TracerProvider"/> factories. |
| 57 | + /// </summary> |
| 58 | + /// <param name="sourceName">The name of the source that the exporter will filter on.</param> |
| 59 | + /// <param name="sourceVersion">The (optional) version of the source that the exporter will filter on.</param> |
| 60 | + /// <returns>A <see cref="Builder"/> object.</returns> |
| 61 | + /// <exception cref="ArgumentNullException"></exception> |
| 62 | + public static Builder Build(string sourceName, string? sourceVersion = default, bool addDefaultExporters = false) |
| 63 | + { |
| 64 | + if (string.IsNullOrWhiteSpace(sourceName)) |
| 65 | + { |
| 66 | + throw new ArgumentNullException(nameof(sourceName)); |
| 67 | + } |
| 68 | + return new Builder(sourceName, sourceVersion, addDefaultExporters); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// <para> |
| 73 | + /// Attempts to activate an exporter based on the dictionary of <see cref="TracerProvider"/> factories |
| 74 | + /// added to the <see cref="Builder"/>. If the value of exporterOption is not null and not empty, it will be |
| 75 | + /// used as the key to the dictionary. If exporterOption is null or empty, then the exporter option will |
| 76 | + /// check the environment variable identified by environmentName (default <see cref="ExportersOptions.Environment.Exporter"/>). |
| 77 | + /// If both the exporterOption and the environment variable value are null or empty, then this function will return null |
| 78 | + /// and no exporeter will be activated. |
| 79 | + /// </para> |
| 80 | + /// <para> |
| 81 | + /// If the exporterOption or the value of the environment variable are not null and not empty, then |
| 82 | + /// if a matching factory delegate is found, it is called to activate the exporter. If no exception is thrown, |
| 83 | + /// the result of the factory method returns the result which may be null. If a matching function is found, the expoertName is set. |
| 84 | + /// If the factory delegate throws an exception it is not caught. |
| 85 | + /// </para> |
| 86 | + /// </summary> |
| 87 | + /// <param name="exporterOption">The value (name) of the exporter option, typically passed as option <see cref="ExportersOptions.Exporter"/>.</param> |
| 88 | + /// <param name="exporterName">The actual exporter name when successfully activated.</param> |
| 89 | + /// <param name="environmentName">The (optional) name of the environment variable to test for the exporter name. Default: <see cref="ExportersOptions.Environment.Exporter"/></param> |
| 90 | + /// <exception cref="AdbcException" /> |
| 91 | + /// <returns>The a non-null <see cref="TracerProvider"/> when successfully activated. Note: this object must be explicitly disposed when no longer necessary.</returns> |
| 92 | + public TracerProvider? Activate( |
| 93 | + string? exporterOption, |
| 94 | + out string? exporterName, |
| 95 | + string environmentName = ExportersOptions.Environment.Exporter) |
| 96 | + { |
| 97 | + TracerProvider? tracerProvider = null; |
| 98 | + exporterName = null; |
| 99 | + |
| 100 | + if (string.IsNullOrWhiteSpace(exporterOption)) |
| 101 | + { |
| 102 | + // Fall back to check the environment variable |
| 103 | + exporterOption = Environment.GetEnvironmentVariable(environmentName); |
| 104 | + } |
| 105 | + if (string.IsNullOrWhiteSpace(exporterOption)) |
| 106 | + { |
| 107 | + // Neither option or environment variable is set - no tracer provider will be activated. |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + if (!_tracerProviderFactories.TryGetValue(exporterOption!, out Func<string, string?, TracerProvider?>? factory)) |
| 112 | + { |
| 113 | + // Requested option has not been added via the builder |
| 114 | + throw AdbcException.NotImplemented($"Exporter option '{exporterOption}' is not implemented."); |
| 115 | + } |
| 116 | + |
| 117 | + tracerProvider = factory.Invoke(_sourceName, _sourceVersion); |
| 118 | + if (tracerProvider != null) |
| 119 | + { |
| 120 | + exporterName = exporterOption; |
| 121 | + } |
| 122 | + return tracerProvider; |
| 123 | + } |
| 124 | + |
| 125 | + public static TracerProvider NewAdbcFileTracerProvider(string sourceName, string? sourceVersion) => |
| 126 | + Sdk.CreateTracerProviderBuilder() |
| 127 | + .AddSource(sourceName) |
| 128 | + .ConfigureResource(resource => |
| 129 | + resource.AddService( |
| 130 | + serviceName: sourceName, |
| 131 | + serviceVersion: sourceVersion)) |
| 132 | + .AddAdbcFileExporter(sourceName) |
| 133 | + .Build(); |
| 134 | + |
| 135 | + public static TracerProvider NewConsoleTracerProvider(string sourceName, string? sourceVersion) => |
| 136 | + Sdk.CreateTracerProviderBuilder() |
| 137 | + .AddSource(sourceName) |
| 138 | + .ConfigureResource(resource => |
| 139 | + resource.AddService( |
| 140 | + serviceName: sourceName, |
| 141 | + serviceVersion: sourceVersion)) |
| 142 | + .AddConsoleExporter() |
| 143 | + .Build(); |
| 144 | + |
| 145 | + public static TracerProvider NewOtlpTracerProvider(string sourceName, string? sourceVersion) => |
| 146 | + Sdk.CreateTracerProviderBuilder() |
| 147 | + .AddSource(sourceName) |
| 148 | + .ConfigureResource(resource => |
| 149 | + resource.AddService( |
| 150 | + serviceName: sourceName, |
| 151 | + serviceVersion: sourceVersion)) |
| 152 | + .AddOtlpExporter() |
| 153 | + .Build(); |
| 154 | + |
| 155 | + public static TracerProvider? NewNoopTracerProvider(string sourceName, string? sourceVersion) => |
| 156 | + null; |
| 157 | + |
| 158 | + public class Builder |
| 159 | + { |
| 160 | + private readonly string _sourceName; |
| 161 | + private readonly string? _sourceVersion; |
| 162 | + private readonly Dictionary<string, Func<string, string?, TracerProvider?>> _tracerProviderFactories; |
| 163 | + |
| 164 | + internal string SourceName => _sourceName; |
| 165 | + |
| 166 | + internal string? SourceVersion => _sourceVersion; |
| 167 | + |
| 168 | + internal IReadOnlyDictionary<string, Func<string, string?, TracerProvider?>> TracerProviderFactories => _tracerProviderFactories; |
| 169 | + |
| 170 | + public Builder(string sourceName, string? sourceVersion, bool addDefaultExporters = false) |
| 171 | + { |
| 172 | + _sourceName = sourceName; |
| 173 | + _sourceVersion = sourceVersion; |
| 174 | + _tracerProviderFactories = []; |
| 175 | + if (addDefaultExporters) |
| 176 | + { |
| 177 | + AddDefaultExporters(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public ExportersBuilder Build() |
| 182 | + { |
| 183 | + if (_tracerProviderFactories.Count == 0) |
| 184 | + { |
| 185 | + throw new InvalidOperationException("No options provided. Please add one or more exporter."); |
| 186 | + } |
| 187 | + return new ExportersBuilder(this); |
| 188 | + } |
| 189 | + |
| 190 | + public Builder AddExporter(string exporterName, Func<string, string?, TracerProvider?> tracerProviderFactory) |
| 191 | + { |
| 192 | + if (string.IsNullOrWhiteSpace(exporterName)) |
| 193 | + { |
| 194 | + throw new ArgumentNullException(nameof(exporterName)); |
| 195 | + } |
| 196 | + |
| 197 | + _tracerProviderFactories.Add(exporterName, tracerProviderFactory); |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + private Builder AddDefaultExporters() |
| 202 | + { |
| 203 | + foreach (KeyValuePair<string, Func<string, string?, TracerProvider?>> item in s_tracerProviderFactoriesDefault) |
| 204 | + { |
| 205 | + _tracerProviderFactories[item.Key] = item.Value; |
| 206 | + } |
| 207 | + return this; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments