|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#nullable disable |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Diagnostics; |
| 8 | +using Azure.Core; |
| 9 | +using Azure.Monitor.OpenTelemetry.Exporter.Internals; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.Options; |
| 12 | +using OpenTelemetry; |
| 13 | +using OpenTelemetry.Logs; |
| 14 | +using OpenTelemetry.Metrics; |
| 15 | +using OpenTelemetry.Trace; |
| 16 | + |
| 17 | +namespace Azure.Monitor.OpenTelemetry.Exporter |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Extension methods to simplify registering of Azure Monitor Exporter for all signals. |
| 21 | + /// </summary> |
| 22 | + public static class AzureMonitorExporterExtensions |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// Adds Azure Monitor Trace exporter to the TracerProvider. |
| 26 | + /// </summary> |
| 27 | + /// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param> |
| 28 | + /// <param name="configure">Callback action for configuring <see cref="AzureMonitorExporterOptions"/>.</param> |
| 29 | + /// <param name="credential"> |
| 30 | + /// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token. |
| 31 | + /// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter, |
| 32 | + /// the Options will take precedence. |
| 33 | + /// </param> |
| 34 | + /// <param name="name">Name which is used when retrieving options.</param> |
| 35 | + /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns> |
| 36 | + public static TracerProviderBuilder AddAzureMonitorTraceExporter( |
| 37 | + this TracerProviderBuilder builder, |
| 38 | + Action<AzureMonitorExporterOptions> configure = null, |
| 39 | + TokenCredential credential = null, |
| 40 | + string name = null) |
| 41 | + { |
| 42 | + if (builder == null) |
| 43 | + { |
| 44 | + throw new ArgumentNullException(nameof(builder)); |
| 45 | + } |
| 46 | + |
| 47 | + var finalOptionsName = name ?? Options.DefaultName; |
| 48 | + |
| 49 | + if (name != null && configure != null) |
| 50 | + { |
| 51 | + // If we are using named options we register the |
| 52 | + // configuration delegate into options pipeline. |
| 53 | + builder.ConfigureServices(services => services.Configure(finalOptionsName, configure)); |
| 54 | + } |
| 55 | + |
| 56 | + var deferredBuilder = builder as IDeferredTracerProviderBuilder; |
| 57 | + if (deferredBuilder == null) |
| 58 | + { |
| 59 | + throw new InvalidOperationException("The provided TracerProviderBuilder does not implement IDeferredTracerProviderBuilder."); |
| 60 | + } |
| 61 | + |
| 62 | + return deferredBuilder.Configure((sp, builder) => |
| 63 | + { |
| 64 | + var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get(finalOptionsName); |
| 65 | + if (name == null && configure != null) |
| 66 | + { |
| 67 | + // If we are NOT using named options, we execute the |
| 68 | + // configuration delegate inline. The reason for this is |
| 69 | + // AzureMonitorExporterOptions is shared by all signals. Without a |
| 70 | + // name, delegates for all signals will mix together. See: |
| 71 | + // https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043 |
| 72 | + configure(exporterOptions); |
| 73 | + } |
| 74 | + |
| 75 | + builder.SetSampler(new ApplicationInsightsSampler(exporterOptions.SamplingRatio)); |
| 76 | + |
| 77 | + if (credential != null) |
| 78 | + { |
| 79 | + // Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter. |
| 80 | + // Options should take precedence. |
| 81 | + exporterOptions.Credential ??= credential; |
| 82 | + } |
| 83 | + |
| 84 | + builder.AddProcessor(new CompositeProcessor<Activity>(new BaseProcessor<Activity>[] |
| 85 | + { |
| 86 | + new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(exporterOptions)), |
| 87 | + new BatchActivityExportProcessor(new AzureMonitorTraceExporter(exporterOptions)) |
| 88 | + })); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Adds Azure Monitor Metric exporter. |
| 94 | + /// </summary> |
| 95 | + /// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param> |
| 96 | + /// <param name="configure">Exporter configuration options.</param> |
| 97 | + /// <param name="credential"> |
| 98 | + /// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token. |
| 99 | + /// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter, |
| 100 | + /// the Options will take precedence. |
| 101 | + /// </param> |
| 102 | + /// <param name="name">Name which is used when retrieving options.</param> |
| 103 | + /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns> |
| 104 | + public static MeterProviderBuilder AddAzureMonitorMetricExporter( |
| 105 | + this MeterProviderBuilder builder, |
| 106 | + Action<AzureMonitorExporterOptions> configure = null, |
| 107 | + TokenCredential credential = null, |
| 108 | + string name = null) |
| 109 | + { |
| 110 | + if (builder == null) |
| 111 | + { |
| 112 | + throw new ArgumentNullException(nameof(builder)); |
| 113 | + } |
| 114 | + |
| 115 | + var finalOptionsName = name ?? Options.DefaultName; |
| 116 | + |
| 117 | + if (name != null && configure != null) |
| 118 | + { |
| 119 | + // If we are using named options we register the |
| 120 | + // configuration delegate into options pipeline. |
| 121 | + builder.ConfigureServices(services => services.Configure(finalOptionsName, configure)); |
| 122 | + } |
| 123 | + |
| 124 | + return builder.AddReader(sp => |
| 125 | + { |
| 126 | + var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get(finalOptionsName); |
| 127 | + |
| 128 | + if (name == null && configure != null) |
| 129 | + { |
| 130 | + // If we are NOT using named options, we execute the |
| 131 | + // configuration delegate inline. The reason for this is |
| 132 | + // AzureMonitorExporterOptions is shared by all signals. Without a |
| 133 | + // name, delegates for all signals will mix together. See: |
| 134 | + // https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043 |
| 135 | + configure(exporterOptions); |
| 136 | + } |
| 137 | + |
| 138 | + if (credential != null) |
| 139 | + { |
| 140 | + // Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter. |
| 141 | + // Options should take precedence. |
| 142 | + exporterOptions.Credential ??= credential; |
| 143 | + } |
| 144 | + |
| 145 | + return new PeriodicExportingMetricReader(new AzureMonitorMetricExporter(exporterOptions)) |
| 146 | + { TemporalityPreference = MetricReaderTemporalityPreference.Delta }; |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Adds Azure Monitor Log Exporter with OpenTelemetryLoggerOptions. |
| 152 | + /// </summary> |
| 153 | + /// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param> |
| 154 | + /// <param name="configure">Exporter configuration options.</param> |
| 155 | + /// <param name="credential"> |
| 156 | + /// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token. |
| 157 | + /// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter, |
| 158 | + /// the Options will take precedence. |
| 159 | + /// </param> |
| 160 | + /// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns> |
| 161 | + public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter( |
| 162 | + this OpenTelemetryLoggerOptions loggerOptions, |
| 163 | + Action<AzureMonitorExporterOptions> configure = null, |
| 164 | + TokenCredential credential = null) |
| 165 | + { |
| 166 | + if (loggerOptions == null) |
| 167 | + { |
| 168 | + throw new ArgumentNullException(nameof(loggerOptions)); |
| 169 | + } |
| 170 | + |
| 171 | + var options = new AzureMonitorExporterOptions(); |
| 172 | + configure?.Invoke(options); |
| 173 | + |
| 174 | + if (credential != null) |
| 175 | + { |
| 176 | + // Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter. |
| 177 | + // Options should take precedence. |
| 178 | + options.Credential ??= credential; |
| 179 | + } |
| 180 | + |
| 181 | + return loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options))); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments