Skip to content

Commit

Permalink
[zipkin\otlp] Call SendAsync on mobile (#5821)
Browse files Browse the repository at this point in the history
Co-authored-by: Mikel Blanchard <[email protected]>
  • Loading branch information
lukyamuziB and CodeBlanch authored Sep 12, 2024
1 parent 2979892 commit 2b43c09
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Notes](../../RELEASENOTES.md).
Grace, etc.).
([#5808](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5808))

* Fixed `PlatformNotSupportedException`s being thrown during export when running
on mobile platforms which caused telemetry to be dropped silently.
([#5821](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/5821))

## 1.9.0

Released 2024-Jun-14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClie
internal abstract class BaseOtlpHttpExportClient<TRequest> : IExportClient<TRequest>
{
private static readonly ExportClientHttpResponse SuccessExportResponse = new ExportClientHttpResponse(success: true, deadlineUtc: default, response: null, exception: null);
#if NET
private readonly bool synchronousSendSupportedByCurrentPlatform;
#endif

protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpClient, string signalPath)
{
Expand All @@ -27,6 +30,14 @@ protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpC
this.Endpoint = new UriBuilder(exporterEndpoint).Uri;
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v));
this.HttpClient = httpClient;

#if NET
// See: https://github.com/dotnet/runtime/blob/280f2a0c60ce0378b8db49adc0eecc463d00fe5d/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs#L767
this.synchronousSendSupportedByCurrentPlatform = !OperatingSystem.IsAndroid()
&& !OperatingSystem.IsIOS()
&& !OperatingSystem.IsTvOS()
&& !OperatingSystem.IsBrowser();
#endif
}

internal HttpClient HttpClient { get; }
Expand Down Expand Up @@ -89,7 +100,9 @@ protected HttpRequestMessage CreateHttpRequest(TRequest exportRequest)
protected HttpResponseMessage SendHttpRequest(HttpRequestMessage request, CancellationToken cancellationToken)
{
#if NET
return this.HttpClient.Send(request, cancellationToken);
return this.synchronousSendSupportedByCurrentPlatform
? this.HttpClient.Send(request, cancellationToken)
: this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
#else
return this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Notes](../../RELEASENOTES.md).
`Convert.ToString` will now format using `CultureInfo.InvariantCulture`.
([#5700](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5700))

* Fixed `PlatformNotSupportedException`s being thrown during export when running
on mobile platforms which caused telemetry to be dropped silently.
([#5821](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/5821))

## 1.9.0

Released 2024-Jun-14
Expand Down
19 changes: 17 additions & 2 deletions src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class ZipkinExporter : BaseExporter<Activity>
private readonly ZipkinExporterOptions options;
private readonly int maxPayloadSizeInBytes;
private readonly HttpClient httpClient;
#if NET
private readonly bool synchronousSendSupportedByCurrentPlatform;
#endif

/// <summary>
/// Initializes a new instance of the <see cref="ZipkinExporter"/> class.
Expand All @@ -35,8 +38,18 @@ public ZipkinExporter(ZipkinExporterOptions options, HttpClient? client = null)
Guard.ThrowIfNull(options);

this.options = options;
this.maxPayloadSizeInBytes = (!options.MaxPayloadSizeInBytes.HasValue || options.MaxPayloadSizeInBytes <= 0) ? ZipkinExporterOptions.DefaultMaxPayloadSizeInBytes : options.MaxPayloadSizeInBytes.Value;
this.maxPayloadSizeInBytes = (!options.MaxPayloadSizeInBytes.HasValue || options.MaxPayloadSizeInBytes <= 0)
? ZipkinExporterOptions.DefaultMaxPayloadSizeInBytes
: options.MaxPayloadSizeInBytes.Value;
this.httpClient = client ?? options.HttpClientFactory?.Invoke() ?? throw new InvalidOperationException("ZipkinExporter was missing HttpClientFactory or it returned null.");

#if NET
// See: https://github.com/dotnet/runtime/blob/280f2a0c60ce0378b8db49adc0eecc463d00fe5d/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs#L767
this.synchronousSendSupportedByCurrentPlatform = !OperatingSystem.IsAndroid()
&& !OperatingSystem.IsIOS()
&& !OperatingSystem.IsTvOS()
&& !OperatingSystem.IsBrowser();
#endif
}

internal ZipkinEndpoint? LocalEndpoint { get; private set; }
Expand All @@ -62,7 +75,9 @@ public override ExportResult Export(in Batch<Activity> batch)
};

#if NET
using var response = this.httpClient.Send(request, CancellationToken.None);
using var response = this.synchronousSendSupportedByCurrentPlatform
? this.httpClient.Send(request, CancellationToken.None)
: this.httpClient.SendAsync(request, CancellationToken.None).GetAwaiter().GetResult();
#else
using var response = this.httpClient.SendAsync(request, CancellationToken.None).GetAwaiter().GetResult();
#endif
Expand Down

0 comments on commit 2b43c09

Please sign in to comment.