Skip to content

Commit 2b43c09

Browse files
[zipkin\otlp] Call SendAsync on mobile (open-telemetry#5821)
Co-authored-by: Mikel Blanchard <[email protected]>
1 parent 2979892 commit 2b43c09

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Notes](../../RELEASENOTES.md).
1818
Grace, etc.).
1919
([#5808](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5808))
2020

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

2327
Released 2024-Jun-14

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/BaseOtlpHttpExportClient.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClie
1313
internal abstract class BaseOtlpHttpExportClient<TRequest> : IExportClient<TRequest>
1414
{
1515
private static readonly ExportClientHttpResponse SuccessExportResponse = new ExportClientHttpResponse(success: true, deadlineUtc: default, response: null, exception: null);
16+
#if NET
17+
private readonly bool synchronousSendSupportedByCurrentPlatform;
18+
#endif
1619

1720
protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpClient, string signalPath)
1821
{
@@ -27,6 +30,14 @@ protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpC
2730
this.Endpoint = new UriBuilder(exporterEndpoint).Uri;
2831
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v));
2932
this.HttpClient = httpClient;
33+
34+
#if NET
35+
// See: https://github.com/dotnet/runtime/blob/280f2a0c60ce0378b8db49adc0eecc463d00fe5d/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs#L767
36+
this.synchronousSendSupportedByCurrentPlatform = !OperatingSystem.IsAndroid()
37+
&& !OperatingSystem.IsIOS()
38+
&& !OperatingSystem.IsTvOS()
39+
&& !OperatingSystem.IsBrowser();
40+
#endif
3041
}
3142

3243
internal HttpClient HttpClient { get; }
@@ -89,7 +100,9 @@ protected HttpRequestMessage CreateHttpRequest(TRequest exportRequest)
89100
protected HttpResponseMessage SendHttpRequest(HttpRequestMessage request, CancellationToken cancellationToken)
90101
{
91102
#if NET
92-
return this.HttpClient.Send(request, cancellationToken);
103+
return this.synchronousSendSupportedByCurrentPlatform
104+
? this.HttpClient.Send(request, cancellationToken)
105+
: this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
93106
#else
94107
return this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
95108
#endif

src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Notes](../../RELEASENOTES.md).
1010
`Convert.ToString` will now format using `CultureInfo.InvariantCulture`.
1111
([#5700](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5700))
1212

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

1519
Released 2024-Jun-14

src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class ZipkinExporter : BaseExporter<Activity>
2424
private readonly ZipkinExporterOptions options;
2525
private readonly int maxPayloadSizeInBytes;
2626
private readonly HttpClient httpClient;
27+
#if NET
28+
private readonly bool synchronousSendSupportedByCurrentPlatform;
29+
#endif
2730

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

3740
this.options = options;
38-
this.maxPayloadSizeInBytes = (!options.MaxPayloadSizeInBytes.HasValue || options.MaxPayloadSizeInBytes <= 0) ? ZipkinExporterOptions.DefaultMaxPayloadSizeInBytes : options.MaxPayloadSizeInBytes.Value;
41+
this.maxPayloadSizeInBytes = (!options.MaxPayloadSizeInBytes.HasValue || options.MaxPayloadSizeInBytes <= 0)
42+
? ZipkinExporterOptions.DefaultMaxPayloadSizeInBytes
43+
: options.MaxPayloadSizeInBytes.Value;
3944
this.httpClient = client ?? options.HttpClientFactory?.Invoke() ?? throw new InvalidOperationException("ZipkinExporter was missing HttpClientFactory or it returned null.");
45+
46+
#if NET
47+
// See: https://github.com/dotnet/runtime/blob/280f2a0c60ce0378b8db49adc0eecc463d00fe5d/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs#L767
48+
this.synchronousSendSupportedByCurrentPlatform = !OperatingSystem.IsAndroid()
49+
&& !OperatingSystem.IsIOS()
50+
&& !OperatingSystem.IsTvOS()
51+
&& !OperatingSystem.IsBrowser();
52+
#endif
4053
}
4154

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

6477
#if NET
65-
using var response = this.httpClient.Send(request, CancellationToken.None);
78+
using var response = this.synchronousSendSupportedByCurrentPlatform
79+
? this.httpClient.Send(request, CancellationToken.None)
80+
: this.httpClient.SendAsync(request, CancellationToken.None).GetAwaiter().GetResult();
6681
#else
6782
using var response = this.httpClient.SendAsync(request, CancellationToken.None).GetAwaiter().GetResult();
6883
#endif

0 commit comments

Comments
 (0)