Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Most flexibility provides gRPC connection #4815

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ OpenTelemetry.Exporter.OtlpExporterOptions.Headers.get -> string
OpenTelemetry.Exporter.OtlpExporterOptions.Headers.set -> void
OpenTelemetry.Exporter.OtlpExporterOptions.HttpClientFactory.get -> System.Func<System.Net.Http.HttpClient>
OpenTelemetry.Exporter.OtlpExporterOptions.HttpClientFactory.set -> void
OpenTelemetry.Exporter.OtlpExporterOptions.HttpHandlerFactory.get -> System.Func<System.Net.Http.HttpMessageHandler>
OpenTelemetry.Exporter.OtlpExporterOptions.HttpHandlerFactory.set -> void
OpenTelemetry.Exporter.OtlpExporterOptions.OtlpExporterOptions() -> void
OpenTelemetry.Exporter.OtlpExporterOptions.Protocol.get -> OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExporterOptions.Protocol.set -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ OpenTelemetry.Exporter.OtlpExporterOptions.Headers.get -> string
OpenTelemetry.Exporter.OtlpExporterOptions.Headers.set -> void
OpenTelemetry.Exporter.OtlpExporterOptions.HttpClientFactory.get -> System.Func<System.Net.Http.HttpClient>
OpenTelemetry.Exporter.OtlpExporterOptions.HttpClientFactory.set -> void
OpenTelemetry.Exporter.OtlpExporterOptions.HttpHandlerFactory.get -> System.Func<System.Net.Http.HttpMessageHandler>
OpenTelemetry.Exporter.OtlpExporterOptions.HttpHandlerFactory.set -> void
OpenTelemetry.Exporter.OtlpExporterOptions.OtlpExporterOptions() -> void
OpenTelemetry.Exporter.OtlpExporterOptions.Protocol.get -> OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExporterOptions.Protocol.set -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* GrpcExporter support custom HttpClient.
([#4813](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4815))

## 1.6.0

Released 2023-Sep-05
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,41 @@ public Uri Endpoint
/// </remarks>
public Func<HttpClient> HttpClientFactory { get; set; }

#if NETSTANDARD2_1 || NET6_0_OR_GREATER
/// <summary>
/// Gets or sets the factory function called to create the <see
/// cref="HttpMessageHandler"/> 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>This is only invoked for the <see
/// cref="OtlpExportProtocol.Grpc"/> protocol.</item>
/// <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.ihttpmessagehandlerfactory">IHttpMessageHandlerFactory</a>
/// instance can be resolved through the application <see
/// cref="IServiceProvider"/> then an <see cref="HttpMessageHandler"/> will be
/// created through the factory with the name "OtlpTraceExporter"
/// otherwise an <see cref="HttpMessageHandler"/> 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.ihttpmessagehandlerfactory">IHttpMessageHandlerFactory</a>
/// instance can be resolved through the application <see
/// cref="IServiceProvider"/> then an <see cref="HttpMessageHandler"/> will be
/// created through the factory with the name "OtlpMetricExporter"
/// otherwise an <see cref="HttpMessageHandler"/> will be instantiated
/// directly.</item>
/// </list>
/// </remarks>
public Func<HttpMessageHandler> HttpHandlerFactory { get; set; }
#endif

/// <summary>
/// Gets a value indicating whether <see cref="Endpoint" /> was modified via its setter.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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 { HttpHandler = options.HttpHandlerFactory?.Invoke() });
#else
ChannelCredentials channelCredentials;
if (options.Endpoint.Scheme == Uri.UriSchemeHttps)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// <copyright file="OtlpGrpcTraceExportClientTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if NET6_0_OR_GREATER
using System.Reflection;
using Grpc.Net.Client;
using Moq;
#endif
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using Xunit;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;

public class OtlpGrpcTraceExportClientTests
{
[Fact]
public void NewOtlpGrpcTraceExportClient_OtlpExporterOptions_ExporterHasCorrectProperties()
{
var header1 = new
{
Name = "hdr1",
Value = "val1"
};
var header2 = new
{
Name = "hdr2",
Value = "val2"
};

var options = new OtlpExporterOptions
{
Headers = $"{header1.Name}={header1.Value}, {header2.Name} = {header2.Value}",
};

var client = new OtlpGrpcTraceExportClient(options);

Assert.NotNull(client.Channel);

Assert.Equal(2 + OtlpExporterOptions.StandardHeaders.Length, client.Headers.Count);
Assert.Contains(client.Headers, kvp => kvp.Key == header1.Name && kvp.Value == header1.Value);
Assert.Contains(client.Headers, kvp => kvp.Key == header2.Name && kvp.Value == header2.Value);

for (int i = 0; i < OtlpExporterOptions.StandardHeaders.Length; i++)
{
Assert.Contains(client.Headers, entry => entry.Key.Equals(OtlpExporterOptions.StandardHeaders[i].Key, StringComparison.OrdinalIgnoreCase) && entry.Value == OtlpExporterOptions.StandardHeaders[i].Value);
}
}

#if NET6_0_OR_GREATER
[Fact]
public void NewOtlpGrpcTraceExportClient_UseCustomHttpClient()
{
var httpHandler = new Mock<HttpMessageHandler>();

var options = new OtlpExporterOptions
{
HttpHandlerFactory = () => httpHandler.Object
};

var client = new OtlpGrpcTraceExportClient(options);

var httpInvoker = Assert.IsAssignableFrom<HttpMessageInvoker>(typeof(GrpcChannel).GetProperty("HttpInvoker", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(client.Channel));

var httpMessageHandler = Assert.IsAssignableFrom<HttpMessageHandler>(typeof(HttpMessageInvoker).GetField("_handler", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(httpInvoker));

while (httpMessageHandler is DelegatingHandler delegatingHandler)
{
httpMessageHandler = delegatingHandler.InnerHandler;
}

Assert.Equal(httpHandler.Object, httpMessageHandler);
}
#endif
}