|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#nullable enable |
| 5 | + |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using OpenTelemetry.Exporter; |
| 8 | +using OpenTelemetry.Internal; |
| 9 | + |
| 10 | +namespace OpenTelemetry; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Contains extension methods to facilitate registration of the OpenTelemetry |
| 14 | +/// Protocol (OTLP) exporter into an <see cref="IOpenTelemetryBuilder"/> |
| 15 | +/// instance. |
| 16 | +/// </summary> |
| 17 | +public static class OpenTelemetryBuilderOtlpExporterExtensions |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Uses OpenTelemetry Protocol (OTLP) exporter for all signals. |
| 21 | + /// </summary> |
| 22 | + /// <remarks> |
| 23 | + /// Notes: |
| 24 | + /// <list type="bullet"> |
| 25 | + /// <item>Calling this method automatically enables logging, metrics, and |
| 26 | + /// tracing.</item> |
| 27 | + /// <item>The exporter registered by this method will be added as the last |
| 28 | + /// processor in the pipeline established for logging and tracing.</item> |
| 29 | + /// <item>This method can only be called once. Subsequent calls will results |
| 30 | + /// in a <see cref="NotSupportedException"/> being thrown.</item> |
| 31 | + /// <item>This method cannot be called in addition to signal-specific |
| 32 | + /// <c>AddOtlpExporter</c> methods. If this method is called signal-specific |
| 33 | + /// <c>AddOtlpExporter</c> calls will result in a <see |
| 34 | + /// cref="NotSupportedException"/> being thrown.</item> |
| 35 | + /// </list> |
| 36 | + /// </remarks> |
| 37 | + /// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param> |
| 38 | + /// <returns>Supplied <see cref="IOpenTelemetryBuilder"/> for chaining calls.</returns> |
| 39 | + public static IOpenTelemetryBuilder UseOtlpExporter( |
| 40 | + this IOpenTelemetryBuilder builder) |
| 41 | + => UseOtlpExporter(builder, name: null, configuration: null, configure: null); |
| 42 | + |
| 43 | + /// <summary><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)"/></summary> |
| 44 | + /// <remarks><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/remarks"/></remarks> |
| 45 | + /// <returns><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/returns"/></returns> |
| 46 | + /// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param> |
| 47 | + /// <param name="protocol"><see cref="OtlpExportProtocol"/>.</param> |
| 48 | + /// <param name="baseEndpoint"> |
| 49 | + /// <para>Base endpoint to use.</para> |
| 50 | + /// Note: A signal-specific path will be appended to the base endpoint for |
| 51 | + /// each signal automatically if the protocol is set to <see |
| 52 | + /// cref="OtlpExportProtocol.HttpProtobuf"/>. |
| 53 | + /// </param> |
| 54 | + public static IOpenTelemetryBuilder UseOtlpExporter( |
| 55 | + this IOpenTelemetryBuilder builder, |
| 56 | + OtlpExportProtocol protocol, |
| 57 | + Uri baseEndpoint) |
| 58 | + { |
| 59 | + Guard.ThrowIfNull(baseEndpoint); |
| 60 | + |
| 61 | + return UseOtlpExporter(builder, name: null, configuration: null, configure: otlpBuilder => |
| 62 | + { |
| 63 | + otlpBuilder.ConfigureDefaultExporterOptions(o => |
| 64 | + { |
| 65 | + o.Protocol = protocol; |
| 66 | + o.Endpoint = baseEndpoint; |
| 67 | + }); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)"/></summary> |
| 72 | + /// <remarks><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/remarks"/></remarks> |
| 73 | + /// <returns><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/returns"/></returns> |
| 74 | + /// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param> |
| 75 | + /// <param name="configure">Callback action for configuring <see cref="OtlpExporterBuilder"/>.</param> |
| 76 | + internal static IOpenTelemetryBuilder UseOtlpExporter( |
| 77 | + this IOpenTelemetryBuilder builder, |
| 78 | + Action<OtlpExporterBuilder> configure) |
| 79 | + { |
| 80 | + Guard.ThrowIfNull(configure); |
| 81 | + |
| 82 | + return UseOtlpExporter(builder, name: null, configuration: null, configure); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)"/></summary> |
| 86 | + /// <remarks><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/remarks"/></remarks> |
| 87 | + /// <returns><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/returns"/></returns> |
| 88 | + /// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param> |
| 89 | + /// <param name="configuration"> |
| 90 | + /// <para><see cref="IConfiguration"/> to bind onto <see cref="OtlpExporterBuilderOptions"/>.</para> |
| 91 | + /// <para>Notes: |
| 92 | + /// <list type="bullet"> |
| 93 | + /// <item docLink="true">See [TODO:Add doc link] for details on the configuration |
| 94 | + /// schema.</item> |
| 95 | + /// <item>The <see cref="OtlpExporterBuilderOptions"/> instance will be |
| 96 | + /// named "otlp" by default when calling this method.</item> |
| 97 | + /// </list> |
| 98 | + /// </para> |
| 99 | + /// </param> |
| 100 | + internal static IOpenTelemetryBuilder UseOtlpExporter( |
| 101 | + this IOpenTelemetryBuilder builder, |
| 102 | + IConfiguration configuration) |
| 103 | + { |
| 104 | + Guard.ThrowIfNull(configuration); |
| 105 | + |
| 106 | + return UseOtlpExporter(builder, name: null, configuration, configure: null); |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)"/></summary> |
| 110 | + /// <remarks><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/remarks"/></remarks> |
| 111 | + /// <returns><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/returns"/></returns> |
| 112 | + /// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param> |
| 113 | + /// <param name="name">Optional name which is used when retrieving options.</param> |
| 114 | + /// <param name="configuration"> |
| 115 | + /// <para>Optional <see cref="IConfiguration"/> to bind onto <see |
| 116 | + /// cref="OtlpExporterBuilderOptions"/>.</para> |
| 117 | + /// <para>Notes: |
| 118 | + /// <list type="bullet"> |
| 119 | + /// <item><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder, |
| 120 | + /// IConfiguration)" |
| 121 | + /// path="/param[@name='configuration']/para/list/item[@docLink='true']"/></item> |
| 122 | + /// <item>If <paramref name="name"/> is not set the <see |
| 123 | + /// cref="OtlpExporterBuilderOptions"/> instance will be named "otlp" by |
| 124 | + /// default when <paramref name="configuration"/> is used.</item> |
| 125 | + /// </list> |
| 126 | + /// </para> |
| 127 | + /// </param> |
| 128 | + /// <param name="configure">Optional callback action for configuring <see cref="OtlpExporterBuilder"/>.</param> |
| 129 | + internal static IOpenTelemetryBuilder UseOtlpExporter( |
| 130 | + this IOpenTelemetryBuilder builder, |
| 131 | + string? name, |
| 132 | + IConfiguration? configuration, |
| 133 | + Action<OtlpExporterBuilder>? configure) |
| 134 | + { |
| 135 | + Guard.ThrowIfNull(builder); |
| 136 | + |
| 137 | + // Note: We automatically turn on signals for "UseOtlpExporter" |
| 138 | + builder |
| 139 | + .WithLogging() |
| 140 | + .WithMetrics() |
| 141 | + .WithTracing(); |
| 142 | + |
| 143 | + var otlpBuilder = new OtlpExporterBuilder(builder.Services, name, configuration); |
| 144 | + |
| 145 | + configure?.Invoke(otlpBuilder); |
| 146 | + |
| 147 | + return builder; |
| 148 | + } |
| 149 | +} |
0 commit comments