Skip to content

Commit

Permalink
Make OtlpExporterOptions.HttpClientFactory work with GRPC protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhoang committed Jun 29, 2023
1 parent 104cf32 commit 3c421d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,37 @@ public Uri Endpoint
/// </summary>
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; }

#if NETSTANDARD2_1 || NET6_0_OR_GREATER
/// <summary>
/// Gets or sets the factory function called to create the <see
/// cref="HttpClient"/> instance that will be used at runtime to
/// transmit telemetry over HTTP. The returned instance will be reused
/// for all export invocations.
/// </summary>
/// <remarks>
/// Notes:
/// <list type="bullet">
/// <item>The default behavior when using the <see
/// cref="OtlpTraceExporterHelperExtensions.AddOtlpExporter(TracerProviderBuilder,
/// Action{OtlpExporterOptions})"/> extension is if an <a
/// href="https://docs.microsoft.com/dotnet/api/system.net.http.ihttpclientfactory">IHttpClientFactory</a>
/// instance can be resolved through the application <see
/// cref="IServiceProvider"/> then an <see cref="HttpClient"/> will be
/// created through the factory with the name "OtlpTraceExporter"
/// otherwise an <see cref="HttpClient"/> will be instantiated
/// directly.</item>
/// <item>The default behavior when using the <see
/// cref="OtlpMetricExporterExtensions.AddOtlpExporter(MeterProviderBuilder,
/// Action{OtlpExporterOptions})"/> extension is if an <a
/// href="https://docs.microsoft.com/dotnet/api/system.net.http.ihttpclientfactory">IHttpClientFactory</a>
/// instance can be resolved through the application <see
/// cref="IServiceProvider"/> then an <see cref="HttpClient"/> will be
/// created through the factory with the name "OtlpMetricExporter"
/// otherwise an <see cref="HttpClient"/> will be instantiated
/// directly.</item>
/// </list>
/// </remarks>
#else
/// <summary>
/// Gets or sets the factory function called to create the <see
/// cref="HttpClient"/> instance that will be used at runtime to
Expand Down Expand Up @@ -188,6 +219,7 @@ public Uri Endpoint
/// directly.</item>
/// </list>
/// </remarks>
#endif
public Func<HttpClient> HttpClientFactory { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public static Channel CreateChannel(this OtlpExporterOptions options)
}

#if NETSTANDARD2_1 || NET6_0_OR_GREATER
return GrpcChannel.ForAddress(options.Endpoint);
return GrpcChannel.ForAddress(options.Endpoint, new GrpcChannelOptions
{
HttpClient = options.HttpClientFactory?.Invoke()
?? throw new InvalidOperationException("OtlpExporterOptions was missing HttpClientFactory or it returned null."),
});
#else
ChannelCredentials channelCredentials;
if (options.Endpoint.Scheme == Uri.UriSchemeHttps)
Expand Down Expand Up @@ -130,7 +134,10 @@ public static THeaders GetHeaders<THeaders>(this OtlpExporterOptions options, Ac
public static void TryEnableIHttpClientFactoryIntegration(this OtlpExporterOptions options, IServiceProvider serviceProvider, string httpClientName)
{
if (serviceProvider != null
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
#else
&& options.Protocol == OtlpExportProtocol.HttpProtobuf
#endif
&& options.HttpClientFactory == options.DefaultHttpClientFactory)
{
options.HttpClientFactory = () =>
Expand Down

0 comments on commit 3c421d2

Please sign in to comment.