-
Notifications
You must be signed in to change notification settings - Fork 890
[OTLP] Limit response body size read by exporters #7017
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
Merged
martincostello
merged 9 commits into
open-telemetry:main
from
martincostello:limit-response-size-read
Apr 6, 2026
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
18b5811
[OTLP] Limit response body size read by exporters
martincostello ba293a4
[OTLP] Update CHANGELOG
martincostello 69909e8
[OTLP] Fix method name
martincostello c92243b
[OTLP] Address review feedback
martincostello f8070b6
[OTLP] Update default limit
martincostello c7bf057
[OTLP] Rent buffer
martincostello 0d7bd6c
[OTLP] Add test for compression
martincostello bb18cb2
[OTLP] Address feedback
martincostello 31336b7
[OTLP] Add truncation suffix
martincostello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExportClientTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #if NETFRAMEWORK | ||
| using System.Net.Http; | ||
| #endif | ||
| using System.Net; | ||
| using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient; | ||
|
|
||
| namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests; | ||
|
|
||
| public class OtlpExportClientTests | ||
| { | ||
| private const int MessageSizeLimit = 32 * 1024; | ||
|
|
||
| [Fact] | ||
| public void TryGetResponseBody_NullHttpResponse_ReturnsNull() | ||
| { | ||
| // Arrange | ||
| HttpResponseMessage? httpResponse = null; | ||
| var cancellationToken = CancellationToken.None; | ||
|
|
||
| // Act | ||
| var actual = OtlpExportClient.TryGetResponseBody(httpResponse, cancellationToken); | ||
|
|
||
| // Assert | ||
| Assert.Null(actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryGetResponseBody_HttpResponseWithoutContent_ReturnsNull() | ||
| { | ||
| // Arrange | ||
| using var httpResponse = new HttpResponseMessage() { Content = null }; | ||
| var cancellationToken = CancellationToken.None; | ||
|
|
||
| // Act | ||
| var actual = OtlpExportClient.TryGetResponseBody(httpResponse, cancellationToken); | ||
|
|
||
| // Assert | ||
| #if NETFRAMEWORK | ||
| Assert.Null(actual); | ||
| #else | ||
| Assert.Equal(string.Empty, actual); | ||
| #endif | ||
| } | ||
|
martincostello marked this conversation as resolved.
|
||
|
|
||
| [Theory] | ||
| [InlineData(0)] | ||
| [InlineData(1)] | ||
| [InlineData(256)] | ||
| [InlineData(1024)] | ||
| [InlineData((30 * 1024) - 1)] | ||
| [InlineData(30 * 1024)] | ||
| public void TryGetResponseBody_SmallContent_ReturnsFullContent(int length) | ||
| { | ||
| // Arrange | ||
| var expected = new string('A', length); | ||
| var cancellationToken = CancellationToken.None; | ||
|
|
||
| using var httpResponse = new HttpResponseMessage() | ||
| { | ||
| Content = new StringContent(expected), | ||
| }; | ||
|
|
||
| // Act | ||
| var actual = OtlpExportClient.TryGetResponseBody(httpResponse, cancellationToken); | ||
|
|
||
| // Assert | ||
| Assert.Equal(expected, actual); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(1)] | ||
| [InlineData(1024)] | ||
| [InlineData(2048)] | ||
| public void TryGetResponseBody_ContentExceedsLimit_ReturnsTruncatedContent(int excess) | ||
| { | ||
| // Arrange | ||
| var content = new string('C', MessageSizeLimit + excess); | ||
| var cancellationToken = CancellationToken.None; | ||
|
|
||
| using var httpResponse = new HttpResponseMessage | ||
| { | ||
| Content = new StringContent(content), | ||
| }; | ||
|
|
||
| // Act | ||
| var actual = OtlpExportClient.TryGetResponseBody(httpResponse, cancellationToken); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(actual); | ||
| Assert.Equal(MessageSizeLimit, actual.Length); | ||
| Assert.Equal(new string('C', MessageSizeLimit), actual); | ||
| } | ||
|
|
||
|
martincostello marked this conversation as resolved.
|
||
| [Fact] | ||
| public void TryGetResponseBody_EmptyContent_ReturnsEmptyString() | ||
| { | ||
| // Arrange | ||
| var cancellationToken = CancellationToken.None; | ||
|
|
||
| using var httpResponse = new HttpResponseMessage | ||
| { | ||
| Content = new StringContent(string.Empty), | ||
| }; | ||
|
|
||
| // Act | ||
| var actual = OtlpExportClient.TryGetResponseBody(httpResponse, cancellationToken); | ||
|
|
||
| // Assert | ||
| Assert.Equal(string.Empty, actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryGetResponseBody_ExceptionDuringRead_ReturnsNull() | ||
| { | ||
| // Arrange | ||
| var cancellationToken = CancellationToken.None; | ||
|
|
||
| using var httpResponse = new HttpResponseMessage | ||
| { | ||
| Content = new ThrowingHttpContent(), | ||
| }; | ||
|
|
||
| // Act | ||
| var actual = OtlpExportClient.TryGetResponseBody(httpResponse, cancellationToken); | ||
|
|
||
| // Assert | ||
| Assert.Null(actual); | ||
| } | ||
|
|
||
| private sealed class ThrowingHttpContent : HttpContent | ||
| { | ||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) | ||
| => throw new InvalidOperationException("Test exception"); | ||
|
|
||
| #if NET | ||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) | ||
| => throw new InvalidOperationException("Test exception"); | ||
| #endif | ||
|
|
||
| protected override bool TryComputeLength(out long length) | ||
| { | ||
| length = 0; | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.